android 将int转byte,byte转int的两种方法
来源:互联网 发布:软件猎手官方下载 编辑:程序博客网 时间:2024/04/27 18:39
作者:mailbomb
/**
* 将基本数据类型转换为byte数组,以及反向转换的方法
* 只涉及转换操作,对于参数没有进行校验
* 适用范围:RMS操作、网络数据传输
*/
public class DataConvert{
/**
* 将int类型的数据转换为byte数组
* @param n int数据
* @return 生成的byte数组
*/
public static byte[] intToBytes(int n){
String s = String.valueOf(n);
return s.getBytes();
}
/**
* 将byte数组转换为int数据
* @param b 字节数组
* @return 生成的int数据
*/
public static int bytesToInt(byte[] b){
String s = new String(b);
return Integer.parseInt(s);
}
/**
* 将int类型的数据转换为byte数组
* 原理:将int数据中的四个byte取出,分别存储
* @param n int数据
* @return 生成的byte数组
*/
public static byte[] intToBytes2(int n){
byte[] b = new byte[4];
for(int i = 0;i < 4;i++){
b[i] = (byte)(n >> (24 - i * 8));
}
return b;
}
/**
* 将byte数组转换为int数据
* @param b 字节数组
* @return 生成的int数据
*/
public static int byteToInt2(byte[] b){
return (((int)b[0]) << 24) + (((int)b[1]) << 16) + (((int)b[2]) << 8) + b[3];
}
}
* 将基本数据类型转换为byte数组,以及反向转换的方法
* 只涉及转换操作,对于参数没有进行校验
* 适用范围:RMS操作、网络数据传输
*/
public class DataConvert{
/**
* 将int类型的数据转换为byte数组
* @param n int数据
* @return 生成的byte数组
*/
public static byte[] intToBytes(int n){
String s = String.valueOf(n);
return s.getBytes();
}
/**
* 将byte数组转换为int数据
* @param b 字节数组
* @return 生成的int数据
*/
public static int bytesToInt(byte[] b){
String s = new String(b);
return Integer.parseInt(s);
}
/**
* 将int类型的数据转换为byte数组
* 原理:将int数据中的四个byte取出,分别存储
* @param n int数据
* @return 生成的byte数组
*/
public static byte[] intToBytes2(int n){
byte[] b = new byte[4];
for(int i = 0;i < 4;i++){
b[i] = (byte)(n >> (24 - i * 8));
}
return b;
}
/**
* 将byte数组转换为int数据
* @param b 字节数组
* @return 生成的int数据
*/
public static int byteToInt2(byte[] b){
return (((int)b[0]) << 24) + (((int)b[1]) << 16) + (((int)b[2]) << 8) + b[3];
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=551450
- android 将int转byte,byte转int的两种方法
- android 将int转byte,byte转int的两种方法
- android的byte转int
- Java中byte转int的方法
- int转byte[],byte[]转int
- byte[] 转int ,int转byte[]
- int转byte[],byte[]转int
- byte[]转int以及int转byte[]
- int 转byte
- int 转byte[]
- int 转byte[]互转
- int转byte
- int强转byte
- Java byte转int
- android中int转byte[],double转byte[]
- BYTE转int,BYTE转CString
- 数据类型int跟byte[]数组互转的方法!
- int转4个byte,byte的拼接
- Ogre内部渲染流程分析系列
- mysql
- PC并没有没落 手机移动市场分析
- Oracle中RAW和Varchar2常用的两个转换函数
- oracle行转列组合成字符串函数
- android 将int转byte,byte转int的两种方法
- CListCtrl 使用技巧
- PHP中的数字与字符串比较
- Tip:自定义标签简介
- 设计模式学习笔记:行为型模式
- 页面传值提交ACTION的方法 两种 一
- C#正则表达式小结
- object与embed的区别,嵌入flash
- 提交ACTION的两种传值方式 二