Re: to_hex() for negative inputs

Поиск
Список
Период
Сортировка
От Aleksander Alekseev
Тема Re: to_hex() for negative inputs
Дата
Msg-id CAJ7c6TNfbbcqZbw03oxN7-U0h2oU=kwoqoHJG5LdU8e3r7zdoQ@mail.gmail.com
обсуждение исходный текст
Ответ на to_hex() for negative inputs  (Dean Rasheed <dean.a.rasheed@gmail.com>)
Ответы Re: to_hex() for negative inputs  (Dean Rasheed <dean.a.rasheed@gmail.com>)
Список pgsql-hackers
Hi Dean,

> I only recently realised that to_hex() converts its input to unsigned
> before converting it to hex (something that's not mentioned in the
> docs):

Technically the documentation is accurate [1]:

"""
Converts the number to its equivalent hexadecimal representation.
"""

But I agree that adding an example with negative numbers will not
hurt. Would you like to submit a patch?

> I also think it might be useful for it to gain a couple of boolean options:

Adding extra arguments for something the user can implement
(him/her)self doesn't seem to be a great idea. With this approach we
may end up with hundreds of arguments one day.

> 1). An option to output a signed value (defaulting to false, to
> preserve the current two's complement output).

This in particular can be done like this:

```
=# select case when sign(x) > 0 then '' else '-' end || to_hex(abs(x))
from ( values (123), (-123) ) as s(x);
 ?column?
----------
 7b
 -7b
(2 rows)
```

> 2). An option to output the base prefix "0x" (which comes after the
> sign, making it inconvenient for the user to add themselves).

Ditto:

```
=# select '0x' || to_hex(x) from ( values (123), (-123) ) as s(x);
  ?column?
------------
 0x7b
 0xffffff85
```

[1]: https://www.postgresql.org/docs/current/functions-string.html

-- 
Best regards,
Aleksander Alekseev



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Melanie Plageman
Дата:
Сообщение: Re: Monotonic WindowFunc support for ntile(), percent_rank() and cume_dist()
Следующее
От: Robert Haas
Дата:
Сообщение: Re: Non-superuser subscription owners