Re: Help with text(decimal) to hex conversion

From: Gavin Flower <GavinFlower(at)archidevsys(dot)co(dot)nz>
To: Wei Shan <weishan(dot)ang(at)gmail(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Help with text(decimal) to hex conversion
Date: 2016-03-21 20:52:13
Message-ID: 56F05EFD.4030102@archidevsys.co.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 21/03/16 21:30, Wei Shan wrote:
> Hi all,
>
> There's a column that has datatype of text. The conversion will work
> fine for some of the rows.
>
> /psql> select to_hex(data::bigint)from table limit 5;/
> / to_hex/
> /------------------/
> / 499602d2/
> / 499602d2/
> / 4fa83d1136d920ef/
> / 2e1b71785c8e11c/
> / 53ff4c2824860fb8/
>
>
> However, for some records, the original data is too large for casting.
>
> /ERROR: value "14481874327766585215" is out of range for type bigint/
>
> Any idea how to overcome this? to_hex function only accepts int or bigint.
>
> Thanks!
> --
> Regards,
> Ang Wei Shan
If feasible to use Java, then you could use the BigInteger class to
convert the Hex string into a number, see:

https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html

BigInteger bigInteger = new BigInteger(hexString, 16);

Cheers,
Gavin

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Szymon Lipiński 2016-03-21 21:05:12 Re: Help with text(decimal) to hex conversion
Previous Message Skylar Thompson 2016-03-21 15:48:50 Re: Upgrading to a major version