Re: [GENERAL] converting from bytea to integers

2009-04-20 Thread John DeSoi


On Apr 20, 2009, at 5:23 PM, Daniel Verite wrote:


get_byte()?

mailtest=> \set e '\'\12\15\107\20\'::bytea'

mailtest=> select get_byte(:e,0),get_byte(:e,1),get_byte(:e, 
2),get_byte(:e,3);
get_byte | get_byte | get_byte | get_byte --+-- 
+--+--

 10 | 13 |   71 |   16


That's what I ended up with. My first attempts at it were unsuccessful  
because I did not notice that get_byte uses zero indexing. Earlier in  
the routine I extracted bytes using substring and just assumed they  
used the same indexing. They don't. It might be worthy of a  
documentation note -- it seems easy to miss if you have not used the  
binary functions before.


I generated the integer from the bytes using something like this:

b1 = get_byte(p_array, i+3);
b2 = get_byte(p_array, i+2);
b3 = get_byte(p_array, i+1);
b4 = get_byte(p_array, i);
val = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;


Thanks,


John DeSoi, Ph.D.


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] converting from bytea to integers

2009-04-20 Thread Daniel Verite

John DeSoi wrote:

I'd like to convert some bytea data to an array of four byte integers 


(and vice versa). I'm probably missing something obvious, but I don't 


see an efficient way to generate a 4 byte integer from a bytea string 



(could be big endian or little endian).


get_byte()?

mailtest=> \set e '\'\12\15\107\20\'::bytea'

mailtest=> select 
get_byte(:e,0),get_byte(:e,1),get_byte(:e,2),get_byte(:e,3);
get_byte | get_byte | get_byte | get_byte 
--+--+--+--

  10 |13 |   71 |   16
(1 row)

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage:
http://www.manitou-mail.org

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[GENERAL] converting from bytea to integers

2009-04-20 Thread John DeSoi
I'd like to convert some bytea data to an array of four byte integers  
(and vice versa). I'm probably missing something obvious, but I don't  
see an efficient way to generate a 4 byte integer from a bytea string  
(could be big endian or little endian).  Converting back to bytea  
seems easy enough using to_hex.


Thanks for any suggestions,



John DeSoi, Ph.D.





--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general