将Byte []转换为int [英] Convert Byte[] to int

查看:148
本文介绍了将Byte []转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种方法将已签名或未签名的字节转换为 int ,但它没有返回它应该返回的内容。有人可以在下面的代码中指出问题吗?

I have this method that converts a signed or non-signed byte to int, but it doesn't return what it is supposed to return. Can someone point out the issue in the below code?

public int convertByteToInt(byte[] b){          
    int value= 0;
    for(int i=0;i<b.length;i++){                
    int n=(b[i]<0?(int)b[i]+256:(int)b[i])<<(8*i);             
        value+=n;
    }         
    return value;       
}

编辑:

我正在读取一个wav文件以计算SNR。来自转换的返回值应该在0到255之间给出一些东西。
应用程序应该比较2个wave文件,on是orignal one,另一个是修改并计算SNR。

I'am actualy reading a wav file in order to calculate the SNR. the returned value from the conversion should give something beetween 0 and 255. The application should compare 2 waves file, on is the orignal one and the other is modified and calculate the SNR .

推荐答案

感谢您的回答,但我发现问题来自其他地方,而不是转换方法,

Thank you for the answers but I found that the problem comes from somewhere else, not from the conversion methode,

还有另一个版本的方法

public int convertirOctetEnEntier(byte[] b){    
    int MASK = 0xFF;
    int result = 0;   
        result = b[0] & MASK;
        result = result + ((b[1] & MASK) << 8);
        result = result + ((b[2] & MASK) << 16);
        result = result + ((b[3] & MASK) << 24);            
    return result;
}

这篇关于将Byte []转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆