Java 中 byte、byte 数组和 int、long 之间的转换
来源:互联网 发布:农村上网数据 编辑:程序博客网 时间:2024/06/06 01:57
原文: http://blog.csdn.net/defonds/article/details/8782785
Java 中 byte 和 int 之间的转换源码:
-
- public static byte intToByte(int x) {
- return (byte) x;
- }
-
- public static int byteToInt(byte b) {
-
- return b & 0xFF;
- }
测试代码:
-
- int int0 = 234;
- byte byte0 = intToByte(int0);
- System.out.println("byte0=" + byte0);
-
- int int1 = byteToInt(byte0);
- System.out.println("int1=" + int1);
Java 中 byte 数组和 int 之间的转换源码:
-
- public static int byteArrayToInt(byte[] b) {
- return b[3] & 0xFF |
- (b[2] & 0xFF) << 8 |
- (b[1] & 0xFF) << 16 |
- (b[0] & 0xFF) << 24;
- }
-
- public static byte[] intToByteArray(int a) {
- return new byte[] {
- (byte) ((a >> 24) & 0xFF),
- (byte) ((a >> 16) & 0xFF),
- (byte) ((a >> 8) & 0xFF),
- (byte) (a & 0xFF)
- };
- }
测试代码:
-
- int int2 = 1417;
- byte[] bytesInt = intToByteArray(int2);
- System.out.println("bytesInt=" + bytesInt);
-
- int int3 = byteArrayToInt(bytesInt);
- System.out.println("int3=" + int3);
Java 中 byte 数组和 long 之间的转换源码:
- private static ByteBuffer buffer = ByteBuffer.allocate(8);
-
- public static byte[] longToBytes(long x) {
- buffer.putLong(0, x);
- return buffer.array();
- }
-
- public static long bytesToLong(byte[] bytes) {
- buffer.put(bytes, 0, bytes.length);
- buffer.flip();
- return buffer.getLong();
- }
测试代码:
-
- long long1 = 2223;
- byte[] bytesLong = longToBytes(long1);
- System.out.println("bytes=" + bytesLong);
-
- long long2 = bytesToLong(bytesLong);
- System.out.println("long2=" + long2);
整体工具类源码:
- import java.nio.ByteBuffer;
-
-
- public class Test {
-
- private static ByteBuffer buffer = ByteBuffer.allocate(8);
-
- public static void main(String[] args) {
-
-
- int int0 = 234;
- byte byte0 = intToByte(int0);
- System.out.println("byte0=" + byte0);
-
- int int1 = byteToInt(byte0);
- System.out.println("int1=" + int1);
-
-
-
-
- int int2 = 1417;
- byte[] bytesInt = intToByteArray(int2);
- System.out.println("bytesInt=" + bytesInt);
-
- int int3 = byteArrayToInt(bytesInt);
- System.out.println("int3=" + int3);
-
-
-
- long long1 = 2223;
- byte[] bytesLong = longToBytes(long1);
- System.out.println("bytes=" + bytesLong);
-
- long long2 = bytesToLong(bytesLong);
- System.out.println("long2=" + long2);
- }
-
-
-
- public static byte intToByte(int x) {
- return (byte) x;
- }
-
- public static int byteToInt(byte b) {
-
- return b & 0xFF;
- }
-
-
- public static int byteArrayToInt(byte[] b) {
- return b[3] & 0xFF |
- (b[2] & 0xFF) << 8 |
- (b[1] & 0xFF) << 16 |
- (b[0] & 0xFF) << 24;
- }
-
- public static byte[] intToByteArray(int a) {
- return new byte[] {
- (byte) ((a >> 24) & 0xFF),
- (byte) ((a >> 16) & 0xFF),
- (byte) ((a >> 8) & 0xFF),
- (byte) (a & 0xFF)
- };
- }
-
-
- public static byte[] longToBytes(long x) {
- buffer.putLong(0, x);
- return buffer.array();
- }
-
- public static long bytesToLong(byte[] bytes) {
- buffer.put(bytes, 0, bytes.length);
- buffer.flip();
- return buffer.getLong();
- }
-
- }
运行测试结果:byte0=-22
int1=234
bytesInt=[B@de6ced
int3=1417
bytes=[B@c17164
long2=2223参考文章1:http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back
。参考文章2:http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java
。参考文章3:http://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
。 阅读全文
0 0
- Java 中 byte、byte 数组和 int、long 之间的转换
- Java 中 byte、byte 数组和 int、long 之间的转换
- Java 中 byte、byte 数组和 int、long 之间的转换
- Java 中 byte、byte 数组和 int、long 之间的转换
- Java 中 byte、byte 数组和 int、long 之间的转换
- java中byte数组和int,long,short之间的转换
- java中int类型和byte[]数组之间的转换
- Java中byte[]数组和int之间的转换
- java中long,int,short与byte数组之间的转换
- java中long,int,short与byte数组之间的转换
- java中byte数组与int,long,short,float,char之间的转换
- java中byte数组与int,long,short,float,char之间的转换
- java中long,int,short与byte数组之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- java中long,int,short与byte数组之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- java int long 和byte的转换
- java中int与byte,以及long与byte之间的转换
- orhanobut/logger的使用
- 响应式图片<picture>标签兼容处理(IE9)
- 击杀大魔王
- Android studio使用JNI实例
- 单片机
- Java 中 byte、byte 数组和 int、long 之间的转换
- Jsp制作验证码
- 实现居中的方法
- dev
- Kafka 设计原理
- POJ 2406 Power Strings(KMP+最小循环节)
- 字符串问题---括号字符串的有效性和最长有效长度
- 简单实现日历功能
- 机器学习各种算法的特点归纳