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 631221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:56:29+00:00 2026-05-13T19:56:29+00:00

I need to be able to convert byte arrays to/from other primitive type arrays,

  • 0

I need to be able to convert byte arrays to/from other primitive type arrays, but instead of casting, I need type punning. Correct term for raw copy without casting?

I thought it would be possible to do the following:

// idea: byte[12] -> int[3], and int[3] -> byte[12]

int[] ints;

ByteBuffer bb = ByteBuffer.wrap(
    new byte[]{ 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3 });
IntBuffer ib = bb.asIntBuffer();

ints = ib.array(); // java.lang.UnsupportedOperationException
ints = ib.duplicate().array(); // java.lang.UnsupportedOperationException

Unfortunately, it seems that bb.asIntBuffer() is not creating a new IntBuffer by copying the content “bitwise” or “raw”, but creates a new “view” on existing ByteBuffer. That’s why .array() is intended to fail.

I browsed around in JDK’s sources, and found few classes, which are used by all these buffer classes and would do the stuff I need, but are internal (such as the class Unsafe).

While I think that my goal could be achieved by wrapping the byte buffer in some ObjectInputStream and read the primitive values by .readInt(), I think it would be a messy and slow workaround.

So, are there any other solutions possible without doing magical primitive type arithmetics (shifting, checking endians, …)?

NOTE: I need both directions: byte[12] -> int[3], and int[3] -> byte[12]

  • 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-13T19:56:29+00:00Added an answer on May 13, 2026 at 7:56 pm

    According to the javadoc, array() [1] returns the buffer’s backing array which is the array you specify with the call to wrap() [2].

    Hence, you must create a new array with the desired type. But the arithmetics can still be handled via the Buffer classes.

    ByteBuffer bb = ByteBuffer.wrap(new byte[]{ 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3 });
    IntBuffer ib = bb.asIntBuffer();
    
    int[] intArray = new int[ib.limit()];
    ib.get(intArray);
    

    Backwards requires a little bit of calculation by yourself.

    ByteBuffer newBb = ByteBuffer.allocate(intArray.length*4);
    newBb.asIntBuffer().put(intArray);
    byte[] byteArray = newBb.array();
    

    See:

    [1] http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html#array%28%29

    [2] http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html#wrap%28byte%5B%5D%29

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

Sidebar

Related Questions

I need to be able to convert from a Delphi Real48 to C# double.
What is the correct way to convert my class to/from a byte array? I
I need to be able to convert dynamic HTML (html that is rendered on
I need to be able to convert a PDF file to images (one image
I need to be able to quickly convert an image (inside a rails controller)
I need to be able to take EPS and PDF's and convert them to
I need to be able to convert the below CSV into objects. Is that
I need to be able to convert RTF or HTML to Markdown-syntaxed plain text
I need to be able to convert time on an time object I recieve
In my Java Program, I need a byte array from a Hex String. So

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.