java整型数与网络字节序的 byte[] 数组转换关系

合集下载

Java中的基本数据类型和基本数据类型之间的转换

Java中的基本数据类型和基本数据类型之间的转换

Java中的基本数据类型和基本数据类型之间的转换在Java中有8中基本数据类型,分别为:整型: byte、short、int、long浮点型:float、double布尔型:boolean字符型:char.byte: 8位,封装类:Byte 1byte = 8bit; -128~127之间所有的整数 "位"是byte,"字节"是bit 2个字节表⽰⼀个字符。

声明举例:byte a = 1;short: 16位,短整型,封装类Short,范围在(-2^15) ~ (2^15)-1 之间 short 2byte = 16bit 声明举例:short a = 1;int : 32位,整型,封装类Integer ,范围在(-2^31) ~ (2^31)-1 之间 int 4byte = 32bit 默认整型直接量为int 声明举例:int a = 1;long: 64位,长整型,封装类Long,范围在(-2^63) ~ (2^63)-1 之间 ling 8byte = 64bit 声明举例:long a = 1L;或者 long a = 1l;float: 单精度浮点型,封装类Float, float 4byte = 4*8bit = 32bit 32位 声明举例:float a = 1.1f;或者float a = 1.1F;double:双精度浮点型,封装类Double double 8byte = 8*8bit = 64bit 64位 默认浮点型直接量为double 声明举例:double a = 1.1;char: 字符类型,封装类Character Java中采⽤unicode编码 char类型占⽤两个字节, java中的字符类型⽤单引号声明 声明举例:char a = 'a'; char a = 97; //ascii中97对应的字符为a char a = '中'; char a = '\u987f';boolean:布尔型,封装类Boolean 只能存储 true ,false------------------------------------------------------------------------------------------------数据类型之间的转换: ⾃动类型转换:byte --> short --> int --> long --> float --> double char-----^ ⼩的类型可以⾃动转换成⼤的类型 例如:double a = 1; //会⾃动转换成1.0 强制类型转换:⼤的类型转换成⼩的类型,会出现精度损失或者溢出 例如:double a = 1.0; float a1 = (float)a;----------------------------------------------------------------------------------------------------- 整型直接量可以直接赋值给byte,short,char,只要不超过其范围就可以byte,short,char参与运算时,会先统⼀转换成int类型,然后再运算 整数之间相除结果还是整数,⼩数舍去 不同数据类型之间运算,会先转换成⼤的数据类型,然后再运算 double计算时会出现舍⼊差,2进制系统中⽆法精确的表⽰1/10,就好像⼗进制中⽆法精确表⽰1/3⼀ 样。

java中int与byte,以及long与byte之间的转换

java中int与byte,以及long与byte之间的转换

java中int与byte,以及long与byte之间的转换原⽂地址:/zgyulongfei/article/details/7738970public class Utilities {public static byte[] int2Bytes(int num) {byte[] byteNum = new byte[4];for (int ix = 0; ix < 4; ++ix) {int offset = 32 - (ix + 1) * 8;byteNum[ix] = (byte) ((num >> offset) & 0xff);}return byteNum;}public static int bytes2Int(byte[] byteNum) {int num = 0;for (int ix = 0; ix < 4; ++ix) {num <<= 8;num |= (byteNum[ix] & 0xff);}return num;}public static byte int2OneByte(int num) {return (byte) (num & 0x000000ff);}public static int oneByte2Int(byte byteNum) {return byteNum > 0 ? byteNum : (128 + (128 + byteNum));}public static byte[] long2Bytes(long num) {byte[] byteNum = new byte[8];for (int ix = 0; ix < 8; ++ix) {int offset = 64 - (ix + 1) * 8;byteNum[ix] = (byte) ((num >> offset) & 0xff);}return byteNum;}public static long bytes2Long(byte[] byteNum) {long num = 0;for (int ix = 0; ix < 8; ++ix) {num <<= 8;num |= (byteNum[ix] & 0xff);}return num;}public static void main(String[] args) {int num = 129;System.out.println("测试的int值为:" + num);byte[] int2bytes = Utilities.int2Bytes(num);System.out.printf("int转成bytes: ");for (int i = 0; i < 4; ++i) {System.out.print(int2bytes[i] + " ");}System.out.println();int bytes2int = Utilities.bytes2Int(int2bytes);System.out.println("bytes转⾏成int: " + bytes2int);byte int2OneByte = Utilities.int2OneByte(num);System.out.println("int转⾏成one byte: " + int2OneByte);int oneByte2Int = Utilities.oneByte2Int(int2OneByte);System.out.println("one byte转⾏成int: " + oneByte2Int);System.out.println();long longNum = 100000;System.out.println("测试的long值为:" + longNum);byte[] long2Bytes = Utilities.long2Bytes(longNum);System.out.printf("long转⾏成bytes: ");for (int ix = 0; ix < long2Bytes.length; ++ix) {System.out.print(long2Bytes[ix] + " ");}System.out.println();long bytes2Long = Utilities.bytes2Long(long2Bytes); System.out.println("bytes转⾏成long: " + bytes2Long); }}测试的int值为:129int转成bytes: 0 0 0 -127bytes转⾏成int: 129int转⾏成one byte: -127one byte转⾏成int: 129测试的long值为:100000long转⾏成bytes: 0 0 0 0 0 1 -122 -96bytes转⾏成long: 100000。

java中long,int,short与byte数组之间的转换

java中long,int,short与byte数组之间的转换

java中long,int,short与byte数组之间的转换//long类型转成byte数组public static byte[] longToByte(long number) {long temp = number;byte[] b = new byte[8];for (int i = 0; i < b.length; i++) {b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位temp = temp >> 8; // 向右移8位}return b;}//byte数组转成longpublic static long byteToLong(byte[] b) {long s = 0;long s0 = b[0] & 0xff;// 最低位long s1 = b[1] & 0xff;long s2 = b[2] & 0xff;long s3 = b[3] & 0xff;long s4 = b[4] & 0xff;// 最低位long s5 = b[5] & 0xff;long s6 = b[6] & 0xff;long s7 = b[7] & 0xff;// s0不变s1 <<= 8;s2 <<= 16;s3 <<= 24;s4 <<= 8 * 4;s5 <<= 8 * 5;s6 <<= 8 * 6;s7 <<= 8 * 7;s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;return s;}/*** 注释:int到字节数组的转换!** @param number* @return*/public static byte[] intToByte(int number) {int temp = number;byte[] b = new byte[4];for (int i = 0; i < b.length; i++) {b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位temp = temp >> 8; // 向右移8位}return b;}/*** 注释:字节数组到int的转换!** @param b* @return*/public static int byteToInt(byte[] b) {int s = 0;int s0 = b[0] & 0xff;// 最低位int s1 = b[1] & 0xff;int s2 = b[2] & 0xff;int s3 = b[3] & 0xff;s3 <<= 24;s2 <<= 16;s1 <<= 8;s = s0 | s1 | s2 | s3;return s;}/*** 注释:short到字节数组的转换!** @param s* @return*/public static byte[] shortToByte(short number) {int temp = number;byte[] b = new byte[2];for (int i = 0; i < b.length; i++) {b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位temp = temp >> 8; // 向右移8位}return b;}/*** 注释:字节数组到short的转换!** @param b* @return*/public static short byteToShort(byte[] b) { short s = 0;short s0 = (short) (b[0] & 0xff);// 最低位 short s1 = (short) (b[1] & 0xff); s1 <<= 8;s = (short) (s0 | s1);return s;}。

java 基本类型 转 byte 原理

java 基本类型 转 byte 原理

java 基本类型转byte 原理摘要:1.Java基本数据类型概述2.byte类型与其它基本数据类型的关系3.Java中byte转float的原理4.实际应用场景及注意事项正文:Java基本数据类型分为字节型(byte)、短整型(short)、整型(int)、长整型(long)、单精度浮点型(float)、双精度浮点型(double)、布尔型(boolean)和字符型(char)。

在这些类型之间,可以进行相互转换。

本文将重点介绍byte类型与其他类型之间的转换,特别是byte转float的原理及其应用场景。

首先,让我们了解一下byte类型。

在Java中,byte类型是8位的,即1个字节。

它的默认值为0,取值范围为-128至127。

与其他类型相比,byte 类型的数值范围较小,但在某些场景下,如处理IO、网络传输等,byte类型具有优势。

接下来,我们探讨一下byte类型与其他类型的关系。

在Java中,不同数据类型之间可以进行转换,但需要注意以下几点:1.整型转换:byte类型可以转换为int、long类型,但反之则需要进行强制转换。

2.浮点型转换:byte类型可以转换为float、double类型,但反之则需要进行强制转换。

3.布尔型转换:byte类型可以转换为boolean类型,但反之则需要进行强制转换。

4.字符型转换:byte类型可以转换为char类型,但反之则需要进行强制转换。

在上述转换中,byte转float的原理尤为重要。

在Java中,byte转float 的过程实质上是将byte数值转换为float数值。

具体来说,首先将byte数值转换为int类型,然后将int类型转换为float类型。

以下是一个示例:```javabyte b = 10;float f = (float) (b & 0xFF);```在上面的代码中,我们首先将byte类型的变量b转换为int类型,然后将int类型转换为float类型。

java中byte,byte[]和int之间的转换

java中byte,byte[]和int之间的转换

java中byte,byte[]和int之间的转换1>byte类型转换为,直接隐式转换,适⽤于要求保持数值不变,例如要求进⾏数值计算如 byte b=0x01; int i=b;2>另⼀种是要求保持最低字节中各个位不变,3个⾼字节全部⽤0填充,例如进⾏编解码操作,则需要采⽤位操作,int i=b & 0xff;3>byte[]数组和int之间的转换// 将byte[]转换为int类型public static int byte2int(byte[] res) {// ⼀个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表⽰安位或| ((res[2] << 24) >>> 8) | (res[3] << 24);return targets;}// 将int类型装换为byte[]数组public static byte[] int2byte(int res) {byte[] targets = new byte[4];targets[0] = (byte) (res & 0xff);// 最低位targets[1] = (byte) ((res >> 8) & 0xff);// 次低位targets[2] = (byte) ((res >> 16) & 0xff);// 次⾼位targets[3] = (byte) (res >>> 24);// 最⾼位,⽆符号右移。

return targets;}。

java中int与byte相互转换

java中int与byte相互转换

java中int与byte相互转换1:int和byte的关系 在java中,int整形变量是32位的,⽽byte是8位的,他们之间的转换有⼀定的策略和讲究。

1.1:int 到byte⾸先我们实现int和byte之间的转换,思路如下:1. 创建⼀个byte数组,长度为4。

byte[0]是最⾼位,byte[1]是次⾼位,byte[2]是次次⾼位,byte[3]是最低位,2. 在将int数据右移24位,然后与0xFF相与即可得到byte[0]。

3. 在将int数据右移16位,然后与0xFF相与即可得到byte[1]。

4. 在将int数据右移8位,然后与0xFF相与即可得到byte[2]。

5. 在将int数据右移0位,然后与0xFF相与即可得到byte[3]。

6. 输出byte[]数组即可。

1.2:byte 到int⾸先我们实现byte和int之间的转换,思路如下:1. 输⼊⼀个byte数组,长度为4。

byte[0]是最⾼位,byte[1]是次⾼位,byte[2]是次次⾼位,byte[3]是最低位,2. 创建⼀个int型变量sum,作为byte到int的结果。

3. ⽤⼀个for(int i=0;i<4;i++)的循环。

每次将(3-i)*8的值,作为该byte的权重。

4. sum = sum + byte[i]<<(3-i)*8。

重复3-4即可。

5. return sum即可。

2:代码实现/*** int到byte[] 由⾼位到低位* @param i 需要转换为byte数组的整⾏值。

* @return byte数组*/public static byte[] intToByteArray(int i) {byte[] result = new byte[4];result[0] = (byte)((i >> 24) & 0xFF);result[1] = (byte)((i >> 16) & 0xFF);result[2] = (byte)((i >> 8) & 0xFF);result[3] = (byte)(i & 0xFF);return result;}/*** byte[]转int* @param bytes 需要转换成int的数组* @return int值*/public static int byteArrayToInt(byte[] bytes) {int value=0;for(int i = 0; i < 4; i++) {int shift= (3-i) * 8;value +=(bytes[i] & 0xFF) << shift;}return value;}。

byte数组与byte数组转化

byte数组与byte数组转化

byte数组与byte数组转化byte数组与byte数组之间的转化是在编程中经常遇到的需求。

有时候,我们需要将一个byte数组转化为另一个byte数组来满足我们的需要。

这些需要包括但不限于:网络传输数据、数据加密、数据压缩等等。

在本文中,我将讨论如何在Java中将byte数组转化为另一个byte数组。

在Java中,byte数组是存储二进制数据的一种数据类型。

它由固定长度的字节组成,每个字节都保存了一个8位的二进制值。

因此,byte数组可以表示任何二进制数据,如图像、音频、视频等。

要将一个byte数组转化为另一个byte数组,我们可以使用Java提供的一些方法和技巧。

下面是一些可供参考的方法和思路:1. 使用System.arraycopy()方法:这是Java中最常用的将一个byte数组复制到另一个byte数组的方法之一。

该方法可以在两个数组之间执行快速的内存复制,以达到将一个数组复制到另一个数组的目的。

以下是使用System.arraycopy()方法的示例代码:```javabyte[] sourceArray = {1, 2, 3, 4, 5};byte[] destinationArray = new byte[sourceArray.length];System.arraycopy(sourceArray, 0, destinationArray, 0,sourceArray.length);```在上面的代码中,我们首先创建了一个源数组sourceArray 和一个目标数组destinationArray。

然后,我们使用System.arraycopy()方法将源数组的内容复制到目标数组中。

2. 使用ByteArrayOutputStream和ByteArrayInputStream:Java中的ByteArrayOutputStream和ByteArrayInputStream类提供了用于在内存中生成和处理字节数组的功能。

Java中字符串与byte数组之间的相互转换

Java中字符串与byte数组之间的相互转换

Java中字符串与byte数组之间的相互转换前⾔Java与其他语⾔编写的程序进⾏tcp/ip socket通讯时,通讯内容⼀般都转换成byte数组型,java在字符与数组转换也是⾮常⽅便的。

下⾯跟我⼀起来了解⼀下字符串与byte之间转换的原理原理我们都知道,在Java⾥byte类型是占⽤1个字节,即8位的,⽽16进制的字符占⽤4位,所以每个byte可以⽤两个字符来表⽰,反之亦然。

举个例⼦byte = 123⽤⼆进制表⽰:0111 1011每4位⽤字符表⽰:7 b是的,原理就这么简单,接下来⽤代码实现:byte[] 转16进制字符串⽅法⼀思路:先把byte[] 转换维char[],再把char[]转换为字符串public static String bytes2Hex(byte[] src) {if (src == null || src.length <= 0) {return null;}char[] res = new char[src.length * 2]; // 每个byte对应两个字符final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };for (int i = 0, j = 0; i < src.length; i++) {res[j++] = hexDigits[src[i] >> 4 & 0x0f]; // 先存byte的⾼4位res[j++] = hexDigits[src[i] & 0x0f]; // 再存byte的低4位}return new String(res);}⽅法⼆思路:先把byte转换为int类型,再转换为字符串public static String bytesToHex(byte[] src){if (src == null || src.length <= 0) {return null;}StringBuilder stringBuilder = new StringBuilder("");for (int i = 0; i < src.length; i++) {// 之所以⽤byte和0xff相与,是因为int是32位,与0xff相与后就舍弃前⾯的24位,只保留后8位String str = Integer.toHexString(src[i] & 0xff);if (str.length() < 2) { // 不⾜两位要补0stringBuilder.append(0);}stringBuilder.append(str);}return stringBuilder.toString();}16进制字符串转byte[]思路:先把字符串转换为char[] ,再转换为byte[]public static byte[] hexToBytes(String hexString) {if (hexString == null || hexString.equals("")) {return null;}int length = hexString.length() / 2;char[] hexChars = hexString.toCharArray();byte[] bytes = new byte[length];String hexDigits = "0123456789abcdef";for (int i = 0; i < length; i++) {int pos = i * 2; // 两个字符对应⼀个byteint h = hexDigits.indexOf(hexChars[pos]) << 4; // 注1int l = hexDigits.indexOf(hexChars[pos + 1]); // 注2if(h == -1 || l == -1) { // ⾮16进制字符return null;}bytes[i] = (byte) (h | l);}return bytes;}注:注1得到xxxx0000,注2得到0000xxxx,相或就把两个字符转换为⼀个byte了。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java整型数与网络字节序的byte[] 数组转换关系工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。

如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。

而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。

本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte 数组)之间转换方法。

实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对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 longT oBytes( 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[] uintT oBytes( 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 uintT oBytes( 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 shortT oBytes(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[] 数组之间的转换关系还需继续研究实现。

相关文档
最新文档