十六进制到字符串和返回值不匹配
HEX to String and back - value not matching
我正在尝试将十六进制值转换为字符串,然后再转换回十六进制。
我看到对于某些十六进制值,当我将生成的字符串转换回十六进制时,我得到了原始值,但是对于某些值,该值不匹配。
工作示例:414d512044435344454d4f2020202020bc9b72561dda9820
不工作示例:414d512044435344454d4f2020202020bc9b7256026cb420
我也尝试过 http://codebeautify.org and from java code (http://www.mkyong.com/java/how-to-convert-hex-to-ascii-in-java/ 等在线网站。
请帮助我理解为什么我会看到这样的行为。提前致谢。
public class StringToHex{
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for( int i=0; i<hex.length()-1; i+=2 ){
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
public static void main(String[] args) {
StringToHex strToHex = new StringToHex();
String hex = "414d512044435344454d4f2020202020bc9b7256026cb420";
System.out.println("Hex : " + hex);
System.out.println("\n***** Convert Hex to ASCII *****");
System.out.println("Hex : " + hex);
String acsii = strToHex.convertHexToString(hex);
System.out.println("ASCII : " + acsii);
System.out.println("\n***** Convert ASCII to Hex *****");
String str = acsii;
System.out.println("Original input : " + str);
System.out.println("Hex : " + strToHex.convertStringToHex(str));
}
}
我重写了将字符串转换为十六进制的代码
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(String.format("%02x", ((int)chars[i])));
}
return hex.toString();
}
现在它可以正常工作了。问题似乎是早些时候它忽略了十六进制中的前导零。
我正在尝试将十六进制值转换为字符串,然后再转换回十六进制。
我看到对于某些十六进制值,当我将生成的字符串转换回十六进制时,我得到了原始值,但是对于某些值,该值不匹配。
工作示例:414d512044435344454d4f2020202020bc9b72561dda9820
不工作示例:414d512044435344454d4f2020202020bc9b7256026cb420
我也尝试过 http://codebeautify.org and from java code (http://www.mkyong.com/java/how-to-convert-hex-to-ascii-in-java/ 等在线网站。
请帮助我理解为什么我会看到这样的行为。提前致谢。
public class StringToHex{
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for( int i=0; i<hex.length()-1; i+=2 ){
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
public static void main(String[] args) {
StringToHex strToHex = new StringToHex();
String hex = "414d512044435344454d4f2020202020bc9b7256026cb420";
System.out.println("Hex : " + hex);
System.out.println("\n***** Convert Hex to ASCII *****");
System.out.println("Hex : " + hex);
String acsii = strToHex.convertHexToString(hex);
System.out.println("ASCII : " + acsii);
System.out.println("\n***** Convert ASCII to Hex *****");
String str = acsii;
System.out.println("Original input : " + str);
System.out.println("Hex : " + strToHex.convertStringToHex(str));
}
}
我重写了将字符串转换为十六进制的代码
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(String.format("%02x", ((int)chars[i])));
}
return hex.toString();
}
现在它可以正常工作了。问题似乎是早些时候它忽略了十六进制中的前导零。