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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:43:24+00:00 2026-05-24T15:43:24+00:00

I have a byte[] in Java which reports its length as 256 bytes which

  • 0

I have a byte[] in Java which reports its length as 256 bytes which I pass to a native function in C.

When I tried to get the data out of this array it was completely wrong and when I printed it out it did not match the data I printed out right before I passed it to C.

I tried a few ways to access the data including both GetByteArrayRegion and GetByteArrayElements but nothing seems to give me the data I expect.

As I was investigating this I tried to look at what JNI believed the jbyteArray‘s length was with GetArrayLength – it reported the length as 1079142960, far more than the 256 bytes I expected. Also the value was different each time the function was called, for example another time GetArrayLength returned 1079145720.

Here is the code I am using to access the array:

JNIEXPORT jbyteArray function(JNIEnv* env, jbyteArray array) {
    int length = (*env)->GetArrayLength(env, array);

    jbyte data[256];

    (*env)->GetByteArrayRegion(env, array, 0, 256, data);
    //also tried
    //jbyte *data = (jbyte*) (*env)->GetByteArrayElements(env, array, NULL);
}

This seems pretty straight forward so I’m not really sure what is going on. The array seems fine from Java but it was generated in C and passed back so I suppose something might have gone wrong there that Java doesn’t care about but breaks the array when it comes back to C.

Here is the code I used to generate the array and pass it back to Java:

//there is some openSSL stuff here that sets up a pointer to an RSA struct called keys that is size bytes large

jbyteArray result = (*env)->NewByteArray(env, size);

(*env)->SetByteArrayRegion(env, result, 0, size, (jbyte*)keys;

Am I missing something?

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-05-24T15:43:25+00:00Added an answer on May 24, 2026 at 3:43 pm

    This function prototype is incorrect:

    JNIEXPORT jbyteArray function(JNIEnv* env, jbyteArray array)
    

    The second argument is either a jclass or a jobject. If your method is static, it should be:

    JNIEXPORT jbyteArray function(JNIEnv* env, jclass cls, jbyteArray array)
    

    And if it’s not static:

    JNIEXPORT jbyteArray function(JNIEnv* env, jobject obj, jbyteArray array)
    

    You are treating the class or object as an array, which explains the unexpected results that you get.

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

Sidebar

Related Questions

I have this method in my java code which returns byte array for given
I have a Java class, where I'm reading data in via an InputStream byte[]
I have a byte[] array, the contents of which represent a TIFF file (as
I have a string containing byte data. How can I perform an in-place conversion
I have a byte array which i want to copy/clone to avoid calling code
i am making a app which takes photo on button click i have camera.java
I have a Java method which starts up a Process with ProcessBuilder, and pipes
If you have an array of Java objects which have a primitive type (for
I have an image in the form of a byte array in java. I'm
Every Java circular byte buffer implementation I have seen referenced on SO and elsewhere

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.