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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: Store a random C buffer inside a Java String

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

    Default Store a random C buffer inside a 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:

    	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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 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.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 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?
    Unfortunately, I don't know what environment your coding in, Linux, Windows etc. But I'll assume Windows. First of all, strings are different in C and Java. In Java they are sequences of UTF-16 code points whereas C strings are null terminated sequence of bytes. I believe JNI has two the functions for manipulating these strings. One is "modified UTF-8" byte sequences and the other is arrays of UTF-16 values.

    Windows has two types of strings ASCII and Unicode. If your C code is using ASCII strings then use the "modified UTF-8 conversion functions. Otherwise, if you are using Unicode strings then UTF-16 conversion functions are what you need.

    Finally, I can't be of much help to you since my Java experience is minimal. But you do have to convert the strings as indicated above.

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, 02: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