Warning: while trying to convert java byte[] to C unsigned char* - java

I am writing a JNI. In that, my Java program takes an Image byte array using ByteOutputStream() and then this array is used to call a function in C that converts byte array to unsigned char*. Here is the code:
JNIEXPORT void JNICALL Java_ImageConversion_covertBytes(JNIEnv *env, jobject obj, jbyteArray array)
{
unsigned char* flag = (*env)->GetByteArrayElements(env, array, NULL);
jsize size = (*env)->GetArrayLength(env, array);
for(int i = 0; i < size; i++) {
printf("%c", flag[i]);}
}
In this I keep getting a warning when I compile:
warning: initializing 'unsigned char *' with an expression of type 'jbyte *' (aka 'signed char *') converts between pointers to integer types with different sign [-Wpointer-sign]
unsigned char* flag = (*env)->GetByteArrayElements(env, array, NULL);
How can I remove this warning? I want to print the all characters.

The warning exists because the sign change might be important. In JNI the jbyte corresponds to Java byte which is a signed 8-bit integer; in C it is explicitly signed char.
However, it is OK to access any object with any character pointer, so you can cast to unsigned char explicitly:
unsigned char* flag = (unsigned char*)(*env)->GetByteArrayElements(env, array, NULL);
Alternatively, you can declare flag as signed char:
signed char* flag = (*env)->GetByteArrayElements(env, array, NULL);
This is fine for printf("%c\n", flag[i]); because %c requires that the argument be an integer; the integer is then converted to unsigned char so both signed and unsigned char will do.
However 3rd option would be to use neither - if you just want to write them to the terminal, use a void * pointer and fwrite:
JNIEXPORT void JNICALL
Java_ImageConversion_covertBytes(JNIEnv *env, jobject obj, jbyteArray array)
{
void *flag = (*env)->GetByteArrayElements(env, array, NULL);
jsize size = (*env)->GetArrayLength(env, array);
fwrite(flag, 1, size, stdout);
}
and let fwrite worry about the looping.

Related

Data through JNI is not passing properly

I am using JNI to call the native C++ layer.
java layer
int res= recog(audioFilePath, grammarFilePath, contextID, subContextID);
C++ layer
JNIEXPORT void JNICALL Java_com_uniphore_voice_recogniser_NuanceOfflineRecogniser_recog(JNIEnv *jenv, jobject jobj, jstring jaudioFilePath,
jstring jgrammarFilePath, <br/>
jstring jcontextID,
jstring jsubContext)
{
const char* _audioFilePath = (char*)jenv->GetStringChars(jaudioFilePath, JNI_FALSE);
const char* _grammarFilePath = (char*)jenv->GetStringChars(jgrammarFilePath, JNI_FALSE);
const char* _contextId = (char*)jenv->GetStringChars(jcontextID, JNI_FALSE);
const char* _subContextId = (char*)jenv->GetStringChars(jsubContext, JNI_FALSE);
std::wcout << "audio file path: " << _audioFilePath <<" "<< std::strlen(_audioFilePath) <<std::endl
<< "grammar file path: "<< _grammarFilePath <<" "<<std::strlen(_grammarFilePath) << std::endl
<< "contextId: " << _contextId << std::endl
<< "subContextId: " << _subContextId << std::endl << std::endl;
I can see at java layer values is properly passed to the lower level but in c++ layer while printing that value in C++ layer I can see it is printing only first character of whole string.
suppose if audioFilePath I am passing like "c:\test.wav" I am getting print in c++ layer only like c
I am trying in visual studio 2013 and project character support I selected as Unicode support.
I am new to c++ environment, please help to get the reason for this one.
According to JNI docs GetStringChars returns the unicode characters for the given string in a jchar * which is an unsigned short *. You cast it to a char *. When you use cout with a char * it expects a string in ASCII format with a null-terminator. You pass it a pointer to a string in unicode format, which has every other character 0 for plain ASCII characters. Hence why you only print the first character in the string.
GetStringChars is not returning a pointer to single byte characters, but two byte, unicode characters
const jchar * GetStringChars(JNIEnv *env, jstring string,
jboolean *isCopy);
Returns a pointer to the array of Unicode characters of the string.
Instead, try
GetStringUTFChars
This will be null terminated also.

create short int array in c like java short[]

Java:
byte[] arr1=new byte[]{0x01};
String aa = "helloo";
short[] s1 = new short[1]; // **
s1[0] = 246; // **
Object[] obj = new Object[]{s1,arr1,aa}
C:
signed char a1[] = {0x01};
char *str = "helloo";
short int st1[] = {246}; // **
char* c [] = {st1,str1,c2};
Is short int st1[] = {246} correct? And I am getting this error:
"illegal implicit conversion from 'short *' to 'char *'".
How to assign short to char?
char* c []
is an array of pointers, not an array of chars.
Use something like
short st1[] = { 246 };
char* str = "helloo";
char c [] = {st1[0], str[0], str[1], str[2], str[3], str[4], str[5]};
str[i] gets individual characters, since 'char* str' points to the first element of an array.
If you need an array of string, then make it
char tmp[1024];
// itoa is the conversion of st1[0] to string
char* c[] = { itoa(st1[0], tmp, 10), str };
Cast st1 to a char*. I.e.:
char* c [] = {(char*)st1,str1,c2};
Note that you'll have to cast the pointer back to short* when accessing the elements it points to if you want to get the correct data.
C++ doesn't have a base Object type. You will have to convert your strings all to a specific type.
std::string wtf[]= { std::string(a1, a1+ 1), std::string(st1, st1+ 1), std::string(str) }; // don't forget to #include <string>
In C or C++ there is no common base class for all types (like Java's Object), the best you can use is void* c[]=...; (void* stands for untyped pointers, so it can hold anything) or explicitely cast to the desired type (but then it's undefined to access a short via a char-pointer).
Although its highly not recommended, the rough equivalent of the las line in c is:
void*  c [] = {st1,str1,c2};

Android JNI C simple append function

Id like to make a simple function, that return value of two Strings, basically:
java
public native String getAppendedString(String name);
c
jstring Java_com_example_hellojni_HelloJni_getAppendedString(JNIEnv* env, jobject thiz, jstring s) {
jstring sx = (*env)->GetStringUTFChars(env, s, NULL);
return ((*env)->NewStringUTF(env, "asd ")+sx);
}
It returns:
jni/hello-jni.c:32: warning: initialization discards qualifiers from pointer target type
jni/hello-jni.c:34: error: invalid operands to binary + (have 'char *' and 'char *')
The retval will be: "asd qwer", how can I do this?
jstring s1 = (*env)->NewStringUTF(env, "456");
jstring s2 = (*env)->NewStringUTF(env, "123");
jstring sall=strcat(s1, s2);
return sall;
Only returns "456"
There are a few issues here:
GetStringUTFChars returns a jbyte * (a null-terminated C string), not a jstring. You need this C string to do string manipulation in C.
You need to call ReleaseStringUTFChars when you're done with it.
You need to allocate enough memory to hold the concatenated string, using malloc.
As ethan mentioned, you need to concatenate your two C strings with strcat. (You cannot do this with the + operator. When applied to a pointer, + returns the pointer from the offset of the original pointer.)
Remember to free the memory you allocated after you're done with it (ie, after it's been interned as a Java string.)
You should do something along the lines of:
char *concatenated;
const jbyte *sx;
jstring retval;
/* Get the UTF-8 characters that represent our java string */
sx = (*env)->GetStringUTFChars(env, s, NULL);
/* Concatenate the two strings. */
concatenated = malloc(strlen("asd ") + strlen(sx) + 1);
strcpy(concatenated, "asd ");
strcat(concatenated, sx);
/* Create java string from our concatenated C string */
retval = (*env)->NewStringUTF(env, concatenated);
/* Free the memory in sx */
(*env)->ReleaseStringUTFChars(env, s, sx);
/* Free the memory in concatenated */
free(concatenated);
return retval;
You can't concatenate two char* with + in c++. Try using strcat instead.
http://www.cplusplus.com/reference/clibrary/cstring/strcat/
EDIT:
from the documentation for strcat:
char * strcat ( char * destination, const char * source );
Concatenate strings
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.
This means that the first argument to strcat needs to have enough memory allocated to fit the entire concatenated string.

Return Arabic from JNI call

I have been trying to return an ARABIC string from a JNI call.
The java method is as follows
private native String ataTrans_CheckWord(String lpszWord, String lpszDest, int m_flag, int lpszReserved);
lpszWord : Input English
lpszDest : Ignore
m_flag : Ignore
lpszReserved :Ignore
Now when I use javah to generate the header file I get a C++ header file with this signature
JNIEXPORT jstring JNICALL Java_MyClass_ataTrans_1CheckWord (JNIEnv* env, jobject, jstring, jstring, jint , jint)
Now in this C++ code I have statements such as this
JNIEXPORT jstring JNICALL Java_MyClass_ataTrans_1CheckWord(JNIEnv* env, jobject, jstring jstrInput, jstring, jint , jint)
{
char aa[10];
char* bb;
char** cc;
bb = aa;
cc = &bb;
jstring tempValue;
const char* strCIn = (env)->GetStringUTFChars(jstrInput , &blnIsCopy);
int retVal = pDllataTrans_CheckWord(strCIn, cc, m_flag, lpszReserved);
printf("Orginal Arabic Conversion Index 0: %s \n",cc[0]); //This prints ARABIC properly
tempValue = (env)->NewString((jchar* )cc[0],10); // convert char array to jstring
printf("JSTRING UNICODE Created : %s \n",tempValue); //This prints junk
return tempValue;
}
I believe the ARABIC content is inside the pointer to a pointer “cc”. Finally in my java code I have a call like this
String temp = myclassInstance.ataTrans_CheckWord("ABCDEFG", "",1, 0);
System.out.println("FROM JAVE OUTPUT : "+temp); //Prints Junk
I just can’t get to return some ARABIC character out into my JAVA code. Is there something wrong I am doing? I have tried out various other alternates such as
tempValue = env->NewStringUTF("شسيشسيشسيشس");
and return tempValue but no luck. Its always garbage on the JAVA side.
Java strings are internally UTF-16, an encoding which uses 2 or 4 bytes per character. Your translation system seems to return strings encoded in a MBCS (Multi-Byte Character Set) - 1-N bytes per character.
The JNI NewString function expects data encoded as UTF-16, and you're passing it something else - so in java you get garbage data. The one thing that is lacking from your information is which encoding your translation system uses. I'll assume it's UTF-8, and use MultiByteToWideChar to convert to the format java expects. The below code assumes that you're doing this on Windows - if not, specify platform, and look at e.g. the iconv library.
int Len = strlen(cc[0])*2+2;
wchar_t * Buffer = (wchar_t *) malloc(Len);
MultiByteToWideChar(CP_UTF8, 0, cc[0], -1, Buffer, Len);
tempValue = (env)->NewString((jchar* )Buffer,wcslen(Buffer));
free(Buffer);
If you get strings as some other codepage, replace CP_UTF8 above.
As a side note, if the encoding actually is UTF-8, you can simply pass your cc[0] to NewStringUTF instead - This function handles the UTF-8 to UTF-16 conversion internally.

Convert ICU4C byte to java char

I am accessing an ICU4C function through JNI which returns a UChar * (i.e. unicode character array).... I was able to convert that to jbyteArray by equating each member of the UChar array to a local jbyte[] array that I created and then I returned it to Java using the env->SetByteArrayRegion() function... now I have the Byte[] array in Java but it's all gibberish pretty much.. Weird symbols at best... I am not sure where the problem might be... I am working with unicode characters if that matters... how do I convert the byte[] to a char[] in java properly? Something is not being mapped right... Here is a snippet of the code:
--- JNI code (altered slighter to make it shorter) ---
static jint testFunction(JNIEnv* env, jclass c, jcharArray srcArray, jbyteArray destArray) {
jchar* src = env->GetCharArrayElements(srcArray, NULL);
int n = env->getArrayLength(srcArray);
UChar *testStr = new UChar[n];
jbyte destChr[n];
//calling ICU4C function here
icu_function (src, testStr); //takes source characters and returns UChar*
for (int i=0; i<n; i++)
destChr[i] = testStr[i]; //is this correct?
delete testStr;
env->SetByteArrayRegion(destArray, 0, n, destChr);
env->ReleaseCharArrayElements(srcArray, src, JNI_ABORT);
return (n); //anything for now
}
-- Java code --
string wohoo = "ABCD bal bla bla";
char[] myChars = wohoo.toCharArray();
byte[] myICUBytes = new byte[myChars.length];
int value = MyClass.testFunction (myChars, myICUBytes);
System.out.println(new String(myICUBytes)) ;// produces gibberish & weird symbols
I also tried: System.out.println(new String(myICUBytes, Charset.forName("UTF-16"))) and it's just as gebberishy....
note that the ICU function does return the proper unicode characters in the UChar *... somewheres between the conversion to jbyteArray and Java that is is messing up...
Help!
destChr[i] = testStr[i]; //is this correct?
This looks like an issue all right.
JNI types:
byte jbyte signed 8 bits
char jchar unsigned 16 bits
ICU4C types:
Define UChar to be wchar_t if that is
16 bits wide; always assumed to be
unsigned.
If wchar_t is not 16 bits wide, then
define UChar to be uint16_t or
char16_t because GCC >=4.4 can handle
UTF16 string literals. This makes the
definition of UChar platform-dependent
but allows direct string type
compatibility with platforms with
16-bit wchar_t types.
So, aside from anything icu_function might be doing, you are trying to fit a 16-bit value into an 8-bit-wide type.
If you must use a Java byte array, I suggest converting to the 8-bit char type by transcoding to a Unicode encoding.
To paraphrase some C code:
UChar *utf16 = (UChar*) malloc(len16 * sizeof(UChar));
//TODO: fill data
// convert to UTF-8
UConverter *encoding = ucnv_open("UTF-8", &status);
int len8 = ucnv_fromUChars(encoding, NULL, 0, utf16, len16, &status);
char *utf8 = (char*) malloc(len8 * sizeof(char));
ucnv_fromUChars(encoding, utf8, len8, utf16, len16, &status);
ucnv_close(encoding);
//TODO: char to jbyte
You can then transcode this to a Java String using new String(myICUBytes, "UTF-8").
I used UTF-8 because it was already in my sample code and you don't have to worry about endianness. Convert my C to C++ as appropriate.
Have you considered using ICU4J?
Also, when converting your bytes to a string, you will need to specify a character encoding. I'm not familiar with the library in question, so I can't advise you further, but perhaps this will be "UTF-16" or similar?
Oh, and it's also worth noting that you might simply be getting display errors because the terminal you're printing to isn't using the correct character set and/or doesn't have the right glyphs available.

Categories