📌  相关文章
📜  将字节数组转换为长整数的Java程序

📅  最后修改于: 2022-05-13 01:55:28.327000             🧑  作者: Mango

将字节数组转换为长整数的Java程序

Java字节数组是一个仅用于存储字节数据类型的数组。字节数组的每个元素的默认值为 0。

给定一个字节数组,将其转换为长值。

例子:

Byte Array: 1 2 3 4
Long Value: 16909060
Equivalent Hexadecimal String: 0x1020304

将 Byte Array 转换为 Long 的方法有很多,下面列出了其中的一些。

方法:

  • 使用移位运算符
  • 使用大整数

方法一:使用移位运算符

将字节数组转换为长值时,字节数组的长度应等于或小于 8,因为长值占用 8 个字节。否则会导致远程溢出。

让我们考虑一个字节数组:

byte[] b = {(byte)0x1, (byte)0x2, (byte) 0x3, (byte) 0x4};
it's long value = 0x1020304

字节数据类型占用 8 位。

initially let val = 0
val = (0<<8) + (11111111 & 1) = 1
val = (1<<8) + (11111111 & 010) = 100000010
val = (100000010 << 8) + (11111111 & 011) = 10000001000000011
val = (10000001000000011 << 8) + (11111111 & 011) = 1000000100000001100000100 = 0x1020304

实施:以下是上述方法的实施——

Java
// Java Program for Converting byte
// array to a long value
 
class GFG {
 
    // A utility function that returns
    // long value from a byte array
    static long convertToLong(byte[] bytes)
    {
        long value = 0l;
 
        // Iterating through for loop
        for (byte b : bytes) {
            // Shifting previous value 8 bits to right and
            // add it with next value
            value = (value << 8) + (b & 255);
        }
 
        return value;
    }
   
    // Driver code
    public static void main(String[] args)
    {
        byte[] byte_array
            = new byte[] { (byte)0x1, (byte)0x2, (byte)0x3,
                           (byte)0x4 };
 
        // Printing the required Byte Array
        System.out.print("Byte Array : ");
        for (int i = 0; i < byte_array.length; i++) {
            System.out.print(byte_array[i] + " ");
        }
        System.out.println();
 
        // Finding long value through the approach discussed
        long ans = convertToLong(byte_array);
 
        // Now compare OUTPUT_VALUE and value and you
        // will find both are equal
        System.out.println("Long value in hex string: "
                           + Long.toHexString(ans));
    }
}


Java
// Java Program to convert byte[]
// to long Using BigInteger
 
import java.math.BigInteger;
 
class ByteToLong {
   
    // returns long value from byte[]
    static long convertToLong(byte[] bytes)
    {
        // Using longValue() from BigInteger
        return new BigInteger(bytes).longValue();
    }
 
    // Driver code
    public static void main(String[] args)
    {
        byte[] byte_array
            = new byte[] { (byte)0x1, (byte)0x2, (byte)0x3,
                           (byte)0x4 };
 
        // Printing the required Byte Array
        System.out.print("Byte Array : ");
        for (int i = 0; i < byte_array.length; i++) {
            System.out.print(byte_array[i] + " ");
        }
        System.out.println();
 
        // Finding long value through the approach discussed
        long ans = convertToLong(byte_array);
 
        // Now you can compare OUTPUT_VALUE and value and you
        // will find both are equal
        System.out.println("Long value in hex string: "
                           + Long.toHexString(ans));
    }
}



输出
Byte Array : 1 2 3 4 
Long value in hex string: 1020304

复杂性分析:

  • 时间复杂度: O(N),其中 N 是 byte_array 的长度
  • 空间复杂度: O(1)

方法 2:使用 BigInteger

尽管我们可以使用它在字节数组和原始值之间进行转换,但使用 BigInteger 对于这种目的来说有点繁重。让我们使用 BigInteger 类将字节数组转换为长值。 BigInteger 类有一个 longValue() 方法将字节数组转换为长值:

long value = new BigInteger(bytes).longValue();

Java

// Java Program to convert byte[]
// to long Using BigInteger
 
import java.math.BigInteger;
 
class ByteToLong {
   
    // returns long value from byte[]
    static long convertToLong(byte[] bytes)
    {
        // Using longValue() from BigInteger
        return new BigInteger(bytes).longValue();
    }
 
    // Driver code
    public static void main(String[] args)
    {
        byte[] byte_array
            = new byte[] { (byte)0x1, (byte)0x2, (byte)0x3,
                           (byte)0x4 };
 
        // Printing the required Byte Array
        System.out.print("Byte Array : ");
        for (int i = 0; i < byte_array.length; i++) {
            System.out.print(byte_array[i] + " ");
        }
        System.out.println();
 
        // Finding long value through the approach discussed
        long ans = convertToLong(byte_array);
 
        // Now you can compare OUTPUT_VALUE and value and you
        // will find both are equal
        System.out.println("Long value in hex string: "
                           + Long.toHexString(ans));
    }
}


输出
Byte Array : 1 2 3 4 
Long value in hex string: 1020304