JNIEXPORT를 한 이유는 아래 DLL을
JAVA에서 사용하려고........
C에서만 사용하는 거라면 변수 타입에 j붙은거 떼고..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include <stdio.h>
#include <string.h>
#include "CRC16.h"
//2019 01 28 HJH - CRC16 GENERATOR TEST
JNIEXPORT int JNICALL Java_CRC16_byteArray(JNIEnv *env, jobject jobj,jbyteArray jsrc)
{
jbyteArray result;
jsize n = (*env)->GetArrayLength(env,jsrc);
jbyte *pbyte = (*env)->GetByteArrayElements(env,jsrc,0);
jbyte value;
result = (*env)->NewByteArray(env,n);
unsigned char btData[n];
memset(btData,0,sizeof(btData));
for(int k=0;k<n;k++)
{
(*env)->GetByteArrayRegion(env,jsrc,k,1,&value);
btData[k] = (unsigned char)value;
//(*env)->SetByteArrayRegion(env,result,k,1,&value);
}
int nOffset = 0;
int nWords = 13; /*필요 시 매개변수로 받아서*/
unsigned short wCRC16 = 0xA5A5;
unsigned short *pwData = (unsigned short*)(btData + nOffset);
for (int i = 0; i < nWords; i++)
{
//wCRC16 = wCRC16 ^ pwData[i];
wCRC16 = pwData[i] ^ wCRC16;
}
unsigned char convEndian[2];
int ret;
convEndian[0] = (unsigned short)((wCRC16 >> 0) & 0xff);
convEndian[1] = (unsigned short)((wCRC16 >> 8) & 0xff);
ret = ((unsigned short)convEndian[0] << 8) | ((unsigned short)convEndian[1] << 0);
return ret;
}
|
cs |