Re: Storing/sorting hex data in PostgreSQL

From: nha <lyondif02(at)free(dot)fr>
To: JJ <jayson(dot)prichard(at)comsouth(dot)net>
Cc: PgSQL-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Storing/sorting hex data in PostgreSQL
Date: 2009-07-21 11:14:36
Message-ID: 4A65A31C.7090604@free.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

On 20/07/09 21:59, JJ wrote :
> [...]
> I want to sort and query the database based on the
> hex addresses [...]
> How can I store hex values in PostgreSQL and sort and query them?
>

PostgreSQL (since version 8.3 and maybe older) sets string functions for
encoding/decoding strings from/to their hexadecimal representation. The
following page presents these functions:
http://www.postgresql.org/docs/8.3/interactive/functions-string.html

For example:

# SELECT decode('6d61702064617461', 'hex');
results in string: map data
# SELECT encode('map data', 'hex');
results in hexadecimal representation of "map data": 6d61702064617461
(ie. each character [each byte in fact] is replaced by its hexadecimal
representation in ASCII range [space " " is replaced by "20" (hex.
value) or "32" (decimal value)])

A (simplier?) alternative would be to convert hexadecimal value into
text with the natural class operator:
# SELECT '6d61'::text;
results in string (text type): 6d61

At this time, I do not know about any PostgreSQL function that converts
from/to hex. to/from integer. Such functions may be user-created. The
sorting speed may then depend on the meaning of integer values (numeric
relation relevance) with your data.

Regards.
--
nha / Lyon / France.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rob Sargent 2009-07-21 16:27:38 Order of operations in ascii vs archive format (moderately urgent)
Previous Message nha 2009-07-21 00:20:07 Re: unused columns in copy