[Haskell-cafe] bytea decoding

Bryan Green dbryan.green at gmail.com
Sun Jan 20 07:29:21 EST 2008


Does anyone know of a library that will handle bytea encodings from
postgres?  The bytea field that I need to access contains a jpg file.  I
want to retrieve it from the database and write it out for an image display
program.

bytea:

Bytea octets are also escaped in the output. In general, each
"non-printable" octet is converted into its equivalent three-digit octal
value and preceded by one backslash. Most "printable" octets are represented
by their standard representation in the client character set. The octet with
decimal value 92 (backslash) has a special alternative output
representation. Details are in Table
8-8<http://www.postgresql.org/docs/7.4/interactive/datatype-binary.html#DATATYPE-BINARY-RESESC>.


*Table 8-8. bytea Output Escaped Octets*
Decimal Octet ValueDescriptionEscaped Output Representation ExampleOutput
Result92backslash\\SELECT '\\134'::bytea;\\ 0 to 31 and 127 to 255
"non-printable" octets\*xxx* (octal value) SELECT '\\001'::bytea;\00132 to
126"printable" octetsclient character set representation SELECT
'\\176'::bytea;~

Depending on the front end to PostgreSQL you use, you may have additional
work to do in terms of escaping and unescaping bytea strings. For example,
you may also have to escape line feeds and carriage returns if your
interface automatically translates these.

So, here is part of the file from the database:

\377\330\377\340\000\020JFIF\000\001\001\000\000\001\000\001\000\000\377\333\000C\000\012\007\007\010\007\006\012\010\010\010\013\012\012\013\016\030\020\016\015\015\016\035\025\026\021\030#\037%$"\037"!&+7/&)4)!"0A149;>>>%.DIC<H7=>;\377


I may have to write a converter myself but I don't like re-inventing the
wheel if I don't need to do so.  Any pointers would be greatly appreciated.

Bryan Green
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080120/dd8135a8/attachment.htm


More information about the Haskell-Cafe mailing list