[Java] java byte数组与int,long,short,byte转换

  • public class DataTypeChangeHelper {
  • /**
  • * 将一个单字节的byte转换成32位的int
  • *
  • * @param b
  • *            byte
  • * @return convert result
  • */
  • public static int unsignedByteToInt(byte b) {
  • return (int) b & 0xFF;
  • }
  • /**
  • * 将一个单字节的Byte转换成十六进制的数
  • *
  • * @param b
  • *            byte
  • * @return convert result
  • */
  • public static String byteToHex(byte b) {
  • int i = b & 0xFF;
  • return Integer.toHexString(i);
  • }
  • /**
  • * 将一个4byte的数组转换成32位的int
  • *
  • * @param buf
  • *            bytes buffer
  • * @param byte[]中开始转换的位置
  • * @return convert result
  • */
  • public static long unsigned4BytesToInt(byte[] buf, int pos) {
  • int firstByte = 0;
  • int secondByte = 0;
  • int thirdByte = 0;
  • int fourthByte = 0;
  • int index = pos;
  • firstByte = (0x000000FF & ((int) buf[index]));
  • secondByte = (0x000000FF & ((int) buf[index + 1]));
  • thirdByte = (0x000000FF & ((int) buf[index + 2]));
  • fourthByte = (0x000000FF & ((int) buf[index + 3]));
  • index = index + 4;
  • return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
  • }
  • /**
  • * 将16位的short转换成byte数组
  • *
  • * @param s
  • *            short
  • * @return byte[] 长度为2
  • * */
  • public static byte[] shortToByteArray(short s) {
  • byte[] targets = new byte[2];
  • for (int i = 0; i < 2; i++) {
  • int offset = (targets.length - 1 - i) * 8;
  • targets = (byte) ((s >>> offset) & 0xff);
  •         }
  •         return targets;
  •     }
  •     /**
  •      * 将32位整数转换成长度为4的byte数组
  •      *
  •      * @param s
  •      *            int
  •      * @return byte[]
  •      * */
  •     public static byte[] intToByteArray(int s) {
  •         byte[] targets = new byte[2];
  •         for (int i = 0; i < 4; i++) {
  •             int offset = (targets.length - 1 - i) * 8;
  •             targets = (byte) ((s >>> offset) & 0xff);
  •         }
  •         return targets;
  •     }
  •     /**
  •      * long to byte[]
  •      *
  •      * @param s
  •      *            long
  •      * @return byte[]
  •      * */
  •     public static byte[] longToByteArray(long s) {
  •         byte[] targets = new byte[2];
  •         for (int i = 0; i < 8; i++) {
  •             int offset = (targets.length - 1 - i) * 8;
  •             targets = (byte) ((s >>> offset) & 0xff);
  •         }
  •         return targets;
  •     }
  •     /**32位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;
  •     }
  •     /**
  •      * 将长度为2的byte数组转换为16位int
  •      *
  •      * @param res
  •      *            byte[]
  •      * @return int
  •      * */
  •     public static int byte2int(byte[] res) {
  •         // res = InversionByte(res);
  •         // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
  •         int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
  •         return targets;
  •     }
  • }


时间: 2024-08-26 17:45:44

[Java] java byte数组与int,long,short,byte转换的相关文章

【转】java中byte数组与int类型的转换(两种方式)----不错

原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送.者接收的数据都是 byte数组,但是int类型是4个byte组成的,如何把一个整形int转换成byte数组,同时如何把一个长度为4的byte数组转换为int类型.下面有两种方式. 第一种方法: public static byte[] int2byte(int

java中byte数组与int类型的转换(两种方式)

java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送.者接收的数据都是 byte数组,但是int类型是4个byte组成的,如何把一个整形int转换成byte数组,同时如何把一个长度为4的byte数组转换为int类型.下面有两种方式. 方法一 /** * int到byte[] * @param i * @return */ public static byte[] intToByteArray(int i) { byte[] resu

byte[]数组和int之间的转换(转)

本帖转自:h t t p://blog.csdn.net/sunnyfans/article/details/8286906 1.int与byte[]之间的转换(类似的byte short,long型) 1 /** 2 * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序. 和bytesToInt()配套使用 3 * @param value 4 * 要转换的int值 5 * @return byte数组 6 */ 7 public static byte[]

byte数组转float实现与byte转换其它类型时进行&amp;运算原理

下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] & 0xff) << 0; accum = accum|(b[1] & 0xff) << 8; accum = accum|(b[2] & 0xff) << 16; accum = accum|(b[3] & 0xff) << 24;

大数据Java基础(一)int型与byte型数组的转换

为了在接下来的篇章中讲解用java实现文件的归档和解归档,需要先了解一下Java中int型与byte型数组之间的相互转换. 首先,我们先来看看int型转换成byte型数组. 我们知道,Java中,一个int型占用4个字节,一个byte型占用1个字节,所以,对于一个int型,我们需要一个长度为4的byte型数组来对其进行存储. 31位--24位 23位--16位 15位--8位 7位--0位 一个int型的4个字节如上图所示,假设用来存储的字节数组为bytes[],那么,我们可以用bytes[0]

byte数组与int,long,short,byte转换 (转载)

byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort(); } public byte[] shortToBytes(short value) { return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(valu

byte[]数组和int之间的转换

unsigned char head[2];            //标识头    0x64,0xa4 unsigned char cmd;                //命令号,  0x03 unsigned char cmd_id;             //命令类型,0x00 unsigned char serial[6];          //设备序列号,MAC 地址 unsigned char cmd_len[2];         //命令长度,低位在前 unsigned 

byte数组和int互转

import java.nio.ByteBuffer; public class Program { public static void main(String[] args) { ByteBuffer buf = ByteBuffer.allocate(3); writeInt24(-113, buf); buf.flip(); int i1 = readInt24(buf); buf.clear(); writeInt24(9408399, buf); buf.flip(); int i2

Delphi Byte数组与Int String之间的相互转换

http://www.cnblogs.com/lcw/p/3352864.html string string = AnsiString = 长字符串,理论上长度不受限制,但其实受限于最大寻址范围2的32次方=4G字节: 变量Str名字是一个指针,指向位于堆内存的字符序列,字符序列起始于@Str[1],@Str[1]偏移负16个字节的空间存储着字串长度.引用计数等信息.字符序列以NULL结束. string[n] string[n] = ShortString = 短字符串,最多容纳255个字符