java中byte与int互转
2015-06-09 14:45
288 查看
package com.yl.common.utils;
/**
* byte转换工具
*
* @author huangzp
* @date 2015-6-09
*/
public
class ByteUtil {
/**
* 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位
* @param iSource
* @param iArrayLen
* @return
*/
public
static byte[] toByteArray(int iSource, int iArrayLen) {
byte
[] bLocalArr = new byte[iArrayLen];
for
(int i = 0; (i < 4) && (i < iArrayLen); i++) {
bLocalArr[i] = (byte
) (iSource >> 8 * i & 0xFF);
}
return
bLocalArr;
}
/**
* 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
* @param bRefArr
* @return
*/
public
static int toInt(byte[] bRefArr) {
int
iOutcome = 0;
byte
bLoop;
for
(int i = 0; i < 4; i++) {
bLoop = bRefArr[i];
iOutcome += (bLoop & 0xFF
) << (8 * i);
}
return
iOutcome;
}
}
package
com.yl.common.utils;
/**
* byte转换工具
*
* @author huangzp
* @date 2015-6-09
*/
public
class ByteUtil {
/**
* 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位
* @param iSource
* @param iArrayLen
* @return
*/
public
static byte[] toByteArray(int iSource, int iArrayLen) {
byte
[] bLocalArr = new byte[iArrayLen];
for
(int i = 0; (i < 4) && (i < iArrayLen); i++) {
bLocalArr[i] = (byte
) (iSource >> 8 * i & 0xFF);
}
return
bLocalArr;
}
/**
* 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
* @param bRefArr
* @return
*/
public
static int toInt(byte[] bRefArr) {
int
iOutcome = 0;
byte
bLoop;
for
(int i = 0; i < 4; i++) {
bLoop = bRefArr[i];
iOutcome += (bLoop & 0xFF
) << (8 * i);
}
return
iOutcome;
}
}
原文地址 http://techfoxbbs.com/blog-1-5.html
/**
* byte转换工具
*
* @author huangzp
* @date 2015-6-09
*/
public
class ByteUtil {
/**
* 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位
* @param iSource
* @param iArrayLen
* @return
*/
public
static byte[] toByteArray(int iSource, int iArrayLen) {
byte
[] bLocalArr = new byte[iArrayLen];
for
(int i = 0; (i < 4) && (i < iArrayLen); i++) {
bLocalArr[i] = (byte
) (iSource >> 8 * i & 0xFF);
}
return
bLocalArr;
}
/**
* 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
* @param bRefArr
* @return
*/
public
static int toInt(byte[] bRefArr) {
int
iOutcome = 0;
byte
bLoop;
for
(int i = 0; i < 4; i++) {
bLoop = bRefArr[i];
iOutcome += (bLoop & 0xFF
) << (8 * i);
}
return
iOutcome;
}
}
package
com.yl.common.utils;
/**
* byte转换工具
*
* @author huangzp
* @date 2015-6-09
*/
public
class ByteUtil {
/**
* 将iSource转为长度为iArrayLen的byte数组,字节数组的低位是整型的低字节位
* @param iSource
* @param iArrayLen
* @return
*/
public
static byte[] toByteArray(int iSource, int iArrayLen) {
byte
[] bLocalArr = new byte[iArrayLen];
for
(int i = 0; (i < 4) && (i < iArrayLen); i++) {
bLocalArr[i] = (byte
) (iSource >> 8 * i & 0xFF);
}
return
bLocalArr;
}
/**
* 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位
* @param bRefArr
* @return
*/
public
static int toInt(byte[] bRefArr) {
int
iOutcome = 0;
byte
bLoop;
for
(int i = 0; i < 4; i++) {
bLoop = bRefArr[i];
iOutcome += (bLoop & 0xFF
) << (8 * i);
}
return
iOutcome;
}
}
原文地址 http://techfoxbbs.com/blog-1-5.html
相关文章推荐
- java权限设计思路之一
- java中byte与int互转
- java中byte与int互转
- Java泛型 通配符的限定
- java的类加载器ClassLoader
- java自动获得电脑网络配置并存储在beanUtil中
- java的类加载器ClassLoader
- JAVA 中的 Collection FrameWork
- 修改eclipse/MyEclipse中项目包的显示结构
- java集合类都有哪些?主要方法?
- Spring学习笔记之Spring与JDBC整合
- java的类加载器ClassLoader
- java设定窗口步长,依次统计窗口内数值总和
- java设定窗口步长,依次统计窗口内数值总和
- springmvc快速入门
- JAVA字符串分割,两个分隔符的,怎么实现
- android studio 快捷键设置为eclipse的习惯
- spring MVC 2-helloword 入门
- JavaBean的详细及引用
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发