Сonvertingint值字节的android [英] Сonverting int to byte in android

查看:133
本文介绍了Сonvertingint值字节的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林挣扎与此相当长的时间..其实我需要用位<沿着/ code传输的整数价值>通过蓝牙 ..现在我的问题是,我需要转移整数 字节值.. 是达能的转换 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);
}

这篇关于Сonvertingint值字节的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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