Java: BigInteger & 整形 <=> byte[]

复制代码
package io.wig;


import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;

public class Fozy{
  public static void main(String[] args){
    SecureRandom secureRandom = new SecureRandom();
    byte[] bytes = new byte[16];
    secureRandom.nextBytes(bytes);
    System.out.println("bytes = " + Arrays.toString(bytes));
    BigInteger bigInt = new BigInteger(1, bytes);
    System.out.println("bigInt = " + bigInt);
    System.out.println("bigInt.toByteArray() = " + Arrays.toString(bigInt.toByteArray()));
    System.out.println("toByteArray(bigInt) = " + Arrays.toString(toByteArray(bigInt)));

    BigInteger bigInteger = new BigInteger(bytes);
    System.out.println("bigInteger = " + bigInteger);
    System.out.println("bigInteger.toByteArray() = " + Arrays.toString(bigInteger.toByteArray()));

  }

  public static byte[] toByteArray(BigInteger bigInteger){
    byte[] bytes = bigInteger.toByteArray();

    byte[] tmp = new byte[bytes.length - 1];
    System.arraycopy(bytes,1,tmp,0,tmp.length);
    return tmp;
  }
}
复制代码

 

 

 

int => byte[] Big Endian

复制代码
public class Cozy{
  public static void main(String[] args){
    byte[] bytes = intToByteBig(0xFF);
    System.out.println("b1 = " + Arrays.toString(bytes));
  }

  public static byte[] intToByteBig(int n){
    byte[] bytes = new byte[4];
    int length = bytes.length;
    for(int b = 0; b < length; ++b){
      bytes[length - 1 - b] = (byte) (n >> b * 8 & 0xFF);
    }
    return bytes;
  }
}
复制代码

 

 

int => byte[] Little Endian:

复制代码
import java.util.Arrays;

public class Cozy{
  public static void main(String[] args){
    byte[] bytes = intToByteLittle(0xFF);
    System.out.println("b1 = " + Arrays.toString(bytes));
  }

  public static byte[] intToByteLittle(int n){
    byte[] bytes = new byte[4];
    int length = bytes.length;
    for(int b = 0; b < length; ++b){
      bytes[b] = (byte) (n >> b * 8 & 0xFF);
    }
    return bytes;
  }
}
复制代码

 

 

byte[] => int Big Endian:

复制代码
public class Cozy{
  public static void main(String[] args){
    int i = bytesToIntBig(new byte[]{0, -1, -1, -1});
    System.out.println("i = " + i);
  }

  public static int bytesToIntBig(byte[] bytes){
    int n = 0;
    int length = bytes.length;
    for(int b = 0; b < length; ++b){
      n |= (bytes[b] & 0xFF) << 8 * (length - 1 - b);
    }
    return n;
  }
}
复制代码

 

 

 byte[] => int Little Endian:

复制代码
public class Cozy{
  public static void main(String[] args){
    int i = bytesToIntLittle(new byte[]{0, -1, -1, -1});
    System.out.println("i = " + i);
  }


  public static int bytesToIntLittle(byte[] bytes){
    int n = 0;
    int length = bytes.length;
    for(int b = 0; b < length; ++b){
      n |= (bytes[b] & 0xFF) << 8 * b;
    }
    return n;
  }
}
复制代码

 

 

 

posted @   ascertain  阅读(144)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-04-22 python zipfile
2021-04-22 正则Regular Expression
点击右上角即可分享
微信分享提示