public inbox for [email protected]  
help / color / mirror / Atom feed
From: Steve Cooper <[email protected]>
To: Wasim Devale <[email protected]>
Cc: pgsql-admin <[email protected]>
Cc: Pgsql-admin <[email protected]>
Subject: Re: Bytea datatype content to view
Date: Wed, 6 Nov 2024 06:09:09 -0800
Message-ID: <CAKbF40=P0h+H6_1vGhzGWxA6gX-sKkgTRfL1W8NsQ+=gAa7iqg@mail.gmail.com> (raw)
In-Reply-To: <CAB5fag593g9wK+hDdWDqRDUnd-N1XLAFNaGKv37F95LngkvdgQ@mail.gmail.com>
References: <CAB5fag593g9wK+hDdWDqRDUnd-N1XLAFNaGKv37F95LngkvdgQ@mail.gmail.com>

A PostgreSQL bytea type is the hexadecimal representation of the ASCII
codes for a character string.

So, the string 'hex' translates to \x686578 in a bytea field.
\x68 = decimal 104 = 'h'; \x65 = decimal 101 = 'e'; \x78 = decimal 120 = 'x'

Here's a short Python function to translate hex ascii to a character string:

def hexToStr(hexString: str) -> str:
    # Convert hex string to bytes
    bytesObject: bytes = bytes.fromhex(hexString)
    # Decode bytes to string
    asciiString: str = bytesObject.decode("ASCII")
    return asciiString

if __name__ == "__main__":
  hex_string = "686578"  # hexadecimal representation of the ASCII codes
for the string 'hex'
  print(hexToStr(hex_string))

# output: hex

Rewrite the above "__main__" part to open a database, select whatever, loop
through the results, translate the field(s) in question, and perform
whatever output you like.



On Wed, Nov 6, 2024 at 5:05 AM Wasim Devale <[email protected]> wrote:

> Hi All
>
> I have a table having bytea datatype. Anyone has worked on it how we can
> see the content as the files are in .csv, .pdf, .bin and .docx format.
>
> Can anyone help with this? Any SQL script or python script to view the
> content?
>
> Thanks,
> Wasim
>


view thread (5+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Bytea datatype content to view
  In-Reply-To: <CAKbF40=P0h+H6_1vGhzGWxA6gX-sKkgTRfL1W8NsQ+=gAa7iqg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox