Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with C unit conversion
    primarykey
    data
    text
    <p>I am currently developing an <code>Android</code> application which uses a <code>native C library.</code> I am very unfamiliar with C, and am having some conversion problems. I know that the problem has something to do with signed/unsigned numbers, but cannot figure out what I need to do to fix this.</p> <p>The C library works fine on its own, and produces the correct results. However when I port it back to Android, only the positive values are correct. The negative values are returned as 256 minus the value. </p> <p>For example:</p> <pre><code> 8 -&gt; 8 5 -&gt; 5 1 -&gt; 1 0 -&gt; 0 -3 -&gt; 253 -5 -&gt; 251 -8 -&gt; 248 </code></pre> <p>This is what the C code looks like when it assigns the value:</p> <pre><code>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); } </code></pre> <p>The <code>LOG_INFO</code> statements print the values to <code>LogCat</code>. Below I have values when the program is run and a value of <code>-3</code> is expected.</p> <pre><code>The first **parms[1]=%d** statement returns **parms[1]=253** The second **API_isG=%d** statement returns **API_isG=0** The third **(char)parms[1]=%d** statement returns **(char)parms[1]=253** </code></pre> <p>The BYTE and WORD types are defined as follows:</p> <pre><code>typedef unsigned char BYTE; typedef unsigned short WORD; </code></pre> <p>The wrapper function I use to send this value back to Java is shown below:</p> <pre><code>long Java_my_package_Model_00024NativeCalls_GetValueJava( JNIEnv *env, jobject obj, WORD what) { long c; int j = GetValue( what, &amp;c ); if( j == 0 ) { return c; } else { return -1; } } </code></pre> <p><strong>EDIT</strong> </p> <p>The <code>GetValue</code> method is used all the time, but only this individual call gives me problems with negative numbers, it works with other calls. I have tried the two following things for my wrapper function:</p> <pre><code>long Java_my_package_Model_00024NativeCalls_GetValueJava( JNIEnv *env, jobject obj, WORD what) { long c; int j = GetValue( what, &amp;c ); if( j == 0 ) { return (signed char)c; } else { return -1; } } </code></pre> <p>The casting of the return value above provides me with a proper return value. However, all of the other calls to this function for other values are completely haywire. </p> <pre><code>long Java_my_package_Model_00024NativeCalls_GetValueJava( JNIEnv *env, jobject obj, WORD what) { long c; int j = GetValue( what, &amp;c ); if( j == 0 ) { return (signed long)c; } else { return -1; } } </code></pre> <p>The above casting of the return value changed nothing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload