无符号char转int问题

加盾男爵 2020-03-06 10:41:21
现在安卓手机需要和设备通过套接字通信,设备端发送的消息是c/c++写的,有无符号类 ,其中一个字段一个字节(16进制)
unsigned char DN;
这个字节是c中的无符号char,而我收到这条消息需要把这个char转化为int,因为java不支持无符号(因为安卓版本的问题,无法使用Integer.parseUnsignedInt("FF",16)),所以我自定义了一个函数来处理这个无符号char

public int unsignCharToInt(byte[] res) {
DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(res));
int a = 0;
try {
a = dataInputStream.readUnsignedByte();
} catch (IOException e) {
e.printStackTrace();
}
return a;
}

当这个无符号char值为"FF"的时候,我希望得到的值为255,但这个函数返回的确实0,请问问题出在哪里?
...全文
589 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2020-03-12
  • 打赏
  • 举报
回复
引用 5 楼 加盾男爵 的回复:
这个我试过,确实打印出来255,可能"0xff",java和c的不是每一位相同的,因为我的程序'0xff"是c传过来的
所以让你debug看看bytes里是什么
加盾男爵 2020-03-12
  • 打赏
  • 举报
回复
引用 3 楼 jklwan 的回复:

public class TestByte {

    public static void main(String[] args) {
        byte[] bytes = new byte[]{(byte) 0xff};
        int i = unsignCharToInt(bytes);
        System.out.println("result:" + i + " ,byteToStr:" + byteToStr(bytes));
    }

    public static int unsignCharToInt(byte[] res) {
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(res));
        int a = 0;
        try {
            a = dataInputStream.readUnsignedByte();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return a;
    }

    public static String byteToStr(byte[] a) {
        StringBuilder sb = new StringBuilder("");
        if (a == null || a.length == 0) {
            return "";
        }
        for (int i = 0; i < a.length; i++) {
            sb.append(Integer.toHexString((a[i] >>> 4) & 0x0F).toUpperCase());
            sb.append(Integer.toHexString(a[i] & 0x0F).toUpperCase());
        }
        return sb.toString();
    }
}
运行结果:
result:255 ,byteToStr:FF
这个我试过,确实打印出来255,可能"0xff",java和c的不是每一位相同的,因为我的程序'0xff"是c传过来的
jklwan 2020-03-11
  • 打赏
  • 举报
回复

public class TestByte {

    public static void main(String[] args) {
        byte[] bytes = new byte[]{(byte) 0xff};
        int i = unsignCharToInt(bytes);
        System.out.println("result:" + i + " ,byteToStr:" + byteToStr(bytes));
    }

    public static int unsignCharToInt(byte[] res) {
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(res));
        int a = 0;
        try {
            a = dataInputStream.readUnsignedByte();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return a;
    }

    public static String byteToStr(byte[] a) {
        StringBuilder sb = new StringBuilder("");
        if (a == null || a.length == 0) {
            return "";
        }
        for (int i = 0; i < a.length; i++) {
            sb.append(Integer.toHexString((a[i] >>> 4) & 0x0F).toUpperCase());
            sb.append(Integer.toHexString(a[i] & 0x0F).toUpperCase());
        }
        return sb.toString();
    }
}
运行结果:
result:255 ,byteToStr:FF
王能 2020-03-11
  • 打赏
  • 举报
回复
直接使用移位操作不就行了,用stream太慢太浪费内存了
大致如下:

public static int toInt(byte[] bytes){
int number = 0;
for(int i = 0; i < 4 ; i++){
number += bytes[i] << i*8;
}
return number;
}

忘了无符号是从几开始了,自己看着改
加盾男爵 2020-03-09
  • 打赏
  • 举报
回复
引用 1 楼 jklwan 的回复:
抛异常了吧,你debug下看看byte数组内容是啥
没有异常

    public String byteToStr(byte[] a) {
        StringBuffer sb = new StringBuffer("");
        if (a == null || a.length == 0) {
            return "";
        }
        for (int i = 0; i < a.length; i++) {
            sb.append(Integer.toHexString((a[i] >>> 4) & 0x0F).toUpperCase());
            sb.append(Integer.toHexString(a[i] & 0x0F).toUpperCase());
        }
        return sb.toString();
    }
我用这个函数打印

Log.d("DevMsg",byteToStr(Arrays.copyOfRange(msg,6,7)));
打印的结果是"FF"
jklwan 2020-03-06
  • 打赏
  • 举报
回复
抛异常了吧,你debug下看看byte数组内容是啥

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧