【问题标题】:android how to get unsigned byte array from getinputstreamandroid如何从getinputstream获取无符号字节数组
【发布时间】:2013-10-10 06:41:48
【问题描述】:

我使用来自socketgetInputStream;但是,当我想将我的字节数组转换为 long 时,它会得到错误的数字。当我使用调试模式查看字节数组的每个值时,我发现该值将被签名。如何将有符号字节数组转换为无符号数组?或者我错误地认为该问题与signed 问题无关。如果有任何信息不足,请告诉我。谢谢。

下面是我的代码:

InputStream inputStream = socket.getInputStream();

byte[] fileNameBytes = new byte[8];
byte[] totalLengthBytes = new byte[8];
try {
    inputStream.read(fileNameBytes);
    inputStream.read(totalLengthBytes);
} catch (Exception ex) {
    Log.e("Error in InputStream: " + ex.getMessage());
}

totalLength = byteArrayToLong(totalLengthBytes);

【问题讨论】:

    标签: android bytearray inputstream


    【解决方案1】:

    java 在有符号/无符号方面存在问题。即,没有未签名的。当我需要将原始字节上传到期望字节为无符号的 C# Web 服务时(即 0 到 255 与 -127 到 +127),我使用了我并不引以为豪的代码,看起来像:

            List<Integer> l = new ArrayList<Integer>();
            if (bytearray != null) {
                for (int i = 0; i < bytearray.length; ++i) 
                {
                    l.add(Integer.valueOf((char) bytearray [i]) & 0x00FF);
                }
            }
    

    其中bytearraybyte[]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2011-12-07
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多