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();
}
}
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();
}
}
相关文章推荐
- 第十五天
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)
- ULK --- Chap3 Processes: Handling Wait Queues
- 9.6 列表框类
- 集合对象的内存管理、copy概念
- 如何在cocos2dx中使用Lua创建自定义
- 安装linux系统后修改默认开机启动项
- 用nodejs做一个svn密码修改页面
- jdk6版本下载地址
- Linq基础(一)
- 数据库性能优化三:程序操作优化
- 关于编程
- 延续性动词与非延续性动词及其转换
- Nutch 学习笔记2 - 查看抓取的文件
- 数据库性能优化二:数据库表优化
- 一个页面上让多个jQuery共存
- 八大排序,各显神通--动图版
- getRequestDispatcher()与sendRedirect()的区别