<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NewStringUTF | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/newstringutf/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 05:20:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>JNI Part 4: JNI Strings</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Mon, 22 Aug 2011 04:45:47 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[NewStringUTF]]></category>
		<category><![CDATA[JNI strings]]></category>
		<category><![CDATA[jni string example]]></category>
		<category><![CDATA[jni string funtions]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1682</guid>

					<description><![CDATA[<p>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *). We cannot use a jstring as a normal C string. We must use the appropriate JNI functions to convert jstring objects to C/C++ strings. The JNI supports conversion both to</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/">JNI Part 4: JNI Strings</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<ol>
<li>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *).</li>
<li>We cannot use a jstring as a normal C string.</li>
<li>We must use the appropriate JNI functions to convert jstring objects to C/C++ strings.</li>
<li>The JNI supports conversion both to and from Unicode and UTF-8 strings.</li>
<li>Unicode strings represent characters as 16-bit values, whereas UTF-8 strings use an encoding scheme that is upward compatible with 7-bit ASCII strings.</li>
<li>UTF-8 strings act like NULL-terminated C strings.</li>
</ol>
<p><strong>Some JNI String Functions</strong></p>
<ol>
<li>The GetStringUTFChars function is available through the JNIEnv interface pointer. It converts the jstring reference, typically represented by the Java virtual machine implementation as a Unicode sequence, into a C string represented in the UTF-8 format.</li>
<li>The ReleaseStringUTFChars frees the memory used for native string resources. Therefore, calling this function will free the memory taken by the UTF-8 string. Failure to call ReleaseStringUTFChars would result in a memory leak.</li>
<li>The NewStringUTF function constructs a new java.lang.String instance in the native method. The NewStringUTF function takes a C string with the UTF-8 format and constructs a java.lang.String instance. The newly constructed java.lang.String instance represents the same sequence of Unicode characters as the given UTF-8 C string.</li>
</ol>
<p>To convert a jstring to a C-style string, you might write code like the following:</p>
<pre lang="c" line="1" escaped="true">JNIEXPORT void JNICALLJava_MyJavaClass_printName(JNIEnv *env, jobject obj,
	jstring name)
{
	const char *str= (*env)-&gt;GetStringUTFChars(env,name,0);
	printf(“%s”, str);
	//need to release this string when done with it in
	//order to avoid memory leak
	(*env)-&gt;ReleaseStringUTFChars(env, name, str);
}</pre>
<p>To convert a C-style string to a jstring , you can use the (*env)-&gt;NewStringUTF() function to create a new jstring from a C-style string. For example, a C function that needs to return a Java string could contain the following code:</p>
<pre lang="c" line="1" escaped="true">JNIEXPORT jstring JNICALLJava_MyJavaClass_getName(JNIEnv *env, jobject obj)
{
	return (*env)-&gt;NewStringUTF(env, “My String”);
}</pre>
<p><strong>JNI String functions</strong>:</p>
<table class="tbl" border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="30%"><strong>JNI Function</strong></td>
<td width="70%"><strong>Description</strong></td>
</tr>
<tr>
<td>GetStringChars<br />
ReleaseStringChars</td>
<td>Obtains or releases a pointer to the contents of a string in Unicode format. May return a copy of the string.</td>
</tr>
<tr>
<td>GetStringUTFChars<br />
ReleaseStringUTFChars</td>
<td>Obtains or releases a pointer to the contents of a string in UTF-8 format.</td>
</tr>
<tr>
<td>GetStringLength</td>
<td>Returns the number of Unicode characters in the string.</td>
</tr>
<tr>
<td>GetStringUTFLength</td>
<td>Returns the number of bytes needed to represent a string in the UTF-8 format.</td>
</tr>
<tr>
<td>NewString</td>
<td>Creates a java.lang.String instance that contains the same sequence of characters as the given Unicode C string.</td>
</tr>
<tr>
<td>NewStringUTF</td>
<td>Creates a java.lang.String instance that contains the same sequence of characters as the given UTF-8 encoded C string.</td>
</tr>
<tr>
<td>GetStringCritical<br />
ReleaseStringCritical</td>
<td>Obtains a pointer to the contents of a string in Unicode format. May return a copy of the string. Native code must not block between a pair of Get/ReleaseStringCritical calls.</td>
</tr>
<tr>
<td>GetStringRegion<br />
SetStringRegion</td>
<td>Copies the contents of a string to or from a preallocated C buffer in the Unicode format.</td>
</tr>
<tr>
<td>GetStringUTFRegion<br />
SetStringUTFRegion</td>
<td>Copies the content of a string to or from a preallocated C buffer in the UTF-8 format.</td>
</tr>
</tbody>
</table>
<p><strong>JNI String Example:</strong></p>
<p>Let’s write a JNI application that passes a prompt message to a native method, which method prints the prompt message, reads the user input and sends it back to the application.</p>
<p>The Java code will be(NativePrompt.java):</p>
<pre lang="java" line="1" escaped="true">class NativePrompt {
	private native String getInput(String prompt);  //native method
	static   //static initializer code
	{
		System.loadLibrary("NativePrompt");
	} 

	public static void main(String[] args)
	{
		NativePrompt NP = new NativePrompt();
		String sName = NP.getInput("Enter your name: ");
        System.out.println("Hello " + sName);
	}
}</pre>
<p>Compile NativePrompt.java (javac NativePrompt.java) and generate hedder file(javah -jni NativePrompt).</p>
<p>The generated with javah header file NativePrompt.h will be:</p>
<pre lang="c" line="1" escaped="true">/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class NativePrompt */

#ifndef _Included_NativePrompt
#define _Included_NativePrompt
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif
</pre>
<p>The C++ implementation file NativePrompt.cpp will be:</p>
<pre lang="cpp" line="1" escaped="true">#include "NativePrompt.h" 
#include "jni.h"
#include "string"
#include "iostream"
#include "vector"

using namespace std;
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
	(JNIEnv *env, jobject obj, jstring prompt){
	
	string sEntry;
	const char *str;
	str = env-&gt;GetStringUTFChars(prompt, NULL);
	if (str == NULL) {
		return env-&gt;NewStringUTF("");
	}
	else{
	cout &lt;&lt; str; //Frees native string resources env-&gt;ReleaseStringUTFChars(prompt, str);
		
		//reads n-consecutive words from the 
		//keyboard and store them in string
		getline(cin, sEntry);
		
        return env-&gt;NewStringUTF(sEntry.c_str());
	}
}</pre>
<p>Run the program (java NativePrompt) and verify the output.</p><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/">JNI Part 4: JNI Strings</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>How to convert a jstring to a C-style string or vice versa?</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Sat, 20 Aug 2011 12:57:56 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[jni]]></category>
		<category><![CDATA[jstring to string]]></category>
		<category><![CDATA[string to jstring]]></category>
		<category><![CDATA[NewStringUTF]]></category>
		<category><![CDATA[GetStringUTFChars]]></category>
		<category><![CDATA[env]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1674</guid>

					<description><![CDATA[<p>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *).&#160; So we cannot use a jstring as a normal C string. We must use the appropriate JNI functions to convert jstring objects to C/C++ strings.&#160; The JNI supports conversion both</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/">How to convert a jstring to a C-style string or vice versa?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *).&nbsp; So we cannot use a jstring as a normal C string. We must use the appropriate JNI functions to convert jstring objects to C/C++ strings.&nbsp; The JNI supports conversion both to and from Unicode and UTF-8 strings. Unicode strings represent characters as 16-bit values, whereas UTF-8 strings use an encoding scheme that is upward compatible with 7-bit ASCII strings. UTF-8 strings act like NULL-terminated C strings.</p>
<p>jstring, which requires a subroutine call to in order to convert a Java Unicode string (2 bytes) to a C-style char* string (1 byte UTF-8 format).</p>
<p>To convert a<strong> jstring to a C-style string</strong>, you might write code like the following:</p>
<pre lang="c" escaped="true" line="1">JNIEXPORT void JNICALLJava_MyJavaClass_printName(JNIEnv *env, jobject obj,
	jstring name)
{
	const char *str= (*env)-&gt;GetStringUTFChars(env,name,0);
	printf(“%s”, str);
	//need to release this string when done with it in order to
	//avoid memory leak
	(*env)-&gt;ReleaseStringUTFChars(env, name, str);
}</pre>
<p>To convert a<strong> </strong><strong>C-style string to </strong><strong>jstring </strong>, you can use the (*env)-&gt;NewStringUTF() function to create a new jstring from a C-style string. For example, a C function that needs to return a Java string could contain the following code:</p>
<pre lang="c" escaped="true" line="1">JNIEXPORT jstring JNICALLJava_MyJavaClass_getName(JNIEnv *env, jobject obj)
{
	return (*env)-&gt;NewStringUTF(env, “Electrofriends.com”);
}</pre><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/">How to convert a jstring to a C-style string or vice versa?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
