Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Store a random C buffer inside a Java String

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Thanked 0 Times in 0 Posts

    Default Store a random C buffer inside a Java String


    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:

    	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);

    Thanks for any help

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 669 Times in 656 Posts

    Default Re: Store a random C buffer inside a Java String

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Requiring something to be or do something it simply cannot be or do is not a reasonable requirement. Instead, modify your requirement to decode/encode so that the buffer is represented appropriately for the situation.

Similar Threads

  1. stringbuilder and string buffer
    By kanupriya1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 3rd, 2014, 03:34 AM
  2. Reversing a String with no Buffer
    By C++kingKnowledge in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2013, 12:17 PM
  3. Java String Buffer
    By anonb in forum Object Oriented Programming
    Replies: 3
    Last Post: December 19th, 2012, 01:59 AM
  4. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM

Tags for this Thread