1. Byte para decimal
Use a conversão (int) do tipo diretamente.
/** Byte para decimal*/ public static int byte2int (byte b) {int r = (int) b; retornar r; }2. Decimal a bytes
Use diretamente a conversão do tipo (byte).
/** decimal a byte*/ public static byte Int2byte (int i) {byte r = (byte) i; retornar r; }3.
Para cada byte, primeiro faça a mesma operação com 0xff e depois use a função Integer.ToHexString (). Se o resultado for de apenas 1 bit, você precisará adicionar 0 na frente.
/** Matriz de byte para string hexadecimal*/ public static string bytes2hexString (byte [] b) {string r = ""; for (int i = 0; i <b.length; i ++) {string hex = Integer.toHexString (b [i] e 0xff); if (hex.Length () == 1) {hex = '0' + hex; } r += hex.touppercase (); } retornar r; }4. String hexadecimal para matriz de bytes
Isso é mais complicado. Cada caráter hexadecimal é de 4 bits e um byte é de 8 bits; portanto, dois caracteres hexadecimais são convertidos em 1 byte. Para o primeiro caractere, converta -o em byte e depois desligue -o por 4 bits e depois faça ou opere com o segundo byte de caracteres, para que os dois caracteres sejam convertidos em 1 byte.
/** Os caracteres são convertidos em byte*/ byte estático privado ChartObyte (char c) {return (byte) "0123456789abcdef" .IndexOf (c); } /** String hexépida para Byte Array* / public static byte [] hexstring2bytes (string hex) {if ((hex == null) || (hex.equals (""))) {return null; } else if (hex.length ()%2! = 0) {return null; } else {hex = hex.touppercase (); int len = hex.length ()/2; byte [] b = novo byte [len]; char [] hc = hex.toCharArray (); for (int i = 0; i <len; i ++) {int p = 2*i; b [i] = (byte) (ChartObyte (hc [p]) << 4 | ChartObyte (hc [p+1])); } retornar b; }}5. Para converter uma matriz de byte em uma corda
Use new string () diretamente.
/** Array de byte para string*/ public static string bytes2string (byte [] b) lança a exceção {string r = new String (b, "utf-8"); retornar r; }6. String to Byte Array
Use getBytes () diretamente.
/** String to Byte Array*/ public static byte [] string2bytes (string s) {byte [] r = s.getBytes (); retornar r; }7. string hexadecimal para string
Converta em byte [] primeiro e depois converta em string.
/** HexString para String*/ public Static String Hex2String (String Hex) lança Exceção {String r = Bytes2String (HexString2Bytes (Hex)); retornar r; }8. String to hexadecimal string
Converter em byte [] primeiro e depois converta -se em string hexadecimal.
/** String para hexadecimal string*/ public static string string2hexString (string s) lança a exceção {string r = bytes2hexString (string2bytes (s)); retornar r; }Função principal:
public static void main (string [] args) lança exceção {byte b1 = (byte) 45; System.out.println ("1. Byte to decimal:" + BYTE2INT (B1)); int i = 89; System.out.println ("2.1 para byte para byte:" + int2byte (i)); byte [] b2 = novo byte [] {(byte) 0xff, (byte) 0x5f, (byte) 0x6, (byte) 0x5a}; System.out.println ("3. Array de bytes para string hexadecimal:" + bytes2hexstring (b2)); String S1 = new String ("1DA47C"); System.out.println ("4,16% string para matriz de bytes:" + Arrays.toString (hexstring2bytes (s1))); System.out.println ("5. Byte Array para String:" + Bytes2String (B2)); System.out.println ("6. String to Byte Array:" + Arrays.toString (String2Bytes (S1))); System.out.println ("7,16% string para string:" + hex2string (s1)); String S2 = new String ("Hello!"); System.out.println ("8. string to hexadecimal string:" + string2hexString (s2)); }Resultados em execução:
1. Byte a decimal: 452.1 decimal ao byte: 893. Byte Array à string hexadecimal: FF5F065A4. String hexadecimal para matriz de bytes: [29, -92, 124] 5. Array de byte para string :? _ z6. Array de corda para byte: [49, 68, 65, 52, 55, 67] 7. String hexadecimal para string :? | 8. String para hexadecimal string: 48656c6c6f21
O artigo acima discute brevemente a conversão mútua entre binário, decimal, hexadecimal e strings é todo o conteúdo que compartilho com você. Espero que possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.