50,530
社区成员
发帖
与我相关
我的任务
分享
int JasDevID::serialize(uint8_t * pBuf )
{
//chunk head
uint32_t chunk_size = CHUNK_SIZE;
uint16_t chunk_id = CHUNK_ID;
uint16_t chunk_ver = CHUNK_VER;
memcpy(pBuf,&chunk_size,4);
pBuf+=4;
memcpy(pBuf,&chunk_id,2);
pBuf+=2;
memcpy(pBuf,&chunk_ver,2);
pBuf+=2;
// chunk data
uint8_t * pBufDataStart = pBuf;
memcpy(pBuf,guid,GUID_LEN);
pBuf+=GUID_LEN;
memcpy(pBuf,pn,PN_LEN);
pBuf+=PN_LEN;
memcpy(pBuf,sn,SN_LEN);
pBuf+=SN_LEN;
// chunk crc
int crc = Utility::get_CRC(pBufDataStart, CHUNK_SIZE);
memcpy(pBuf,&crc,sizeof(int));
return 0;
}
实在不会啊[/quote]
这个里面的pBuf+=4,这样的代码,这是在他的内存地址上加4吧,根本不知道在Java里面咋处理。。。int JasDevID::serialize(uint8_t * pBuf )
{
//chunk head
uint32_t chunk_size = CHUNK_SIZE;
uint16_t chunk_id = CHUNK_ID;
uint16_t chunk_ver = CHUNK_VER;
memcpy(pBuf,&chunk_size,4);
pBuf+=4;
memcpy(pBuf,&chunk_id,2);
pBuf+=2;
memcpy(pBuf,&chunk_ver,2);
pBuf+=2;
// chunk data
uint8_t * pBufDataStart = pBuf;
memcpy(pBuf,guid,GUID_LEN);
pBuf+=GUID_LEN;
memcpy(pBuf,pn,PN_LEN);
pBuf+=PN_LEN;
memcpy(pBuf,sn,SN_LEN);
pBuf+=SN_LEN;
// chunk crc
int crc = Utility::get_CRC(pBufDataStart, CHUNK_SIZE);
memcpy(pBuf,&crc,sizeof(int));
return 0;
}
实在不会啊
class crc
{
public static int get_CRC(byte[] buf)
{
int crc = 0;
int i;
for (int index = 0; index != buf.length; ++index) {
for (i = 0x80; i != 0; i >>>= 1) {
if ((crc & 0x8000) != 0) {
crc <<= 1;
crc ^= 0x1021;
} else {
crc <<= 1;
}
if ((buf[index] & i) != 0) {
crc ^= 0x1021;
}
}
}
return crc;
}
public static void main(String[] args)
{
byte[] buf = {0x31, 0x14, 0x15, (byte)0x92, 0x66};
System.out.println(get_CRC(buf));
}
}
已改正,测试可用long get_CRC(String pBuf, int len) {
//TODO: implement crc
int crc = 0;
char[] cs = pBuf.toCharArray();
for (int i = 0; i < cs.length; i++) {
cs[i] &= 0xff;
}
int j = 0;
while (len-- > 0) {
for (int i = 0x80; i != 0; i = i >> 1) {
if ((crc & 0x8000) != 0) {
crc = crc << 1;
crc = crc ^ 0x1021;
} else {
crc = crc << 1;
}
if ((cs[j] & i) != 0) {
crc = crc ^ 0x1021;
}
}
j++;
}
return crc;
}
class crc
{
public void get_CRC(byte[] buf, int len)
{
int crc = 0;
byte i;
for (int index = 0; index != buf.length; ++index) {
for (i = (byte)0x80; i != 0; i >>= 1) {
if ((crc & 0x8000) != 0) {
crc <<= 1;
crc ^= 0x1021;
} else {
crc <<= 1;
}
if (buf[index] != 0) {
crc ^= 0x1021;
}
}
}
}
}
public static void main(String[] args) {
char[] cs = new Topics_392076826().serialize("11111111111111111111111111111111", "22", "88888888");
}
public static final int
CHUNK_ID = 1001, CHUNK_VER = 0000, GUID_LEN = 32, PN_LEN = 2, SN_LEN = 8
, CHUNK_SIZE = GUID_LEN + PN_LEN + SN_LEN;
char[] serialize(String guid, String pn, String sn) {
char[] pBuf = new char[64];
int index = 0;
//chunk head
int chunk_size = CHUNK_SIZE;
int chunk_id = CHUNK_ID;
int chunk_ver = CHUNK_VER;
char[] cs = intToChar(chunk_size);
System.arraycopy(cs, 0, pBuf, index, cs.length);
index += 4;
cs = intToChar(chunk_id);
System.arraycopy(cs, 0, pBuf, index, cs.length);
index += 2;
cs = intToChar(chunk_ver);
System.arraycopy(cs, 0, pBuf, index, cs.length);
index += 2;
int temp_index = index;
System.arraycopy(guid.toCharArray(), 0, pBuf, index, GUID_LEN);
index += GUID_LEN;
System.arraycopy(pn.toCharArray(), 0, pBuf, index, PN_LEN);
index += PN_LEN;
System.arraycopy(sn.toCharArray(), 0, pBuf, index, SN_LEN);
index += SN_LEN;
// chunk crc
int crc = get_CRC(new String(pBuf, temp_index, index), CHUNK_SIZE);
System.out.println(crc);
cs = intToChar(crc);
System.out.println((int)cs[0]);
System.out.println((int)cs[1]);
System.arraycopy(cs, 0, pBuf, index, cs.length);
index += 4; // c++ sizeof(int) is 4
return Arrays.copyOfRange(pBuf, 0, index);
}
char[] intToChar(int i) {
if (i <= 0xff) {
return new char[]{(char)i};
}
char[] cs = new char[2];
cs[1] = (char) (i / (1 << 8));
cs[0] = (char) (i - cs[1] * (1 << 8));
return cs;
}
int get_CRC(String pBuf, int len) {
int crc = 0;
char[] cs = pBuf.toCharArray();
for (int i = 0; i < cs.length; i++) {
cs[i] &= 0xff;
}
int j = 0;
while (len-- > 0) {
for (int i = 0x80; i != 0; i = i >> 1) {
if ((crc & 0x8000) != 0) {
crc = crc << 1;
crc = crc ^ 0x1021;
} else {
crc = crc << 1;
}
if ((cs[j] & i) != 0) {
crc = crc ^ 0x1021;
}
}
j++;
}
return crc;
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at crc.get_CRC(Main.java:19)
at crc.main(Main.java:30)
int crc = Utility::get_CRC(pBufDataStart, CHUNK_SIZE);
CHUNK_SIZE这个值的定义是:const uint16_t GUID_LEN = 32;
const uint16_t PN_LEN = 2;
const uint16_t SN_LEN = 8;
const uint32_t CHUNK_SIZE = GUID_LEN + PN_LEN + SN_LEN;
那么是不是这个CHUNK_SIZE 就是42?
如此运行代码的时候,都报错数组越界啦啦:
int JasDevID::serialize(uint8_t * pBuf )
{
//chunk head
uint32_t chunk_size = CHUNK_SIZE;
uint16_t chunk_id = CHUNK_ID;
uint16_t chunk_ver = CHUNK_VER;
memcpy(pBuf,&chunk_size,4);
pBuf+=4;
memcpy(pBuf,&chunk_id,2);
pBuf+=2;
memcpy(pBuf,&chunk_ver,2);
pBuf+=2;
// chunk data
uint8_t * pBufDataStart = pBuf;
memcpy(pBuf,guid,GUID_LEN);
pBuf+=GUID_LEN;
memcpy(pBuf,pn,PN_LEN);
pBuf+=PN_LEN;
memcpy(pBuf,sn,SN_LEN);
pBuf+=SN_LEN;
// chunk crc
int crc = Utility::get_CRC(pBufDataStart, CHUNK_SIZE);
memcpy(pBuf,&crc,sizeof(int));
return 0;
}
这个函数用到的变量的定会为:
public:
const uint16_t CHUNK_ID = 1001;
const uint16_t CHUNK_VER = 0000;
const uint16_t GUID_LEN = 32;
const uint16_t PN_LEN = 2;
const uint16_t SN_LEN = 8;
const uint32_t CHUNK_SIZE = GUID_LEN + PN_LEN + SN_LEN;
public:
char guid[GUID_LEN+1];
char pn[PN_LEN+1];
char sn[SN_LEN+1];
long get_CRC(String pBuf, int len) {
//TODO: implement crc
int crc = 0;
char[] cs = pBuf.toCharArray();
for (int i = 0; i < cs.length; i++) {
cs[i] &= 0xff;
}
int j = 0;
while (len-- > 0) {
for (int i = 0x80; i != 0; i = i >> 1) {
if ((crc & 0x8000) != 0) {
crc = crc << 1;
crc = crc ^ 0x1021;
} else {
crc = crc << 1;
}
if ((cs[j] & i) != 0) {
crc = crc ^ 0x1021;
}
}
j++;
}
return crc;
}
不好意思啊,我其实连移位都不大会,一般写webapp比较多。。。[/quote]
代码错在这里cs[i] &= 0xff;
汉字在Java中用UTF-16LE表示,两个字节,上面的写法把高字节删掉了long get_CRC(String pBuf, int len) {
//TODO: implement crc
int crc = 0;
char[] cs = pBuf.toCharArray();
for (int i = 0; i < cs.length; i++) {
cs[i] &= 0xff;
}
int j = 0;
while (len-- > 0) {
for (int i = 0x80; i != 0; i = i >> 1) {
if ((crc & 0x8000) != 0) {
crc = crc << 1;
crc = crc ^ 0x1021;
} else {
crc = crc << 1;
}
if ((cs[j] & i) != 0) {
crc = crc ^ 0x1021;
}
}
j++;
}
return crc;
}
不好意思啊,我其实连移位都不大会,一般写webapp比较多。。。