Jenks
Jenks

Reputation: 1

How to cast unsigned short to jcharArray?

I'm very new C++. I don't know how to cast unsigned short to jcharArray.

My Code:

extern "C"
    jcharArray
    Java_demo_jni_btten_com_testjnidemo_MainActivity_openStaticWaveFile() {
        float hanfengl, distance;
        int vertical;
        FILE *fp = NULL;
        fp = fopen("/storage/emulated/0/Download/3.stwav", "rb"); 

        PAT.bscan_play_flag = 0;
        U32 cur_param_len = sizeof(PB) + sizeof(Chan[PB.C_N]) + sizeof(PAT.tSd);
        U32 filetype;
        U32 param_len;
        U16 DateStart[6];
        U16 DateEnd[6];

        fread(&filetype, sizeof(filetype), 1, fp);
        fread(DateStart, sizeof(DateStart), 1, fp);
        fread(DateEnd, sizeof(DateEnd), 1, fp);
        fread(&param_len, sizeof(param_len), 1, fp);
        fread(&hanfengl, sizeof(hanfengl), 1, fp);
        fread(&distance, sizeof(distance), 1, fp);
        fread(&vertical, sizeof(vertical), 1, fp);

        if (param_len == cur_param_len) {
            if (PAT.tSto.Read_Start_Flag != 1)    // 读开始标志
                PAT.tSto.Read_Start_Flag = 1;
            fread(&PB, sizeof(PB), 1, fp);
            fread(&Chan[PB.C_N], sizeof(Chan[PB.C_N]), 1, fp);
            fread(&PAT.tSd, sizeof(PAT.tSd), 1, fp);
        }
        fclose(fp);
        return PAT.tSd.HD;
    }

PAT.tSd.HD is U16 unsigned short[512]

look likes:

U16       HD[RESERVE_SAMPLE_DEPTH];  // RESERVE_SAMPLE_DEPTH is 512

I don't know how to return the PAT.tSd.HD, can you help me ?

Upvotes: 0

Views: 71

Answers (2)

Oo.oO
Oo.oO

Reputation: 13385

Maybe this will be a solution for you:

http://jnicookbook.owsiak.org/recipe-No-010/

I'd go via String instead.

Another way is to pass array back and forth:

http://jnicookbook.owsiak.org/recipe-No-013/

On the other hand, you can take a look here:

http://jnicookbook.owsiak.org/recipe-No-014/

to see how to return jcharArray

Hope this helps.

Have fun with JNI!

Upvotes: 1

Suman
Suman

Reputation: 4251

jchararray is a java object and unsigned short is primitive in C++. You can't cast both. You need to make use of JNI Array APIs in order to do the same and copy the data from unsigned short to jchararray.

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#wp17318

Upvotes: 0

Related Questions