Trouble with C unit conversion -
i developing android
application uses native c library.
unfamiliar c, , having conversion problems. know problem has signed/unsigned numbers, cannot figure out need fix this.
the c library works fine on own, , produces correct results. when port android, positive values correct. negative values returned 256 minus value.
for example:
8 -> 8 5 -> 5 1 -> 1 0 -> 0 -3 -> 253 -5 -> 251 -8 -> 248
this c code looks when assigns value:
byte *parms; word __stdcall getvalue(byte what, long *val){ char mystringparmsone[255]; sprintf( mystringparmsone, "parms[1]=%d", parms[1] ); log_info( mystringparmsone ); char mystringisg[255]; sprintf( mystringisg, "api_isg=%d",api_isg ); log_info( mystringisg ); char mystringcharparmsb[255]; sprintf( mystringcharparmsb, "(char)parms[1]=%d", (char)parms[1] ); log_info( mystringcharparmsb ); *val=(char)parms[1]-(api_isg?11:0); }
the log_info
statements print values logcat
. below have values when program run , value of -3
expected.
the first **parms[1]=%d** statement returns **parms[1]=253** second **api_isg=%d** statement returns **api_isg=0** third **(char)parms[1]=%d** statement returns **(char)parms[1]=253**
the byte , word types defined follows:
typedef unsigned char byte; typedef unsigned short word;
the wrapper function use send value java shown below:
long java_my_package_model_00024nativecalls_getvaluejava( jnienv *env, jobject obj, word what) { long c; int j = getvalue( what, &c ); if( j == 0 ) { return c; } else { return -1; } }
edit
the getvalue
method used time, individual call gives me problems negative numbers, works other calls. have tried 2 following things wrapper function:
long java_my_package_model_00024nativecalls_getvaluejava( jnienv *env, jobject obj, word what) { long c; int j = getvalue( what, &c ); if( j == 0 ) { return (signed char)c; } else { return -1; } }
the casting of return value above provides me proper return value. however, of other calls function other values haywire.
long java_my_package_model_00024nativecalls_getvaluejava( jnienv *env, jobject obj, word what) { long c; int j = getvalue( what, &c ); if( j == 0 ) { return (signed long)c; } else { return -1; } }
the above casting of return value changed nothing.
this happens because putting unsigned char (iirc in ndk char unsigned default) inside long. if value put inside long signed compiler have changed sign bit of long.
try change cast from
*val=(char)parms[1]-(api_isg?11:0);
to
*val=(long)((signed char)parms[1]-(api_isg?11:0));
(not sure if (long) cast required, maybe cast (signed char) enough.
Comments
Post a Comment