- 浏览: 151794 次
- 性别:
- 来自: 北京
最新评论
-
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 882/****************************** ... -
Java正则表达式应用总结
2012-05-25 12:23 1103一、概述 正则表达式是Java处理字符串、文本的重要工具。 ... -
JdbcTemplate与事务
2012-05-17 17:42 1127JdbcTemplate与事务 上例中的JdbcTemplat ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-05-09 17:59 928最近的机器内存又爆满 ... -
jconsole远程监控Java进程
2012-05-07 11:44 1078JDK中的工具jconsole可以很好地监控Java进程及其运 ... -
spring集成quartz
2012-04-16 15:56 2223首先,让spring框架运转起来,可以参看一下:ht ... -
JMX RMI 访问
2011-09-02 10:46 4524RMI(Remote Method Invocation) R ... -
采用开发框架quartz调度管理Job
2011-07-11 10:03 19611.所需要的第三方包:quartz-1.5.2.jarcom ... -
java类型转换
2011-05-20 17:13 914string和int之间的转换? 字符串转换成数据 ... -
线程安全总结(二)
2010-11-12 10:34 853站内很多人都问我,所谓线程的“工作内存”到底是个什么东西? ... -
java线程安全总结
2010-11-12 10:33 838java线程安全总结(二 ... -
ora-02289问题解决
2010-10-19 12:35 1647<id name="id" type ... -
JDBC的批处理操作三种方式 pstmt.addBatch();
2010-09-25 15:58 8718SQL批处理是JDBC性能优化的重要武器,经本人研究总结,批处 ... -
log4j输出多个自定义日志文件
2010-05-12 10:28 1518<转>http://hi.baidu.com/ ... -
spring任务调度
2010-04-28 09:48 1411概述 在JDK 1.3以后的版本中,Java通过java.ut ... -
JDK线程池的使用
2010-04-07 16:35 1460一、简介 线程池类为 j ... -
Java文件操作
2010-02-06 15:29 874本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建 ... -
[JMX一步步来] 6、Model Bean
2009-12-21 11:46 1213在上一节是用apache的commons-modeler来 ... -
[JMX一步步来] 5、用Apache的commons-modeler来辅助开发JMX
2009-12-21 11:45 1081一、前言 每一个MBean都要有一个接口,比如前面的Hello ... -
[JMX一步步来] 4、动态MBean:DynamicMBean
2009-12-21 11:37 1531一、前言 动态MBean是在运行期才定义它的属性和方法 ...
相关推荐
"Java整型数与网络字节序byte[]数组转换关系详解" 本文主要介绍了Java整型数与网络字节序byte[]数组之间的转换关系,包括整型数到byte[]数组的转换和byte[]数组到整型数的转换。这些转换关系在Java和C/C++之间的...
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
Byte数组转换为Short值 最后,`byte`数组也可以转换回相应的基本类型。例如,将2字节的`byte`数组转换为`short`值: ```java public static short byteArrToShort(byte[] b) { byte[] a = new byte[2]; int i =...
相反,从byte数组转换回int类型则需要将这些字节合并起来。 **转换方法:** 1. **byte转int**:将一个byte转换为int,可以通过与0xFF进行按位与操作(AND),然后根据位移规则进行左移或右移操作。例如,`byte b =...
写一个方法,将int类型转换为字节数组,输入任意int类型整型,输出字节数组;写第二个方法,输入字节数组,输出对应int类型数据。
总结一下,Java中的基本类型与字节数组的转换涉及到一系列的方法和类,包括`ByteBuffer`, `CharsetEncoder`, `DataInputStream`, 和 `DataOutputStream`。理解这些工具的使用方式和注意事项对于处理二进制数据至关...
上述代码提供了一些静态方法,用于在基本类型与byte数组之间进行转换。我们逐一分析这些方法: 1. `getBytes(short data)` 和 `getShort(byte[] bytes)` - 这两个方法处理short类型。`getBytes(short data)` 将...
上述代码片段提供了一些在Java中实现整型数(long类型)与网络字节序的字节数组之间转换的方法。以下是这些方法的详细说明: 1. `longToBytes(long n)`:这个静态方法将一个长整型(long)数值`n`转换为一个8字节的...
在处理二进制数据时,特别是网络通信或序列化过程中,我们经常需要将整型(int)数据转换为字节数组(byte[]),反之亦然。本文将详细讲解如何在Java中进行这两种类型的转换,并解释相关原理。 1. **int类型转byte[]** ...
- 图片与byte数组的互相转换,通常涉及图像编码和解码过程。 - 字节数组与整型(int)、长整型(long)、短整型(short)和字节类型(byte)之间的转换,需要根据字节顺序和位运算进行操作。 - 字符串与字节数组的...
在Java编程中,有时我们需要将整型(int)和长整型(long)的数据转换为字节(byte),这在处理网络传输、二进制序列化或内存优化等场景中尤其常见。以下是一些关于如何在Java中进行这些转换的方法。 首先,让我们看下...
在Java编程语言中,String类是处理文本字符串的核心类,而与各种进制字符之间的转换是常见的编程需求。本文将详细探讨Java中如何进行String与二进制、八进制、十进制以及十六进制之间的转换。 首先,我们要了解进制...
这个方法首先将字节数组转换为16进制字符串,然后解析为长整型,以得到无符号的4字节整数值。 需要注意的是,这里的无符号整数转换是基于Long类型进行的,因为无符号的32位整数超过了Integer的范围(-2^31到2^31-1...
例如,以下是如何将一个`byte`数组转换为8位、16位和32位有符号整数: ```csharp byte[] data = new byte[] { 0xF8, 0x66, 0x55, 0x44 }; // 假设这是一个4字节的数据 // 转换为8位有符号整数(sbyte) sbyte sb =...
相反,从byte数组转换回int,我们需要组合这些字节,将它们按正确的顺序放置,然后将结果解释为int。以下是一个实现此功能的函数: ```java public static int bytesToInt(byte[] bytes) { int i; i = (int) (...
- 字节类型(byte)与 Byte 类 - 短整型(short)与 Short 类 - 整型(int)与 Integer 类 - 长整型(long)与 Long 类 - 单精度浮点型(float)与 Float 类 - 双精度浮点型(double)与 Double 类 - 布尔型...
字节数组到整型转换是将字节数组转换为整形数据的过程。这个过程可以通过位运算符来实现,例如: `public static int byteToInt(byte[] b) { return ((((b[0] & 0xff) ) | ((b[1] & 0xff) ) | ((b[2] & 0xff) ) | ...
3. **字节到字符串**: 使用`new String(byte[], charset)`构造函数,指定字符编码(如UTF-8)将字节数组转换为字符串。 4. **字符串到字节**: `getBytes(charset)`方法将字符串转换为其字节表示。 接下来,CRC32...
此方法则是将一个由8个`byte`组成的数组转换回`double`类型的浮点数。这里使用了与`doubleToByte`相反的过程,即通过组合这8个字节形成一个`long`值,并利用`Double.longBitsToDouble`方法将其转换回`double`值。 `...
一种常见的做法是在将`byte`转换为整型(如`int`)时,进行按位与操作,使用常量0xFF。例如,`byte b = 0b1000_0111; int ipSegment = b & 0xFF;`这里的`0xFF`是一个无符号的8位二进制数,与`byte`进行按位与操作后...