Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7809561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:28:43+00:00 2026-06-02T03:28:43+00:00

I am trying to understand how to work with binary data in postgresql (v

  • 0

I am trying to understand how to work with binary data in postgresql (v 8.3).
Let’s say I have a following table

Table "public.message"
Column  |  Type   | Modifiers 
---------+---------+-----------
id      | integer | 
message | bytea   | 

I would like to store a packet in the message field in this format:

version (1 byte), identifier (1 byte), epoch (4 bytes)

I would like to pack this data into the message field. Lets say I have version=1, identifier=8 and epoch=123456. How would I pack this data into the message field? How would I convert my integer values to hex.. or octal?

I also need to get the message back and parse it. I was looking at the get_byte function, unless there is another way to parse the data out..

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:28:45+00:00Added an answer on June 2, 2026 at 3:28 am

    So I was able to figure out how to do it in plpg
    Here’s the code to pack

    CREATE FUNCTION pack_numeric_bytes(i_values NUMERIC[], i_byte_sizes NUMERIC[],    i_big_endian BOOLEAN)
    RETURNS BYTEA
        DECLARE
            v_bytes BYTEA := NULL;
            v_start INTEGER := 1;
            v_byte BYTEA;
            v_byte_size INTEGER;
            v_value NUMERIC;
            v_binary_value TEXT;
            v_num NUMERIC;
            i INTEGER;
            x INTEGER;
            v_sql TEXT;
        BEGIN
            IF array_upper(i_values, 1) != array_upper(i_byte_sizes, 1) THEN
                RETURN v_bytes;
            END IF;
    
            FOR x IN array_lower(i_values, 1) .. array_upper(i_values, 1) LOOP
    
                /* Given value and size at x position */
                v_byte_size := i_byte_sizes[x]::INTEGER;
                v_value := i_values[x];
                /* Convert number to binary form */
                v_sql := $$SELECT $$|| v_value ||$$::bit($$|| v_byte_size*8 ||$$);$$;
                EXECUTE v_sql INTO v_binary_value;
                IF i_big_endian IS TRUE THEN
                    /* Convert each byte at a time */
                    FOR i IN 1 .. v_byte_size LOOP
                        /* Extract byte from our binary value. 
                        Big endian starts at 1 and then increments of 8 */
                        v_byte := substring(v_binary_value, v_start, 8);
                        /* Convert binary 8 bits to an integer */
                        v_sql := $$SELECT B$$||quote_literal(v_byte)||$$::int8$$;
                        EXECUTE v_sql INTO v_num;
                        /* Build bytea of bytes */
                        v_bytes := COALESCE(v_bytes, '') || set_byte(E' '::BYTEA, 0, v_num::INTEGER);
                        v_start := v_start + 8;
    
                    END LOOP;
                ELSE
                    /* Small endian is extracted starting from last byte */
                    v_start := (v_byte_size * 8) + 1;
                    /* Convert each byte at a time */
                    FOR i IN 1 .. v_byte_size LOOP
                        v_start := v_start - 8;
                        v_byte := substring(v_binary_value, v_start, 8);
                        /* Convert binary 8 bits to an integer */
                        v_sql := $$SELECT B$$||quote_literal(v_byte)||$$::int8$$;
                        EXECUTE v_sql INTO v_num;
                        /* Build bytea of bytes */
                        v_bytes := COALESCE(v_bytes, '') || set_byte(E' '::BYTEA, 0, v_num::INTEGER);
                    END LOOP;
    
                END IF; /* END endian check */
    
                v_start := 1;
    
            END LOOP;
            RETURN v_bytes;
        END;
    

    And here’s the code to unpack:

    CREATE OR REPLACE FUNCTION public.unpack_numeric_bytes(i_bytes bytea, i_byte_sizes INTEGER[], i_big_endian BOOLEAN)
    RETURNS NUMERIC[]
    SECURITY DEFINER AS
        DECLARE
            v_bytes BYTEA;
            v_start INTEGER := 1;
            v_byte_index INTEGER := 0;
            v_bit_shift INTEGER := 0;
    
            v_length INTEGER;
            v_size INTEGER;
            v_sum_byte_sizes INTEGER;
    
            v_vals NUMERIC[] := '{}';
            v_val BIGINT := 0;
    
            i INTEGER;
            x INTEGER;
            v_sql TEXT;
        BEGIN
           v_sql := $$SELECT $$|| array_to_string(i_byte_sizes, '+')||$$;$$;
    
            EXECUTE v_sql INTO v_sum_byte_sizes;
    
            IF length(i_bytes) != v_sum_byte_sizes::INTEGER THEN
                RETURN v_vals;
            END IF;
    
            /* Loop through values of bytea (split by their sizes) */
            FOR x IN array_lower(i_byte_sizes, 1) .. array_upper(i_byte_sizes, 1) LOOP
    
                v_size := i_byte_sizes[x];
                v_bytes := substring(i_bytes, v_start, v_size);
                v_length := length(v_bytes);
    
                IF i_big_endian IS TRUE THEN
    
                    v_byte_index := v_length - 1;
    
                    FOR i IN 1..v_length LOOP
                        v_val := v_val + (get_byte(v_bytes, v_byte_index) << v_bit_shift);
                        v_bit_shift := v_bit_shift + 8;
                        v_byte_index := v_byte_index - 1;
                    END LOOP;
                ELSE
    
                    FOR i IN 1..v_length LOOP
                        v_val := v_val + (get_byte(v_bytes, v_byte_index) << v_bit_shift);
                        v_bit_shift := v_bit_shift + 8;
                        v_byte_index := v_byte_index + 1;
                    END LOOP;
    
                END IF;
    
                v_vals := array_append(v_vals, v_val::NUMERIC);
                /* Calculate next value start index */
                v_start := v_start + v_size;
                v_byte_index := 0;
                v_bit_shift := 0;
                v_val := 0;
    
            END LOOP;
    
            RETURN v_vals;
        END;
    

    I hope this will help someone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how the Google Test Fixtures work. Say I have the
I'm trying to understand how locks work. Let's say I want to implement a
trying to understand how custom admin commands work, I have my project named mailing
I am trying to understand how Mach-o files work, and have made a good
I'm trying to understand better how Windows sessions work, so if I have some
I am trying to understand how colors work in Android. I have this color
I'm trying to understand how events work in Backbone. The following seems to me
I'm trying to understand how view animations work in iOS; i currently have an
I'm trying to understand how parallelism might work using PLINQ, given delayed execution. Here
I'm trying to understand how ID3 tags work, so, after reading some documentation, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.