Android byte array to convert hexadecimal strings (IOT Development Summary)
I think of outsourcing development of the Internet of Things some time ago, and often encounter data received through wifi, which should be converted into a hexadecimal string or finally in decimal format. It is developed based on the agreement between the two parties. The data I sent in the past also needs to be specially converted into byte bytes, so that the hardware side will not receive garbled data.
1. The raw data of hexadecimal data is sent to android through hardware debugging: 68 38 38 68 A 72 78 55 34 12 43 23 01 07 Y 00 00 00 0C 13 78 56 34 12 0C 3B 78 34 12 0C 26 78 56 34 12 0B 59 45 23 00 02 FD 17 00 CS 16
The data read by android is a byte array.
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));byte readBuffer[] = new byte[64];int count = 0; try {count = dis.read(readBuffer); } catch (IOException e) {continue; }
ReadBuffer receives the following data: 104, 56, 56,104, 0,114,120, 85, 52, 18, 67, 35, 1, 7, 0, 0, 0, 0, 12, 19,120, 86, 52, 18, 12, 59,120, 52, 18, 12, 38,120, 86, 52, 18, 11, 89, 69, 35, 0, 2, -3, 23, 0, 0, 22
Then convert the data to a hexadecimal String. If you convert the data directly to a String, it must be garbled. Because the hardware debugging sent to android is hexadecimal data.
The hexadecimal format of the read buffer byte array is as follows: 68, 38, 38, 68, 00, 72, 78, 55, 34, 12, 43, 23, 01, 07, 00, 00, 00, 00, 0C, 13, 78, 56, 34, 12, 0C, 3B, 78, 34, 12, 0C, 26, 78, 56, 34, 12, 0B, 59, 45, 23, 00, 02, FD, 17, 00, 00, 16
It can be seen that it is the same as the hardware. CS is filled with 00 instead of hexadecimal Y.
Of course, these are all parsed and processed based on the data sent and received by both parties.
2. The original android data sent from the android client to the hardware side is as follows: 68 04 04 68 35 FD 50 00 A0 16, which is also a hexadecimal string.
What needs to be done is to convert the hexadecimal string into a byte array.
Conversion code:
Private String mstrRestartSend = "FE 68 04 68 53 FD 50 00 A0 16"; private byte [] mRestart = null; mRestart = StringUtil. hexCommandtoByte (mstrRestartSend. getBytes (); public class StringUtil {// convert the hexadecimal string to the byte array public static byte [] HexCommandtoByte (byte [] data) {if (data = null) {return null;} int nLength = data. length; String strTemString = new String (data, 0, nLength); String [] strings = str TemString. split (""); nLength = strings. length; data = new byte [nLength]; for (int I = 0; I <nLength; I ++) {if (strings [I]. length ()! = 2) {data [I] = 00; continue;} try {data [I] = (byte) Integer. parseInt (strings [I], 16);} catch (Exception e) {data [I] = 00; continue ;}} return data ;}}
In this case, there will be no errors or garbled characters.
Many beginners, especially in the IOT field, cannot figure out the conversion of this basic data exchange.
3. Write a demo to test data conversion.
Example:
4. Collect comprehensive underlying java data on the Internet to convert binary, decimal, hexadecimal, and ASCII code to String, byte array, and hexadecimal String in Java
Public class DigitalTrans {/*** convert a numeric String to an ASCII String ** @ param String * @ return ASCII String */public static String StringToAsciiString (String content) {String result = ""; int max = content. length (); for (int I = 0; I <max; I ++) {char c = content. charAt (I); String B = Integer. toHexString (c); result = result + B;} return result;}/*** hexadecimal conversion string ** @ param hexString * hexadecimal string * @ param encodeTy Pe * encoding type 4: Unicode, 2: normal encoding * @ return String */public static String hexStringToString (String hexString, int encodeType) {String result = ""; int max = hexString. length ()/encodeType; for (int I = 0; I <max; I ++) {char c = (char) DigitalTrans. hexStringToAlgorism (hexString. substring (I * encodeType, (I + 1) * encodeType); result + = c;} return result ;} /*** hexadecimal string in decimal format ** @ param hex * hexadecimal string * @ Return decimal value */public static int hexStringToAlgorism (String hex) {hex = hex. toUpperCase (); int max = hex. length (); int result = 0; for (int I = max; I> 0; I --) {char c = hex. charAt (I-1); int algorism = 0; if (c> = '0' & c <= '9') {algorism = c-'0 ';} else {algorism = c-55;} result + = Math. pow (16, max-I) * algorism;} return result;}/*** convert binary ** @ param hex * hexadecimal string * @ return Binary String */public static String hexStringToBinary (String hex) {hex = hex. toUpperCase (); String result = ""; int max = hex. length (); for (int I = 0; I <max; I ++) {char c = hex. charAt (I); switch (c) {case '0': result + = "0000"; break; case '1': result + = "0001"; break; case '2': result + = "0010"; break; case '3': result + = "0011"; break; case '4': result + = "0100 "; break; case '5': result + = "01 01 "; break; case '6': result + =" 0110 "; break; case '7': result + =" 0111 "; break; case '8 ': result + = "1000"; break; case '9': result + = "1001"; break; case 'A': result + = "1010"; break; case 'B': result + = "1011"; break; case 'C': result + = "1100"; break; case 'D': result + = "1101 "; break; case 'E': result + = "1110"; break; case 'F': result + = "1111"; break ;}} return result ;} /*** ASCII code string transpose Character String ** @ param String * ASCII String * @ return String */public static String AsciiStringToString (String content) {String result = ""; int length = content. length ()/2; for (int I = 0; I <length; I ++) {String c = content. substring (I * 2, I * 2 + 2); int a = hexStringToAlgorism (c); char B = (char) a; String d = String. valueOf (B); result + = d;} return result;}/*** converts decimal to a hexadecimal string of the specified length ** @ param al Gorism * int decimal number * @ param maxLength * length of the hexadecimal String after int conversion * @ return String converted hexadecimal String */public static String algorismToHEXString (int algorism, int maxLength) {String result = ""; result = Integer. toHexString (algorism); if (result. length () % 2 = 1) {result = "0" + result;} return patchHexString (result. toUpperCase (), maxLength);}/*** convert the byte array to a common string (ASCII character) ** @ param bytearray * byte [] * @ Return String */public static String bytetoString (byte [] bytearray) {String result = ""; char temp; int length = bytearray. length; for (int I = 0; I <length; I ++) {temp = (char) bytearray [I]; result + = temp;} return result ;} /*** convert binary String to decimal ** @ param binary * binary String * @ return decimal value */public static int binaryToAlgorism (String binary) {int max = binary. length (); int result = 0; for (int I = Max; I> 0; I --) {char c = binary. charAt (I-1); int algorism = c-'0'; result + = Math. pow (2, max-I) * algorism;} return result ;} /*** convert the value in decimal format to a hexadecimal String ** @ param algorism * an int decimal number * @ return String corresponds to a hexadecimal String */public static String algorismToHEXString (int algorism) {String result = ""; result = Integer. toHexString (algorism); if (result. length () % 2 = 1) {result = "0" + result;} resu Lt = result. toUpperCase (); return result;}/*** add 0 to the front of the HEX string, mainly used for insufficient length digits. ** @ Param str * String the hex String to be supplemented * @ param maxLength * int the length of the hex String after supplementation * @ return supplement result */static public String patchHexString (String str, int maxLength) {String temp = ""; for (int I = 0; I <maxLength-str. length (); I ++) {temp = "0" + temp;} str = (temp + str ). substring (0, maxLength); return str ;} /*** convert a String to int ** @ param s * String to be converted * @ param defaultInt * int if an exception occurs, the number returned by default * @ Param radix * What is the hexadecimal value of the int to be converted, for example, 16 8 10. * @ return the number converted by int */public static int parseToInt (String s, int defaultInt, int radix) {int I = 0; try {I = Integer. parseInt (s, radix);} catch (NumberFormatException ex) {I = defaultInt;} return I ;} /*** convert a numeric String in decimal format to int ** @ param s * String to be converted * @ param defaultInt * if an exception occurs, the number returned by default * @ return int converted */public static int parseT OInt (String s, int defaultInt) {int I = 0; try {I = Integer. parseInt (s);} catch (NumberFormatException ex) {I = defaultInt;} return I;}/*** convert the hexadecimal string into a Byte array, convert every two hexadecimal characters into a Byte ** @ param hex * hexadecimal String * @ return byte Conversion Result */public static byte [] hexStringToByte (String hex) {int max = hex. length ()/2; byte [] bytes = new byte [max]; String binarys = DigitalTrans. hexStringToBinary (hex); for (in T I = 0; I <max; I ++) {bytes [I] = (byte) DigitalTrans. binaryToAlgorism (binarys. substring (I * 8 + 1, (I + 1) * 8); if (binarys. charAt (8 * I) = '1') {bytes [I] = (byte) (0-bytes [I]) ;}} return bytes ;} /*** convert the hexadecimal String to a byte array ** @ return the array of byte */public static final byte [] hex2byte (String hex) throws IllegalArgumentException {if (hex. length () % 2! = 0) {throw new IllegalArgumentException ();} char [] arr = hex. toCharArray (); byte [] B = new byte [hex. length ()/2]; for (int I = 0, j = 0, l = hex. length (); I <l; I ++, j ++) {String swap = "" + arr [I ++] + arr [I]; int byteint = Integer. parseInt (swap, 16) & 0xFF; B [j] = new Integer (byteint ). byteValue ();} return B ;} /*** convert a byte array to a hexadecimal String ** @ param B * byte [] the byte array to be converted * @ return String hexadecimal String */ Public static final String byte2hex (byte B []) {if (B = null) {throw new IllegalArgumentException ("Argument B (byte array) is null! ");} String hs =" "; String stmp =" "; for (int n = 0; n <B. length; n ++) {stmp = Integer. toHexString (B [n] & 0xff); if (stmp. length () = 1) {hs = hs + "0" + stmp;} else {hs = hs + stmp;} return hs. toUpperCase ();}}