byte[]到short、int、long的相互转换
2006-03-09 20:03
459 查看
public final static byte[] getBytes(short s, boolean asc) {
byte[] buf = new byte[2];
if (asc) for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
return buf;
}
public final static byte[] getBytes(int s, boolean asc) {
byte[] buf = new byte[4];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
return buf;
}
public final static byte[] getBytes(long s, boolean asc) {
byte[] buf = new byte[8];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
return buf;
}
public final static short getShort(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 2) {
throw new IllegalArgumentException("byte array size > 2 !");
}
short r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
return r;
}
public final static int getInt(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 4) {
throw new IllegalArgumentException("byte array size > 4 !");
}
int r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
return r;
}
public final static long getLong(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 8) {
throw new IllegalArgumentException("byte array size > 8 !");
}
long r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
return r;
}
byte[] buf = new byte[2];
if (asc) for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00ff);
s >>= 8;
}
return buf;
}
public final static byte[] getBytes(int s, boolean asc) {
byte[] buf = new byte[4];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x000000ff);
s >>= 8;
}
return buf;
}
public final static byte[] getBytes(long s, boolean asc) {
byte[] buf = new byte[8];
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
else
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (s & 0x00000000000000ff);
s >>= 8;
}
return buf;
}
public final static short getShort(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 2) {
throw new IllegalArgumentException("byte array size > 2 !");
}
short r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00ff);
}
return r;
}
public final static int getInt(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 4) {
throw new IllegalArgumentException("byte array size > 4 !");
}
int r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x000000ff);
}
return r;
}
public final static long getLong(byte[] buf, boolean asc) {
if (buf == null) {
throw new IllegalArgumentException("byte array is null!");
}
if (buf.length > 8) {
throw new IllegalArgumentException("byte array size > 8 !");
}
long r = 0;
if (asc)
for (int i = buf.length - 1; i >= 0; i--) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
else
for (int i = 0; i < buf.length; i++) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
return r;
}
相关文章推荐
- byte[]到short、int、long的相互转换
- byte[]到short、int、long的相互转换
- Java中有关byte和String的转换笔记和byte[]到short、int、long的相互转换
- [ZZ]byte[]到short、int、long的相互转换
- byte[]到short、int、long的相互转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- 测试整数类型:byte,short,int,long。以及进制之间的转换问题
- java Byte和各数据类型(short,int,long,float,double)之间的转换
- java byte数组与int,long,short,byte转换
- java byte数组与int,long,short,byte的转换实现方法
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- java中int、long与byte数组相互转换
- java中byte数组与int,long,short间的转换 .
- byte数组和long,int,short互相转换
- java中byte数组与int,long,short间的转换
- null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
- java中long,int,short与byte数组之间的转换
- java中byte数组与int,long,short间的转换