Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t8gis-005RxL-SS for pgsql-admin@arkaria.postgresql.org; Wed, 06 Nov 2024 14:09:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t8giq-006QSW-01 for pgsql-admin@arkaria.postgresql.org; Wed, 06 Nov 2024 14:09:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t8gip-006QSM-JQ for pgsql-admin@lists.postgresql.org; Wed, 06 Nov 2024 14:09:24 +0000 Received: from mail-oa1-x35.google.com ([2001:4860:4864:20::35]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t8gim-000WL5-DH for pgsql-admin@postgresql.org; Wed, 06 Nov 2024 14:09:23 +0000 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-288d70788d6so3188315fac.1 for ; Wed, 06 Nov 2024 06:09:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730902159; x=1731506959; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=c6LecdiYb/gnUCHLjsTFEZjpSAyJPnOBqPsXpLK3TIE=; b=mXFJOoAYlo6MIHeEBf2Ls232byx0oJRrAn+VVQhzjXIcGW3JFjJUqBZRmCuRAV23Jt 8XhmrGcAaMm+pWJmdZ1j3/IVUg66w41CWsDeyEcDU2eKNPbS8pdqBZxK57lSOsEtnCXt mYswp0IzaCYYvEGTjF+OnlbAz6iUoXHWEqKciDcDLY6qG94VhgOEA/08Bp5Vvk+vD28i 0erweWtVELVaiMmGZ6jBD61MFqJ6V31CCp/yuCuhLNUJO85cZcK/Yhm6obXb2p9SKau/ MBZqYQVcpjheoAmpxIvabPdaw8e0eEbSoBGaZROY07nyXYEfldoNbJKEmZfNg3SzyLY2 q+Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730902159; x=1731506959; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=c6LecdiYb/gnUCHLjsTFEZjpSAyJPnOBqPsXpLK3TIE=; b=u2odlFhgL2+mw5CKsWoHfo7DQiohGgLzaMQ/XMpGb4+UOmr98Bw4mu7SzIjNdN75NZ yBW9sbZDa2McuRt6QUECuKQNd30e2xMMq2BDpHah8zXymRH4BQ9Tu3briKKHRRvn4bSj 2VPq75nRyM/9wKyKHWDOS/ayoxr32sQhhvUz7H+G+ES3GUdqHYl9otZqiFS4dXHO43U+ mY9FVEl2QSCPR5t2iHF8Phc0La9z1MSgqpQvRBkv4c8kHYMz3iNkOK0WvswePN1c8wQD xEGAYDaCUMeGsASaqk9mUQF6bkx8hkNr8yH1BjYMj7W0+vtXCVGz2fSZ2RUe9PDJtWQj 1dGQ== X-Gm-Message-State: AOJu0Yx6ukxnbmOgQscKiuzdmqaCAuoMng/c7uXeSVRvt7utP0oACHNV NlhUxS4gLyKDfycWhb6sg5ct7onRrmFWCfzJaDS55TDKPLicaVdnSofMQlV0YAJosxd6FeohGwi PyMj9qtKMbiLPnJHYLMfeih6SfiU= X-Google-Smtp-Source: AGHT+IHoAcwNpdgWlxdsQqUhfT+Xu4dmqnFojA5AV1+j5IinUy2wsIuvNSiefkObMsiHRP/4Vpp+GCGNL1ICh686MnM= X-Received: by 2002:a05:6870:6c0f:b0:288:e7f2:e9da with SMTP id 586e51a60fabf-29051bdaee1mr33098991fac.20.1730902159456; Wed, 06 Nov 2024 06:09:19 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Steve Cooper Date: Wed, 6 Nov 2024 06:09:09 -0800 Message-ID: Subject: Re: Bytea datatype content to view To: Wasim Devale Cc: pgsql-admin , Pgsql-admin Content-Type: multipart/alternative; boundary="0000000000005b003e06263f10f2" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000005b003e06263f10f2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D decimal 104 =3D 'h'; \x65 =3D decimal 101 =3D 'e'; \x78 =3D decima= l 120 =3D '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 =3D bytes.fromhex(hexString) # Decode bytes to string asciiString: str =3D bytesObject.decode("ASCII") return asciiString if __name__ =3D=3D "__main__": hex_string =3D "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=E2=80=AFAM Wasim Devale wr= ote: > 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 > --0000000000005b003e06263f10f2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
A PostgreSQL bytea type is the hexadecima= l representation of the ASCII codes for a character string.

<= div>So, the string 'hex' translates to=C2=A0\x686578 in a bytea fie= ld.
\x68 =3D decimal 104 =3D 'h'; \x65 =3D decimal 101 = =3D 'e'; \x78 =3D decimal 120 =3D 'x'

<= div>Here's a short Python function to translate hex ascii to a characte= r string:

def hexToStr(hexString: str) -> str:<= br>=C2=A0 =C2=A0 # Convert hex string to bytes
=C2=A0 =C2=A0 bytesObject= : bytes =3D bytes.fromhex(hexString)
=C2=A0 =C2=A0 # Decode bytes to str= ing
=C2=A0 =C2=A0 asciiString: str =3D bytesObject.decode("ASCII&qu= ot;)
=C2=A0 =C2=A0 return asciiString

if __name__ =3D=3D "__= main__":
=C2=A0 hex_string =3D "686578"=C2=A0 # hexadecim= al=C2=A0representation of the ASCII codes for the string 'hex'
= =C2=A0 print(hexToStr(hex_string))
=C2=A0 =C2=A0 =C2=A0
# output: he= x

Rewrite the above "__main__" part to o= pen a database, select whatever, loop through the results, translate the fi= eld(s) in question, and perform whatever output you like.



On Wed, Nov 6, 2024 at 5:05=E2=80=AFAM Wasim Devale <<= a href=3D"mailto:wasimd60@gmail.com">wasimd60@gmail.com> wrote:
<= /div>
Hi= All

I have a table having byt= ea datatype. Anyone has worked on it how we can see the content as the file= s are in .csv, .pdf, .bin and .docx format.

Can anyone help=C2=A0with this? Any SQL script or pytho= n script to view the content?

Thanks,
Wasim
--0000000000005b003e06263f10f2--