Results 1 to 4 of 4
  1. #1
    floating is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default C buffer to Java String

    Hello,

    due to compatibility reasons, I need to be able to store a random C buffer inside of a Java String. THis means that the Java String should contain the exact same buffer information (i.e. byte sequence) as the original C buffer. How would I do that?

    All the functions I found will always somehow code/decode the C buffer, and modify its content depending on the selected encoding.

    I need to doo this inside JNI. Following is what I have:

    Java Code:
        unsigned char* cBuffer=getCBuffer();
        
        // Transfer the C buffer to s:
        jstring s=NULL;
           if (env->EnsureLocalCapacity(2) >= 0)
        {
            jbyteArray bytes = env->NewByteArray(signalLength);
            if (bytes != NULL) 
            {
                env->SetByteArrayRegion(bytes, 0, signalLength,(jbyte *)cBuffer);
                jclass cls = env->FindClass("java/lang/String");
                jmethodID mid = env->GetMethodID(cls, "<init>", "([B)V");
                s = (jstring)env->NewObject(cls, mid, bytes);
                env->DeleteLocalRef(bytes);
            }
        }
    Thanks for any help

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: C buffer to Java String

    Who says you can? Java String objects are UTF-16 encoded characters, no exceptions. You'll have to use something else to store your data. A byte array probably.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: C buffer to Java String

    If you don't want any encoding/decoding done, create a char array (Java chars) and copy the bytes/chars (C chars) from the original array over to the new array; use the String(char[]) constructor from the String class to get your wanted String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    floating is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Re: C buffer to Java String

    Thank you for your replies!
    I switched to a byte array.

    Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 04-15-2014, 02:32 AM
  2. Java Help in storing a Buffer
    By anonb in forum New To Java
    Replies: 8
    Last Post: 10-25-2012, 11:31 PM
  3. Why we use String compare to String Buffer
    By Sharath_Forums in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 07:21 AM
  4. Non Allocating String Buffer
    By chrisdb89 in forum New To Java
    Replies: 5
    Last Post: 10-25-2008, 07:57 AM
  5. Help with String Buffer
    By mathias in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •