Сonvertingint值字节的android字节、onvertingint、android

2023-09-05 04:34:51 作者:別哭勒、怹吥荟吢庝

林挣扎与此相当长的时间..其实我需要用位通过蓝牙 ..现在我的问题是,我需要转移整数为单 字节值.. 是达能的转换 INT 为单 字节价值..和检索它作为一个整数那里......我试过的byteValue()和铸造的事情,但它不是有用。如果我的做法是正确的只是帮助我这还是说一些其他的方式...

Im struggling with this for quite of time.. Actually i need to transfer the integer value along with the bitmap via bluetooth.. Now my problem is i need to transfer the integer as single byte value.. Is tat possible to convert int as single byte value.. and retrieve it as a integer there... I tried byteValue() and the casting thing but its not usefull.. If my approach is right just help me out with this or say some other way...

(当我使用的铸造那么它作为65535返回每次)

(Each time when i am using casting then its returning as 65535)

在此先感谢

推荐答案

这个怎么样?

public static byte[] intToByteArray(int a)
{
    byte[] ret = new byte[4];
    ret[3] = (byte) (a & 0xFF);   
    ret[2] = (byte) ((a >> 8) & 0xFF);   
    ret[1] = (byte) ((a >> 16) & 0xFF);   
    ret[0] = (byte) ((a >> 24) & 0xFF);
    return ret;
}

public static int byteArrayToInt(byte[] b)
{
    return (b[3] & 0xFF) + ((b[2] & 0xFF) << 8) + ((b[1] & 0xFF) << 16) + ((b[0] & 0xFF) << 24);
}