Re: Storing/sorting hex data in PostgreSQL - Mailing list pgsql-sql

From nha
Subject Re: Storing/sorting hex data in PostgreSQL
Date
Msg-id 4A65A31C.7090604@free.fr
Whole thread Raw
In response to Storing/sorting hex data in PostgreSQL  (JJ <jayson.prichard@comsouth.net>)
List 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.


pgsql-sql by date:

Previous
From: JJ
Date:
Subject: Storing/sorting hex data in PostgreSQL
Next
From: Rob Sargent
Date:
Subject: Order of operations in ascii vs archive format (moderately urgent)