Home >Computer Tutorials >Computer Knowledge >How to convert hexadecimal to string using JAVA? grateful!
private static String hexString = "0123456789ABCDEF";
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* Encode the string into hexadecimal numbers, suitable for all characters (including Chinese)
*/
public static String encode(String str) {
// Get the byte array based on the default encoding
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// Decompose each byte in the byte array into a 2-digit hexadecimal integer
for (int i = 0; i
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}
/*
* Decode hexadecimal numbers into strings, applicable to all characters (including Chinese)
*/
public static String decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// Assemble each 2-digit hexadecimal integer into a byte
for (int i = 0; i
baos.write((hexString.indexOf(bytes.charAt(i))
.indexOf(bytes.charAt(i 1))));
return new String(baos.toByteArray());
}
Personal testing is feasible, supports Chinese! ! ! Please remember to adopt
if it is usefulUse this method to convert the string consisting of hexadecimal numbers passed in into a string in UTF-8 format
public static String toStringHex1(String s) {
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
i * 2, i * 2 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, "utf-8");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
I think your problem is just converting numbers into characters, right? Then I won’t help you make the multi-line source code of the full text. I made one line of conversion. You can nest a loop in the outer layer and use Two-dimensional array to achieve full-text multi-line ASCII code conversion:
#include "Stdio.h"
#include "Conio.h"
int main(void)
{
int num[10],i=0;
while(getchar() != '\n')
{
getchar();
scanf("%x",&num[i]);
printf("%c",num[i]);
i ;
}
getch();
return 0;
}
The principle is this:
The format of your four bytes each time is in the form of \xyy. I only need to read the hexadecimal number and use the scanf("%x",&num[i]); statement. Hexadecimal numbers can be read in, and all the hexadecimal numbers in a row can be taken out in a loop and stored in a one-dimensional array.
I was too lazy in terms of output, so I read it directly and output it. However, this is not the key. The key is to output the statement with printf("%c",num[i]), and the compiler will automatically The hexadecimal number is converted into characters corresponding to the ascii code and output.
Now do you understand? It uses formatted input and output to read hexadecimal numbers and output characters. (The two getchar() are to read the characters \ and x respectively~~)
#Convert hexadecimal characters to binary characters
def str2byte(s):
base='0123456789ABCDEF'
i=0
s = s.upper()
s1=''
while i c1=s[i]
c2=s[i 1]
i =2
b1=base.find(c1)
b2=base.find(c2)
if b1 == -1 or b2 == -1:
return None
s1 =chr((b1 return s1
s = 'C7 EB CE F0 BE C6 BA F3 BC DD B3 B5'.replace (' ', '') #Remove spaces
s1 = str2byte(s)
print s1.decode('gbk') #Decode and output with gbk encoding
#result
>>>s1
'\xc7\xeb\xce\xf0\xbe\xc6\xba\xf3\xbc\xdd\xb3\xb5'
>>>print s1.decode('gbk')
Please do not drink and drive
The above is the detailed content of How to convert hexadecimal to string using JAVA? grateful!. For more information, please follow other related articles on the PHP Chinese website!