Home Explore Blog CI



postgresql

60th chunk of `doc/src/sgml/func.sgml`
20e55cc2c38cf46e737ce9c2190d12fe88c220c0f25e3c3b0000000100000fa7
 <varlistentry id="encode-format-base64">
     <term>base64
     <indexterm>
      <primary>base64 format</primary>
     </indexterm></term>
     <listitem>
      <para>
       The <literal>base64</literal> format is that
       of <ulink url="https://datatracker.ietf.org/doc/html/rfc2045#section-6.8">RFC
       2045 Section 6.8</ulink>.  As per the <acronym>RFC</acronym>, encoded lines are
       broken at 76 characters.  However instead of the MIME CRLF
       end-of-line marker, only a newline is used for end-of-line.
       The <function>decode</function> function ignores carriage-return,
       newline, space, and tab characters.  Otherwise, an error is
       raised when <function>decode</function> is supplied invalid
       base64 data &mdash; including when trailing padding is incorrect.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="encode-format-escape">
     <term>escape
     <indexterm>
      <primary>escape format</primary>
     </indexterm></term>
     <listitem>
      <para>
       The <literal>escape</literal> format converts zero bytes and
       bytes with the high bit set into octal escape sequences
       (<literal>\</literal><replaceable>nnn</replaceable>), and it doubles
       backslashes.  Other byte values are represented literally.
       The <function>decode</function> function will raise an error if a
       backslash is not followed by either a second backslash or three
       octal digits; it accepts other byte values unchanged.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="encode-format-hex">
     <term>hex
     <indexterm>
      <primary>hex format</primary>
     </indexterm></term>
     <listitem>
      <para>
       The <literal>hex</literal> format represents each 4 bits of
       data as one hexadecimal digit, <literal>0</literal>
       through <literal>f</literal>, writing the higher-order digit of
       each byte first.  The <function>encode</function> function outputs
       the <literal>a</literal>-<literal>f</literal> hex digits in lower
       case.  Because the smallest unit of data is 8 bits, there are
       always an even number of characters returned
       by <function>encode</function>.
       The <function>decode</function> function
       accepts the <literal>a</literal>-<literal>f</literal> characters in
       either upper or lower case.  An error is raised
       when <function>decode</function> is given invalid hex data
       &mdash; including when given an odd number of characters.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>

  <para>
   In addition, it is possible to cast integral values to and from type
   <type>bytea</type>. Casting an integer to <type>bytea</type> produces
   2, 4, or 8 bytes, depending on the width of the integer type. The result
   is the two's complement representation of the integer, with the most
   significant byte first. Some examples:
<programlisting>
1234::smallint::bytea          <lineannotation>\x04d2</lineannotation>
cast(1234 as bytea)            <lineannotation>\x000004d2</lineannotation>
cast(-1234 as bytea)           <lineannotation>\xfffffb2e</lineannotation>
'\x8000'::bytea::smallint      <lineannotation>-32768</lineannotation>
'\x8000'::bytea::integer       <lineannotation>32768</lineannotation>
</programlisting>
   Casting a <type>bytea</type> to an integer will raise an error if the
   length of the <type>bytea</type> exceeds the width of the integer type.
  </para>

  <para>
   See also the aggregate function <function>string_agg</function> in
   <xref linkend="functions-aggregate"/> and the large object functions
   in <xref linkend="lo-funcs"/>.
  </para>
 </sect1>


  <sect1 id="functions-bitstring">
   <title>Bit String Functions and Operators</title>

   <indexterm zone="functions-bitstring">
    <primary>bit strings</primary>
    <secondary>functions</secondary>
   </indexterm>

   <para>
    This section describes functions and operators

Title: Detailed Explanation of Encode/Decode Formats and Bytea Casting
Summary
This section delves into the specifics of various encoding formats supported by the `encode` and `decode` functions, namely `base64`, `escape`, and `hex`. It outlines the conversion rules and potential errors for each format. Furthermore, it explains how integral values can be cast to and from the `bytea` type, including the byte representation of integers and potential errors due to length constraints. It also cross-references aggregate functions and large object functions related to string manipulation.