byte[] b= new byte[4];
b[0] = 0x39;
b[1] = 0x69;
b[2] = (byte)0xcc;
b[3] = 0x55;
int dec_len = bytesToInt(b);
public static int bytesToInt(byte[] b) {
int temp = 0;
int res = 0;
for (int i = 0; i < 4; i++) {
res <<= 8;
temp = b[i]&0xFF;
res |= temp;
}
return res;
}
这样出来的结果是963234901.但在C里用atoi(b)的话是9。难道这个bytesToInt不对?
b[0] = 0x39;
b[1] = 0x69;
b[2] = (byte)0xcc;
b[3] = 0x55;
int dec_len = bytesToInt(b);
public static int bytesToInt(byte[] b) {
int temp = 0;
int res = 0;
for (int i = 0; i < 4; i++) {
res <<= 8;
temp = b[i]&0xFF;
res |= temp;
}
return res;
}
这样出来的结果是963234901.但在C里用atoi(b)的话是9。难道这个bytesToInt不对?
解决方案 »
- 关于swing的一个问题
- 一道循环题
- java实现RichTextBox求助
- 请教,想把以下字符窜中的空格去掉,为什么有的就是去不掉呢?谢谢
- 各位朋友讲几句,什么时候使用接口interface什么时候使用抽象类abstract?
- 为什么在Tomcat 5.0.28下我用自己写的JNI程序不能读取HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources下面的值名?
- 难道CSDN上就没有人能回答这个问题吗?
- 求助几个java方面的问题(50分)
- 求教java调用数据库时汉字正确显示问题
- jb5的Applet如何能在IE下浏览使用?
- 如何对图片进行翻页
- 输出格式
结果也是963234901,并不是atoi的结果9
char *value = "12345";
int i = atoi(value);
printf("%d",i);结果应该是12345
{
char ss[5] = {0}; ss[0] = 0x39;
ss[1] = 0x69;
ss[2] = 0xCC;
ss[3] = 0x55;
ss[4] = 0; printf("%d\n", atoi(ss));
}这个结果就是9
public static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[ i ] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
返回值: 返回转换后的整型数。
atoi是将字符串转化为整形,并不是你想的功能。
return Integer.parseInt("0"+ new String(bytes).replaceAll("(?s)\\D.*", ""), 10);
}public static void main(String[] args) {
System.out.println(
atoi(new byte[] { 0x39, 0x69, (byte)0xcc, 0x55 })
);
}