最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】java中byte数组转换为int

Java crifan 5256浏览 0评论

【背景】

在折腾:

【记录】给usb-serial-for-android中的Silicon Labs的CP2102中添加RTS和DTR的支持

期间,需要将一个byte的数组转换为int。

【折腾过程】

1.参考:

Converting from byte to int in java

去取数组index为0的即可:

byte[] partNum = new byte[]{0};
.....
int gotPartNum = partNum[0];

 

【总结】

直接将(byte)数组的对应某个值(通过index取),赋值给int变量即可。


【后记】

1.后来遇到:

        //1. verify baudrate
        byte[] baudrateByteArr = new byte[]{0,0,0,0};
        mConnection.controlTransfer(REQTYPE_INTERFACE_TO_HOST, CP210X_GET_BAUDRATE,
                0, 0/* index */, baudrateByteArr, 4, USB_CTRL_GET_TIMEOUT_MILLIS);
        int readbackBaudrate = (baudrateByteArr[0]) | (baudrateByteArr[1] << 8) | (baudrateByteArr[2] << 16) | (baudrateByteArr[3] << 24);
        if(writtenBaudrate == readbackBaudrate){
            //ok
        }
        else{
            //false
        }

其中:

baudrateByteArr 值是:[-80, 4, 0, 0]

如图:

verify code for baudrate convert to byte array to int fail

结果算出来的值是:

-80

是不对的,希望是:

0x4B0

即:

1200 -> 0x4B0

[-80, 4, 0, 0]

B0 04 00 00

0x000004B0 = 0x4B0

所以,继续研究,找到一个好的方式去转换。

去试试:

        //int readbackBaudrate = (baudrateByteArr[0]) | (baudrateByteArr[1] << 8) | (baudrateByteArr[2] << 16) | (baudrateByteArr[3] << 24);
        int readbackBaudrate= (baudrateByteArr[3]<<24)&0xff000000|(baudrateByteArr[2]<<16)&0xff0000|(baudrateByteArr[1]<<8)&0xff00|(baudrateByteArr[0]<<0)&0xff;

结果是:

然后就可以了:

still  80 4 0 0 byte arrary

now can normally convert to int

2.虽然工作,但是用起来很不方便,所以去写个函数,最终如下:

    //convert byte array to integer type value
    //possible returned value: byte/short/int/long
    //Note: max support long -> input byte array length should not exceed 8 
    public static long byteArrToInteger(byte[] byteArr){
        long convertedInterger = 0;
        
        //follow works:
        //int readbackBaudrate= (baudrateByteArr[3]<<24)&0xff000000|(baudrateByteArr[2]<<16)&0xff0000|(baudrateByteArr[1]<<8)&0xff00|(baudrateByteArr[0]<<0)&0xff;
        
        //115200 == [0, -62, 1, 0]
        //1200==[-80, 4, 0, 0]
        for(int i = 0; i < byteArr.length; i++){
            //long curValue = byteArr[i];
            //int curValue = byteArr[i];
        	byte curValue = byteArr[i];
            long shiftedValue = curValue << (i * 8);
            long mask = 0xFF << (i * 8);
            long maskedShiftedValue = shiftedValue & mask;
            //0x0, 0xC200, 0x10000, 0x0 -> 115200==0x1C200
            //0xB0, 0x400, 0x0, 0x0-> 1200==0x4B0
            convertedInterger |= maskedShiftedValue;
        }
        
       return convertedInterger;
    }
    
    //convert interger type value to byte array
    //possible input value: byte/short/int/long
    //Note: max return byte array is 8 -> max support long 
    public static byte[] integerToByteArr(long inputIntergerValue, int byteLen){
        byte[] convertedByteArr = new byte[byteLen];
        
        for (int i = 0; i < convertedByteArr.length; i++) {
        	convertedByteArr[i] = (byte) ((inputIntergerValue >> (8 * i)) & 0xFF);
            //115200 == [0, -62, 1, 0, 0, 0, 0, 0]
            //1200==[-80, 4, 0, 0]
        	//600==[88, 2]
        	//32==[32]
        }

       return convertedByteArr;
    }
        
	private void testByteArrToInt(){
		//byte[] baudrateByteArr1 = new byte[]{0, -62, 1, 0}; //115200
		//byte[] baudrateByteArr2 = new byte[]{-80, 4, 0, 0}; //1200

		long testLong = 115200; //115200==0x1C200
		int testInt = 1200; //1200==0x4B0
		short testShort = 600; //600==0x258
		byte testByte = 32; //32=0x20

		byte[] baudrateByteArr1 = integerToByteArr(testLong, 8); //115200 == [0, -62, 1, 0, 0, 0, 0, 0]
		byte[] baudrateByteArr2 = integerToByteArr(testInt, 4); //1200==[-80, 4, 0, 0]
		byte[] baudrateByteArr3 = integerToByteArr(testShort, 2); //600==[88, 2]
		byte[] baudrateByteArr4 = integerToByteArr(testByte, 1); //32==[32]

		int readbackBaudrate1 = (int)byteArrToInteger(baudrateByteArr1); //115200
		int readbackBaudrate2 = (int)byteArrToInteger(baudrateByteArr2); //1200
		int readbackBaudrate3 = (int)byteArrToInteger(baudrateByteArr3); //600
		int readbackBaudrate4 = (int)byteArrToInteger(baudrateByteArr4); //32
		android.util.Log.d("testByteArrToInt", "readbackBaudrate1=" + readbackBaudrate1);
		android.util.Log.d("testByteArrToInt", "readbackBaudrate2=" + readbackBaudrate2);
		android.util.Log.d("testByteArrToInt", "readbackBaudrate3=" + readbackBaudrate3);
		android.util.Log.d("testByteArrToInt", "readbackBaudrate4=" + readbackBaudrate4);
	}

 

【总结】

java中,还是很多常见功能没实现,搞得还要自己去写函数。哎。。。

转载请注明:在路上 » 【已解决】java中byte数组转换为int

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.172 seconds, using 22.06MB memory