Thread: hex values

hex values

From
"Oberpriller, Wade D."
Date:
Does Postgres support hex values?

Can I do something like:

VAL := 0xFFFF;

And is it possible to output the value in a hex-string? So VAL would display
as 0xFFFF instead of 65535?

Wade Oberpriller                 http://www.storagetek.com
Software Development             Phone: (763) 424-1538
StorageTek: MRDC                        (800) 328-9108 ext. 1538
wade_oberpriller@storagetek.com  Fax:   (763) 391-1095

Re: hex values

From
Heiko Klein
Date:
Hi,

as far as I've found out, it doesn't.
Retrieving int data as hex I solved by this  plperl function:

CREATE FUNCTION int2hex(integer) RETURNS char(10)
    AS '$tmp = sprintf "%X", $_[0];
        return ("0x" . "0"x(4-length($tmp)) . $tmp);'
    LANGUAGE 'plperl';

This will give you allways 4 ciphers behind 0x. I didn't need it for
reading data, but at least, it should work about the same, i.e. if you have
several kinds of numbers, some dec, some hex, some oct, I would do
something like:

CREATE FUNCITON mynumber2int(char(10)) RETURNS integer
    AS 'return int($_[0]);'
    LANGUAGE 'plperl';

which simply uses perls internal conversion-mechanism.


I would also like postgres to include this behavior by default, at least
for the reading. But I haven't found out how.


Heiko

Oberpriller, Wade D. writes:
 > Does Postgres support hex values?
 >
 > Can I do something like:
 >
 > VAL := 0xFFFF;
 >
 > And is it possible to output the value in a hex-string? So VAL would display
 > as 0xFFFF instead of 65535?
 >
 > Wade Oberpriller                 http://www.storagetek.com
 > Software Development             Phone: (763) 424-1538
 > StorageTek: MRDC                        (800) 328-9108 ext. 1538
 > wade_oberpriller@storagetek.com  Fax:   (763) 391-1095
 >
 > ---------------------------(end of broadcast)---------------------------
 > TIP 6: Have you searched our list archives?
 >
 > http://archives.postgresql.org

Problem with factorial operator

From
Jean-Luc Lachance
Date:
Hi all,

The PG Documentation states:


tgl=> select cast(text '44' as int8) ! as "factorial";
      factorial
---------------------
 2673996885588443136
(1 row)


If you try it, you will get the same result, but it is the wrong answer.
Factorial work up to 20. After that one gets negative results.


JLL

Re: Problem with factorial operator

From
Tom Lane
Date:
Jean-Luc Lachance <jllachan@nsd.ca> writes:
> If you try it, you will get the same result, but it is the wrong answer.
> Factorial work up to 20. After that one gets negative results.

There's a TODO item to replace the existing integer-based factorial ops
with a NUMERIC-based one, which would work for output values up to
whatever the NUMERIC precision limit is (10^1000, I think).  Want to
work on that?

            regards, tom lane

Re: Problem with factorial operator

From
Bruce Momjian
Date:
Jean-Luc Lachance wrote:
> Hi all,
>
> The PG Documentation states:
>
>
> tgl=> select cast(text '44' as int8) ! as "factorial";
>       factorial
> ---------------------
>  2673996885588443136
> (1 row)
>
>
> If you try it, you will get the same result, but it is the wrong answer.
> Factorial work up to 20. After that one gets negative results.

I changed the docs to do a 20!.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
Index: doc/src/sgml/typeconv.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/typeconv.sgml,v
retrieving revision 1.20
diff -c -r1.20 typeconv.sgml
*** doc/src/sgml/typeconv.sgml    20 Jan 2002 22:19:56 -0000    1.20
--- doc/src/sgml/typeconv.sgml    18 Apr 2002 03:35:58 -0000
***************
*** 435,441 ****
  is defined only for integer data types, not for float8.  So, if we
  try a similar case with <literal>!</>, we get:
  <screen>
! tgl=> select text '44' ! as "factorial";
  ERROR:  Unable to identify a postfix operator '!' for type 'text'
          You may need to add parentheses or an explicit cast
  </screen>
--- 435,441 ----
  is defined only for integer data types, not for float8.  So, if we
  try a similar case with <literal>!</>, we get:
  <screen>
! tgl=> select text '20' ! as "factorial";
  ERROR:  Unable to identify a postfix operator '!' for type 'text'
          You may need to add parentheses or an explicit cast
  </screen>
***************
*** 443,452 ****
  possible <literal>!</> operators should be preferred.  We can help
  it out with an explicit cast:
  <screen>
! tgl=> select cast(text '44' as int8) ! as "factorial";
        factorial
  ---------------------
!  2673996885588443136
  (1 row)
  </screen>
  </para>
--- 443,452 ----
  possible <literal>!</> operators should be preferred.  We can help
  it out with an explicit cast:
  <screen>
! tgl=> select cast(text '20' as int8) ! as "factorial";
        factorial
  ---------------------
!  2432902008176640000
  (1 row)
  </screen>
  </para>
Index: src/interfaces/python/pgdb.py
===================================================================
RCS file: /cvsroot/pgsql/src/interfaces/python/pgdb.py,v
retrieving revision 1.10
diff -c -r1.10 pgdb.py
*** src/interfaces/python/pgdb.py    19 Mar 2002 02:47:57 -0000    1.10
--- src/interfaces/python/pgdb.py    18 Apr 2002 03:36:04 -0000
***************
*** 337,343 ****
  ### module interface

  # connects to a database
! def connect(dsn = None, user = None, password = None, host = None, database = None):
      # first get params from DSN
      dbport = -1
      dbhost = ""
--- 337,343 ----
  ### module interface

  # connects to a database
! def connect(dsn = None, user = None, password = None, xhost = None, database = None):
      # first get params from DSN
      dbport = -1
      dbhost = ""
***************
*** 364,372 ****
          dbpasswd = password
      if database != None:
          dbbase = database
!     if host != None:
          try:
!             params = string.split(host, ":")
              dbhost = params[0]
              dbport = int(params[1])
          except:
--- 364,372 ----
          dbpasswd = password
      if database != None:
          dbbase = database
!     if xhost != None:
          try:
!             params = string.split(xhost, ":")
              dbhost = params[0]
              dbport = int(params[1])
          except:

Re: Problem with factorial operator

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
>> Factorial work up to 20. After that one gets negative results.

> I changed the docs to do a 20!.

Don't we have a TODO item to replace the existing factorial functions
with a NUMERIC implementation?

            regards, tom lane

Re: Problem with factorial operator

From
Bruce Momjian
Date:
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> >> Factorial work up to 20. After that one gets negative results.
>
> > I changed the docs to do a 20!.
>
> Don't we have a TODO item to replace the existing factorial functions
> with a NUMERIC implementation?

We do, but until then, we should document a factorial that actually
returns a value in the int8 range.  ;-)

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026