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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:19:57+00:00 2026-05-30T11:19:57+00:00

as the title already says, I need to convert an int[] to a ByteBuffer

  • 0

as the title already says, I need to convert an int[] to a ByteBuffer in Java. Is there a recommended way to do this ?

I want to pass the ByteBuffer over JNI to C++. What do I have to look out for regarding any specific endian conversions in this case ?

Edit: Sorry, I mistakenly wrote ByteArray but meant the type ByteBuffer.

Edit: Sample code:

I stripped out the unnecessary parts. I call a Java function over JNI from c++ to load a resource and pass it back to c++ as bytebuffer. It works with various other resources. Now I have an “int []” and would like to know if there is an elegant way to convert it to a bytebuffer or if I have to go the oldfashioned way and fill it in a for loop.

ByteBuffer  resource= null;
resource = ByteBuffer.allocateDirect((x*y+2)*4).order(ByteOrder.nativeOrder());
.
.
ByteBuffer GetResourcePNG(String text)
{
    .
    .
    int []  pix;
    map.getPixels(pix,0,x,0,0,x,y);

    return resource;
}
  • 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-30T11:19:59+00:00Added an answer on May 30, 2026 at 11:19 am

    You have to use ByteBuffer.allocateDirect if you want to be able to use JNI’s GetDirectBufferAddress.

    Use ByteBuffer.order(ByteOrder.nativeOrder()) to adjust the ByteBuffer instance’s endianness to match the current platform.

    After the ByteBuffer‘s endianness is properly configured, use ByteBuffer.asIntBuffer() to get a view of it as a java.nio.IntBuffer and fill it with your data.

    Full Example:

    import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer;
    
    public class Test {
        static final int bytes_per_datum = 4;
    
        public static void main(String args[]) {
            main2("Native Endian", ByteOrder.nativeOrder());
            main2("Big Endian", ByteOrder.BIG_ENDIAN);
            main2("Little Endian", ByteOrder.LITTLE_ENDIAN);
        }
    
        static void main2(String comment, ByteOrder endian) {
            int[] data = { 1, 0xF, 0xFF, 0xFFF, 0xFFFF, 0xFFFFF, 0xFFFFFF, 0xFFFFFFF, 0xFFFFFFFF };
            ByteBuffer bb = ByteBuffer.allocateDirect(data.length * bytes_per_datum);
            bb.order(endian); // endian must be set before putting ints into the buffer
            put_ints(bb, data);
    
            System.out.println(comment + ": ");
            print(bb);
        }
    
        static void put_ints(ByteBuffer bb, int[] data) {
            IntBuffer b = bb.asIntBuffer(); // created IntBuffer starts only from the ByteBuffer's relative position
                                            // if you plan to reuse this IntBuffer, be mindful of its position
            b.put(data); // position of this IntBuffer changes by +data.length;
        } // this IntBuffer goes out of scope
    
        static void print(ByteBuffer bb) { // prints from start to limit
            ByteBuffer bb_2 = bb.duplicate(); // shares backing content, but has its own capacity/limit/position/mark (equivalent to original buffer at initialization)
            bb_2.rewind();
            for (int x = 0, xx = bb_2.limit(); x < xx; ++x) {
                System.out.print((bb_2.get() & 0xFF) + " "); // 0xFF for display, since java bytes are signed
                if ((x + 1) % bytes_per_datum == 0) {
                    System.out.print(System.lineSeparator());
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says, is there another way to pass a variable from "current"
As the title says, I want to parse some Java source code in Java.
As the title already says I need C/C++ sourcecode or a library that I
Not too sure how to elaborate on this over what the title already states,
Title says it mostly. I want to add a simple extension method to the
As the title already says, I'd like to know how to create a 1024x1024
As the title already says, I would like to know if it is somehow
As title says, mock code to demostrate my problem Driver Class import java.io.*; public
As the title says i want to call a mixed mode assembly from unmanaged
I believe the title already says it all, but here is my question: what

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.