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

  • SEARCH
  • Home
  • 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 252973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:43:22+00:00 2026-05-11T21:43:22+00:00

I’m trying to send a Java UUID to C++, where it will be used

  • 0

I’m trying to send a Java UUID to C++, where it will be used as a GUID, then send it back and see it as a UUID, and I’m hoping to send it across as just 16 bytes.

Any suggestions on an easy way to do this?

I’ve got a complicated way of doing it, sending from Java to C++, where I ask the UUID for its least and most significant bits, write this into a ByteBuffer, and then read it out as bytes.

Here is my silly-complicated way of getting 2 longs out of a UUID, sending them to C++:

Java

public static byte[] asByteArray(UUID uuid) 
 {
    long msb = uuid.getMostSignificantBits();
    long lsb = uuid.getLeastSignificantBits();
    byte[] buffer = new byte[16];

    for (int i = 0; i < 8; i++) {
            buffer[i] = (byte) (msb >>> 8 * (7 - i));
    }
    for (int i = 8; i < 16; i++) {
            buffer[i] = (byte) (lsb >>> 8 * (7 - i));
    }

    return buffer;

}




    byte[] bytesOriginal = asByteArray(uuid);
    byte[] bytes = new byte[16];

    // Reverse the first 4 bytes
    bytes[0] = bytesOriginal[3];
    bytes[1] = bytesOriginal[2];
    bytes[2] = bytesOriginal[1];
    bytes[3] = bytesOriginal[0];
    // Reverse 6th and 7th
    bytes[4] = bytesOriginal[5];
    bytes[5] = bytesOriginal[4];
    // Reverse 8th and 9th
    bytes[6] = bytesOriginal[7];
    bytes[7] = bytesOriginal[6];                                 
    // Copy the rest straight up        
    for ( int i = 8; i < 16; i++ )
    {
        bytes[i] = bytesOriginal[i];
    }    

    // Use a ByteBuffer to switch our ENDIAN-ness
    java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(16);
    buffer.order(java.nio.ByteOrder.BIG_ENDIAN);
    buffer.put(bytes);
    buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN);
    buffer.position(0);

    UUIDComponents x = new UUIDComponents();

    x.id1 = buffer.getLong();
    x.id2 = buffer.getLong();

C++

    google::protobuf::int64 id1 = id.id1();
    google::protobuf::int64 id2 = id.id2();

    char* pGuid = (char*) &guid;
    char* pGuidLast8Bytes = pGuid + 8;
    memcpy(pGuid, &id1, 8);
    memcpy(pGuidLast8Bytes, &id2, 8);

This works, but seems way too complex, and I can’t yet get it working in the other direction.

(I’m using google protocol buffers to send the two longs back and forth)

  • Alex
  • 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-05-11T21:43:22+00:00Added an answer on May 11, 2026 at 9:43 pm

    I got something working.

    Instead of sending it across as two longs, I send it across as bytes, here is the Java code:

    public static UUID fromBytes( ByteString byteString)
    {
        byte[] bytesOriginal = byteString.toByteArray();
        byte[] bytes = new byte[16];
    
        // Reverse the first 4 bytes
        bytes[0] = bytesOriginal[3];
        bytes[1] = bytesOriginal[2];
        bytes[2] = bytesOriginal[1];
        bytes[3] = bytesOriginal[0];
        // Reverse 6th and 7th
        bytes[4] = bytesOriginal[5];
        bytes[5] = bytesOriginal[4];
        // Reverse 8th and 9th
        bytes[6] = bytesOriginal[7];
        bytes[7] = bytesOriginal[6];                                 
        // Copy the rest straight up        
        for ( int i = 8; i < 16; i++ )
        {
            bytes[i] = bytesOriginal[i];
        }    
    
        return toUUID(bytes);
    }
    
    public static ByteString toBytes( UUID uuid )
    {
        byte[] bytesOriginal = asByteArray(uuid);
        byte[] bytes = new byte[16];
    
        // Reverse the first 4 bytes
        bytes[0] = bytesOriginal[3];
        bytes[1] = bytesOriginal[2];
        bytes[2] = bytesOriginal[1];
        bytes[3] = bytesOriginal[0];
        // Reverse 6th and 7th
        bytes[4] = bytesOriginal[5];
        bytes[5] = bytesOriginal[4];
        // Reverse 8th and 9th
        bytes[6] = bytesOriginal[7];
        bytes[7] = bytesOriginal[6];                                 
        // Copy the rest straight up        
        for ( int i = 8; i < 16; i++ )
        {
            bytes[i] = bytesOriginal[i];
        }    
    
        return ByteString.copyFrom(bytes);
    }
    
    
    private static byte[] asByteArray(UUID uuid) 
     {
        long msb = uuid.getMostSignificantBits();
        long lsb = uuid.getLeastSignificantBits();
        byte[] buffer = new byte[16];
    
        for (int i = 0; i < 8; i++) {
                buffer[i] = (byte) (msb >>> 8 * (7 - i));
        }
        for (int i = 8; i < 16; i++) {
                buffer[i] = (byte) (lsb >>> 8 * (7 - i));
        }
    
        return buffer;
    
    }
    
    private static UUID toUUID(byte[] byteArray) {
    
        long msb = 0;
        long lsb = 0;
        for (int i = 0; i < 8; i++)
                msb = (msb << 8) | (byteArray[i] & 0xff);
        for (int i = 8; i < 16; i++)
                lsb = (lsb << 8) | (byteArray[i] & 0xff);
        UUID result = new UUID(msb, lsb);
    
        return result;
    }
    

    Doing it this way, the bytes can be used straight up on the C++ side. I suppose the switching around of the order of the bytes could be done on either end.

    C++

        memcpy(&guid, data, 16);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The book is trying to explain why the first two… May 12, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer One way is like so: <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# (((String)DataBinder.Eval(Container.DataItem,… May 12, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer I wrote a "philosophy" of leading software development teams in… May 12, 2026 at 12:49 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.