- 浏览: 147829 次
- 性别:
- 来自: 北京
最新评论
-
pandengzhegt:
好牛!正需要!谢谢了!
JPA 2.0 中的动态类型安全查询 -
yanlp:
万分的感谢。
仿google 的输入提示框 -
huangwenji6111:
可谓良师,在此拜谢!受益匪浅!
hibernate lazy -
jwx0925:
不错!mark!
hibernate对象状态 -
leftstick:
大有裨益,谢了!
hibernate lazy
因工作需要在java和c/c++之间进行socket通信,而socket通信是以字节流或者字节包进行的(这相当于java的byte[]数组),所以需要在java数据类型和网络字节流(包)之间进行转换。网上这方面的资料不少,但往往不全,甚至有些有错漏。于是自己花时间对java整型数和网络字节序的byte[]之间的转换的各种情况做了一些验证和整理。整理出来的函数如下:
public class ByteConvert {
// 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换
public static byte[] longToBytes(long n) {
byte[] b = new byte[8];
b[7] = (byte) (n & 0xff);
b[6] = (byte) (n >> 8 & 0xff);
b[5] = (byte) (n >> 16 & 0xff);
b[4] = (byte) (n >> 24 & 0xff);
b[3] = (byte) (n >> 32 & 0xff);
b[2] = (byte) (n >> 40 & 0xff);
b[1] = (byte) (n >> 48 & 0xff);
b[0] = (byte) (n >> 56 & 0xff);
return b;
}
public static void longToBytes( long n, byte[] array, int offset ){
array[7+offset] = (byte) (n & 0xff);
array[6+offset] = (byte) (n >> 8 & 0xff);
array[5+offset] = (byte) (n >> 16 & 0xff);
array[4+offset] = (byte) (n >> 24 & 0xff);
array[3+offset] = (byte) (n >> 32 & 0xff);
array[2+offset] = (byte) (n >> 40 & 0xff);
array[1+offset] = (byte) (n >> 48 & 0xff);
array[0+offset] = (byte) (n >> 56 & 0xff);
}
public static long bytesToLong( byte[] array )
{
return ((((long) array[ 0] & 0xff) << 56)
| (((long) array[ 1] & 0xff) << 48)
| (((long) array[ 2] & 0xff) << 40)
| (((long) array[ 3] & 0xff) << 32)
| (((long) array[ 4] & 0xff) << 24)
| (((long) array[ 5] & 0xff) << 16)
| (((long) array[ 6] & 0xff) << 8)
| (((long) array[ 7] & 0xff) << 0));
}
public static long bytesToLong( byte[] array, int offset )
{
return ((((long) array[offset + 0] & 0xff) << 56)
| (((long) array[offset + 1] & 0xff) << 48)
| (((long) array[offset + 2] & 0xff) << 40)
| (((long) array[offset + 3] & 0xff) << 32)
| (((long) array[offset + 4] & 0xff) << 24)
| (((long) array[offset + 5] & 0xff) << 16)
| (((long) array[offset + 6] & 0xff) << 8)
| (((long) array[offset + 7] & 0xff) << 0));
}
public static byte[] intToBytes(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
public static void intToBytes( int n, byte[] array, int offset ){
array[3+offset] = (byte) (n & 0xff);
array[2+offset] = (byte) (n >> 8 & 0xff);
array[1+offset] = (byte) (n >> 16 & 0xff);
array[offset] = (byte) (n >> 24 & 0xff);
}
public static int bytesToInt(byte b[]) {
return b[3] & 0xff
| (b[2] & 0xff) << 8
| (b[1] & 0xff) << 16
| (b[0] & 0xff) << 24;
}
public static int bytesToInt(byte b[], int offset) {
return b[offset+3] & 0xff
| (b[offset+2] & 0xff) << 8
| (b[offset+1] & 0xff) << 16
| (b[offset] & 0xff) << 24;
}
public static byte[] uintToBytes( long n )
{
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
public static void uintToBytes( long n, byte[] array, int offset ){
array[3+offset] = (byte) (n );
array[2+offset] = (byte) (n >> 8 & 0xff);
array[1+offset] = (byte) (n >> 16 & 0xff);
array[offset] = (byte) (n >> 24 & 0xff);
}
public static long bytesToUint(byte[] array) {
return ((long) (array[3] & 0xff))
| ((long) (array[2] & 0xff)) << 8
| ((long) (array[1] & 0xff)) << 16
| ((long) (array[0] & 0xff)) << 24;
}
public static long bytesToUint(byte[] array, int offset) {
return ((long) (array[offset+3] & 0xff))
| ((long) (array[offset+2] & 0xff)) << 8
| ((long) (array[offset+1] & 0xff)) << 16
| ((long) (array[offset] & 0xff)) << 24;
}
public static byte[] shortToBytes(short n) {
byte[] b = new byte[2];
b[1] = (byte) ( n & 0xff);
b[0] = (byte) ((n >> 8) & 0xff);
return b;
}
public static void shortToBytes(short n, byte[] array, int offset ) {
array[offset+1] = (byte) ( n & 0xff);
array[offset] = (byte) ((n >> 8) & 0xff);
}
public static short bytesToShort(byte[] b){
return (short)( b[1] & 0xff
|(b[0] & 0xff) << 8 );
}
public static short bytesToShort(byte[] b, int offset){
return (short)( b[offset+1] & 0xff
|(b[offset] & 0xff) << 8 );
}
public static byte[] ushortToBytes(int n) {
byte[] b = new byte[2];
b[1] = (byte) ( n & 0xff);
b[0] = (byte) ((n >> 8) & 0xff);
return b;
}
public static void ushortToBytes(int n, byte[] array, int offset ) {
array[offset+1] = (byte) ( n & 0xff);
array[offset] = (byte) ((n >> 8) & 0xff);
}
public static int bytesToUshort(byte b[]) {
return b[1] & 0xff
| (b[0] & 0xff) << 8;
}
public static int bytesToUshort(byte b[], int offset) {
return b[offset+1] & 0xff
| (b[offset] & 0xff) << 8;
}
public static byte[] ubyteToBytes( int n ){
byte[] b = new byte[1];
b[0] = (byte) (n & 0xff);
return b;
}
public static void ubyteToBytes( int n, byte[] array, int offset ){
array[0] = (byte) (n & 0xff);
}
public static int bytesToUbyte( byte[] array ){
return array[0] & 0xff;
}
public static int bytesToUbyte( byte[] array, int offset ){
return array[offset] & 0xff;
}
// char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。
}
测试程序如下:
public class ByteConvertTest {
public static String byte2Hex(byte[] buf)
{
StringBuffer strbuf = new StringBuffer();
strbuf.append("{");
for (byte b : buf)
{
if (b == 0)
{
strbuf.append("00");
}
else if (b == -1)
{
strbuf.append("FF");
}
else
{
String str = Integer.toHexString(b).toUpperCase();
// sb.append(a);
if (str.length() == 8)
{
str = str.substring(6, 8);
}
else if (str.length() < 2)
{
str = "0" + str;
}
strbuf.append(str);
}
strbuf.append(" ");
}
strbuf.append("}");
return strbuf.toString();
}
public static byte[] longToBytes(long n) {
byte[] b = new byte[8];
b[7] = (byte) (n & 0xff);
b[6] = (byte) (n >> 8 & 0xff);
b[5] = (byte) (n >> 16 & 0xff);
b[4] = (byte) (n >> 24 & 0xff);
b[3] = (byte) (n >> 32 & 0xff);
b[2] = (byte) (n >> 40 & 0xff);
b[1] = (byte) (n >> 48 & 0xff);
b[0] = (byte) (n >> 56 & 0xff);
return b;
}
public static long bytesToLong( byte[] array )
{
return ((((long) array[ 0] & 0xff) << 56)
| (((long) array[ 1] & 0xff) << 48)
| (((long) array[ 2] & 0xff) << 40)
| (((long) array[ 3] & 0xff) << 32)
| (((long) array[ 4] & 0xff) << 24)
| (((long) array[ 5] & 0xff) << 16)
| (((long) array[ 6] & 0xff) << 8)
| (((long) array[ 7] & 0xff) ));
}
public static int bytesToInt(byte b[]) {
return b[3] & 0xff
| (b[2] & 0xff) << 8
| (b[1] & 0xff) << 16
| (b[0] & 0xff) << 24;
}
public static long bytesToUint(byte[] array) {
return ((long) (array[3] & 0xff))
| ((long) (array[2] & 0xff)) << 8
| ((long) (array[1] & 0xff)) << 16
| ((long) (array[0] & 0xff)) << 24;
}
public static byte[] uintToBytes( long n )
{
byte[] b = new byte[4];
b[3] = (byte) (n & 0xff);
b[2] = (byte) (n >> 8 & 0xff);
b[1] = (byte) (n >> 16 & 0xff);
b[0] = (byte) (n >> 24 & 0xff);
return b;
}
public static byte[] shortToBytes(short n) {
byte[] b = new byte[2];
b[1] = (byte) ( n & 0xff);
b[0] = (byte) ((n >> 8) & 0xff);
return b;
}
public static short bytesToShort(byte[] b){
return (short)( b[1] & 0xff
|(b[0] & 0xff) << 8 );
}
static void testShortConvert(){
System.out.println("=================== short convert =============");
System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));
System.out.print("println 0x11f2:");
System.out.println((short)0x11f2);
System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));
System.out.print("println 0xf1f2:");
System.out.println((short)0xf1f2);
System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):");
System.out.println((short)bytesToShort(shortToBytes((short)0x11f2)));
System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):");
System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2)));
}
public static byte[] ushortToBytes(int n) {
byte[] b = new byte[2];
b[1] = (byte) (n & 0xff);
b[0] = (byte) (n >> 8 & 0xff);
return b;
}
public static int bytesToUshort(byte b[]) {
return b[1] & 0xff
| (b[0] & 0xff) << 8;
}
static void testUshortConvert(){
System.out.println("=================== Ushort convert =============");
System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2)));
System.out.print("println 0x11f2:");
System.out.println(0x11f2);
System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2)));
System.out.print("println 0xf1f2:");
System.out.println(0xf1f2);
System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):");
System.out.println(bytesToUshort(ushortToBytes(0x11f2)));
System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):");
System.out.println(bytesToUshort(ushortToBytes(0xf1f2)));
}
public static byte[] ubyteToBytes( int n ){
byte[] b = new byte[1];
b[0] = (byte) (n & 0xff);
return b;
}
public static int bytesToUbyte( byte[] array ){
return array[0] & 0xff;
}
static void testUbyteConvert(){
System.out.println("=================== Ubyte convert =============");
System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112)));
System.out.print("println 0x1112:");
System.out.println(0x1112);
System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2)));
System.out.print("println 0xf2:");
System.out.println(0xf2);
System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):");
System.out.println(bytesToUbyte(ubyteToBytes(0x1112)));
System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):");
System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2)));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] array = new byte[4];
array[3] = (byte) 0xF4;
array[2] = 0x13;
array[1] = 0x12;
array[0] = 0x11;
System.out.println("=================== Integer bytes =============");
System.out.println("the bytes is:"+byte2Hex(array) );
System.out.print("println bytesToInt :");
System.out.println( bytesToInt(array));
System.out.printf("printf bytesToInt :%X\n", bytesToInt(array));
System.out.println("=================== long bytes =============");
byte[] longBytes = new byte[8];
longBytes[7] = (byte) 0xf7;
longBytes[6] = (byte) 0x16;
longBytes[5] = (byte) 0xf5;
longBytes[4] = (byte) 0x14;
longBytes[3] = (byte) 0xf3;
longBytes[2] = (byte) 0x12;
longBytes[1] = (byte) 0xf1;
longBytes[0] = (byte) 0x10;
System.out.println( "the bytes is:"+byte2Hex(longBytes) );
System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes));
System.out.println("=================byte to long ================");
byte b = (byte)0xf1;
System.out.print("Println the byte:");
System.out.println(b);
System.out.printf("Printf the byte:%X\n",b);
long l = b;
System.out.print("Println byte to long:");
System.out.println(l);
System.out.printf("printf byte to long:%X\n",l);
System.out.println("================= uint Bytes ================");
byte[] uint = new byte[4];
uint[3] = (byte) 0xf3;
uint[2] = (byte) 0x12;
uint[1] = (byte) 0xf1;
uint[0] = (byte) 0xFF;
System.out.println( "the bytes is:"+byte2Hex(uint) );
System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint));
System.out.print("Println bytesToUint:");
System.out.println(bytesToUint(uint));
System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)));
System.out.println("===============Long Integer==============");
System.out.print("println 0x11f2f3f4f5f6f7f8l:");
System.out.println(0x11f2f3f4f5f6f7f8l);
System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l);
System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l)));
// 注意,下面的这行,并不能获得正确的uint。
System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l)));
System.out.println("===============bytesToLong(longToBytes())==============");
System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
testShortConvert();
testUshortConvert();
testUbyteConvert();
}
}
发表评论
-
java实现Tree
2012-07-10 09:59 831/****************************** ... -
Java正则表达式应用总结
2012-05-25 12:23 1038一、概述 正则表达式是Java处理字符串、文本的重要工具。 ... -
JdbcTemplate与事务
2012-05-17 17:42 1088JdbcTemplate与事务 上例中的JdbcTemplat ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-05-09 17:59 883最近的机器内存又爆满 ... -
jconsole远程监控Java进程
2012-05-07 11:44 1044JDK中的工具jconsole可以很好地监控Java进程及其运 ... -
spring集成quartz
2012-04-16 15:56 2183首先,让spring框架运转起来,可以参看一下:ht ... -
JMX RMI 访问
2011-09-02 10:46 4417RMI(Remote Method Invocation) R ... -
采用开发框架quartz调度管理Job
2011-07-11 10:03 19181.所需要的第三方包:quartz-1.5.2.jarcom ... -
java类型转换
2011-05-20 17:13 879string和int之间的转换? 字符串转换成数据 ... -
线程安全总结(二)
2010-11-12 10:34 822站内很多人都问我,所谓线程的“工作内存”到底是个什么东西? ... -
java线程安全总结
2010-11-12 10:33 798java线程安全总结(二 ... -
ora-02289问题解决
2010-10-19 12:35 1596<id name="id" type ... -
JDBC的批处理操作三种方式 pstmt.addBatch();
2010-09-25 15:58 8636SQL批处理是JDBC性能优化的重要武器,经本人研究总结,批处 ... -
log4j输出多个自定义日志文件
2010-05-12 10:28 1480<转>http://hi.baidu.com/ ... -
spring任务调度
2010-04-28 09:48 1376概述 在JDK 1.3以后的版本中,Java通过java.ut ... -
JDK线程池的使用
2010-04-07 16:35 1412一、简介 线程池类为 j ... -
Java文件操作
2010-02-06 15:29 847本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建 ... -
[JMX一步步来] 6、Model Bean
2009-12-21 11:46 1170在上一节是用apache的commons-modeler来 ... -
[JMX一步步来] 5、用Apache的commons-modeler来辅助开发JMX
2009-12-21 11:45 1050一、前言 每一个MBean都要有一个接口,比如前面的Hello ... -
[JMX一步步来] 4、动态MBean:DynamicMBean
2009-12-21 11:37 1495一、前言 动态MBean是在运行期才定义它的属性和方法 ...
相关推荐
主要介绍了Java整型数与网络字节序byte[]数组转换关系,结合实例形式归纳整理了java整型数和网络字节序的byte[]之间转换的各种情况,需要的朋友可以参考下
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
LabVIEW程序,功能:将4字节的unsigned char输入组合成1个32-bit int值,若输入字节数不等于4则报错。
写一个方法,将int类型转换为字节数组,输入任意int类型整型,输出字节数组;写第二个方法,输入字节数组,输出对应int类型数据。
实现 字符串,整型数据,字符型数组互相转换源码
C#各种数据类型转换,字符串 转换 char数组,...将Base64字符串解码为普通字符串,图片 转换 byte数组,byte数组 转换 图片,ip 转换 长整形,长整形 转换 IP,将8位日期型整型数据转换为日期字符串数据,string型转换为bool型,
下面小编就为大家带来一篇Java基本类型与byte数组之间相互转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
注意:源程序是为qt写的,不过基本上都用的是c语言,下载之后稍作修改就可以在vc++上运行。将int数组转成uchar数组保存到数据库,然后再取出来将uchar数组还原成int数组。
字节位数和float型转换小工具,可实现双向转换,占容小,速度快捷
C#开发,收到下位机串口数据(温度信息),可能是正数也可能是负数,如何转换?...补充知识:c# byte数组转换 8位有符号整数 16位有符号整数 32位有符号整数 byte数组 byte[] aa = new byte[] { 0xF8
定义20个元素的整型数组并赋初值。输入一个数,如果该数在数组中存在,则删除该数,其后的每个数前移一位,最后补0。
设a[0:n-1]是一个已排好序的数组。请改写二分搜索算法,使得当搜索元素x不在数组中时,返回小于x的最大元素的位置I和大于x的最大元素位置j
程序实现了整型数组到字符数组的相互转换功能
C# 将整型数组元素转换为字符串.别忘了给评分!
//声明一个整形数组 //声明一个字符串类型的数组 //创建数组 //声明数组的同时进行创建 //初始化数组 //循环为整型数组赋值 //循环输出整型数组中的元素 //求整形数组的累加和 //定义整形数组 //从键盘接收数据,...
简单的数组去重、虑空小工具。源码只是接受一个数组,把重复的,和空格去掉
该代码可以实现单精度浮点数和16进制字符之间的转换,包含源代码
有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。这是实现这一算法的具体代码: int temp = 0; //创建一个整型数组,长度为12 int [] arr = new int[12]; Random r = new Random();...
数组作为方法的形参 目录 课程导入 进一步熟悉方法的定义 学会编写方法,初始化数组 学会编写方法,输出数组元素 目录 课程导入 学会编写方法,求数组元素平均值 学会编写方法,实现数组元素逆序存储 理解数组作为...
符合IEEE754规则的Float类型单精度浮点数数据转换为四字节数组的实用小vi