Nonsense does not read said, directly to everyone paste code, code with comments, can explain everything, this article is not good, please forgive me.
Import Java.io.ByteArrayOutputStream;
/** * Created by the Administrator on 2016/2/2. *-----------16 binary and string---------*------------solve the Chinese garbled problem---------/public class Stringtosixthutils {private static Stri
ng hexstring = "0123456789abcdef"; * * Encode string into 16 digits, apply to all characters (including Chinese)/public static string encode (String str) {//Get byte array based on default encoding byte[] bytes = Str.getbytes ()
;
StringBuilder sb = new StringBuilder (Bytes.length * 2); Splits each byte in a byte array into a 2-bit 16-digit integer for (int i = 0; i < bytes.length; i++) {Sb.append (Hexstring.charat (bytes[i) & 0xf0) >& Gt
4));
Sb.append (Hexstring.charat (bytes[i) & 0x0f));
return sb.tostring (); * * * to decode 16 binary digits to a string, applicable to all characters (including Chinese)/public static string decode (string bytes) {Bytearrayoutputstream BAOs = new Bytearr
Ayoutputstream (Bytes.length ()/2); Assembles each 2-bit 16-digit integer into a byte for (int i = 0; i < bytes.length (); i + + 2) baos.write (Hexstring.indexof (Bytes.charat (i)) << 4 |
Hexstring.indexof (Bytes.charat (i + 1)));
return new String (Baos.tobytearray ()); }
}
give you a piece of code here. About 16 binary strings and byte arrays convert to each other
Package com.wpn.net.util; The public class Numberchange {* * * Converts the 16 feed string into a byte array @param hex @return/public static byte[] Hexstringtobyte (String hex
{int len = (hex.length ()/2);
Byte[] result = new Byte[len];
char[] Achar = Hex.tochararray ();
for (int i = 0; i < len; i++) {int pos = i * 2;
Result[i] = (byte) (ToByte (Achar[pos]) << 4 | tobyte (Achar[pos + 1]));
return result;
private static byte ToByte (char c) {byte b = (byte) ' 0123456789ABCDEF '. IndexOf (c);
return b;
/** * Converts a byte array to a 16-character string * * @param barray * @return/public static final String bytestohexstring (byte[] barray) {
StringBuffer sb = new StringBuffer (barray.length);
String stemp;
for (int i = 0; i < barray.length i++) {stemp = integer.tohexstring (0xFF & Barray[i]);
if (Stemp.length () < 2) sb.append (0);
Sb.append (Stemp.touppercase ());
return sb.tostring (); /** * @ Function function: BCD to 10 into string (Arabic data) * @ input parameter: BCD * @ output Result: 10 into string/public static string Bcd2str (byte[] bytes {StringBuffer temp = new StringBuffer (Bytes.length * 2);
for (int i = 0; i < bytes.length i++) {temp.append ((byte) ((bytes[i) & 0xf0) >>> 4));
Temp.append ((Byte) (Bytes[i] & 0x0f)); Return temp.tostring (). substring (0, 1). Equalsignorecase ("0")?
Temp.tostring (). SUBSTRING (1): temp.tostring (); /** * @ Function Function: 10 in string to BCD * @ input parameter: 10 String * @ Output Result: BCD code/public static byte[] STR2BCD (String asc) {int len = ASC
. Length ();
int mod = len% 2;
if (mod!= 0) {ASC = "0" + ASC;
Len = Asc.length ();
} byte abt[] = new Byte[len];
if (Len >= 2) {len = LEN/2;
} byte bbt[] = new Byte[len];
abt = Asc.getbytes ();
Int J, K; for (int p = 0; p < asc.length ()/2; p++) {if (abt[2 * p] >= ' 0 ') && (ABT[2 * p] <= ' 9 ')) {j = abt[
2 * p]-' 0 ';
else if ((ABT[2 * p] >= ' a ') && (ABT[2 * p] <= ' z ')) {j = abt[2 * p]-' a ' + 0x0a;
else {j = abt[2 * p]-' A ' + 0x0a; } if ((ABT[2 * p + 1] >= ' 0 ') &&amP (ABT[2 * p + 1] <= ' 9 '))
{k = abt[2 * p + 1]-' 0 ';
else if ((ABT[2 * p + 1] >= ' a ') && (ABT[2 * p + 1] <= ' z ')) {k = abt[2 * p + 1]-' a ' + 0x0a;
else {k = abt[2 * p + 1]-' A ' + 0x0a;
int a = (J << 4) + K;
byte B = (byte) A;
BBT[P] = b;
return BBT; public static void Main (string[] arg {/** * 6C 6C 6 F 0A * C4 E3 BA C3 * * string[] str = {"C4", "E3", "BA"
, "C3"}; string[] str = {"7E", "" "," "," "," "", "15", "", "", "", "", "", "", "" 4F "," a "," 0B "," 0B "," 10 "," 14 ",
"The", "the", "7E"};
Byte[] B = new Byte[str.length];
for (int i=0;i<str.length;i++) {B[i] = Hexstringtobyte (str[i]) [0];
} System.out.println (New String (b));
String strc = "Hello";
String bth=bytestohexstring (Strc.getbytes ());
System.out.println (BTH);
System.out.println (Short.max_value);
System.out.println (integer.tobinarystring (280)); }
}
The above is the article to share the Android string and hexadecimal conversion of the Chinese garbled problem of the relevant content, I hope to help you.