您的位置:首页 > 其它

long 与byte类型间相互转换

2015-11-10 12:40 302 查看
package test;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Stest {
public static void main(String args[]) throws UnsupportedEncodingException {
// test for convertLongToBytes
long e = -1212;
byte[] b = convertLongToBytes(e);
for (byte c : b) {
System.out.println(c);
}
// test for convertLongToBytes
long ret = convertBytesToLong(b);
System.out.println(ret);

}
public static long convertBytesToLong(byte[] b) {
ByteBuffer buf = ByteBuffer.wrap(b);
return buf.getLong();
}

public static byte[] convertLongToBytes(long l) {
byte b[] = new byte[Long.SIZE];
ByteBuffer buf = ByteBuffer.wrap(b);
buf.putLong(l);
buf.flip();
return buf.array();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: