如何在Java中将扩展的Ascii转换为十六进制.我正遭受这个问题的困扰. [英] How to Convert Extended Ascii To Hexadecimal In Java. I m suffering from this problem.

查看:67
本文介绍了如何在Java中将扩展的Ascii转换为十六进制.我正遭受这个问题的困扰.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我使用扩展的Ascii将Ascii转换为十六进制.请有需要.

我的扩展Ascii字符是:€¥Š"
十六进制预期为:"80 a5 A8

它确实给出了这个答案.
我的代码:

Please Help me to Conert Ascii to Hex with Extended Ascii.Please Needful.

My Extended Ascii Char are :"€ ¥ Š"
Hexa Expected is:"80 a5 A8

It does give this answer.
My Code:

String testString1 = "€¥Š";
                             for (int i=0; i<testString1.length(); i++)
                             {
                              System.out.println(String.format("%x", (byte)(testString1.charAt(i))));
                            }


ng.format(%x",(byte)(testString1.charAt(i))));
}



请帮助代码


ng.format("%x", (byte)(testString1.charAt(i))));
}



Please Help Code

推荐答案

Java char(或字符串的元素)是16位Unicode,而不是8位ASCII.因此,您的演员(byte)(...)通常会被截断.查看Unicode代码表,您的第三个符号的值似乎为0x8a,而不是0xa8.另外,byte是带符号的类型,而char是无符号的.尝试强制转换为int而不是byte并查看得到的结果.

Peter
A Java char (or element of a string) is 16 bit Unicode, not 8 bit ASCII. So your cast (byte)(...) is truncating in general. Looking at a Unicode code chart, it appears that your third symbol has value 0x8a, not 0xa8. Also, byte is a signed type, whereas char is unsigned. Try casting to int instead of byte and see what you get.

Peter


从字符串到十六进制的转换:首先将unicode字符串编码为某种二进制格式,我建议使用utf-8,utf-16或utf-32不是有损的.如果您使用的字符串大多包含拉丁字符,则utf-8是最好的.然后将字节数组转换为十六进制字符串.
从十六进制转换为字符串:将十六进制字符串转换为字节数组,然后,如果您知道编码是什么,则可以轻松地将此字节数组转换为原始字符串.

HexEncode.java:
Conversion from string to hex: First encode the unicode string into some binary format, I recommend utf-8, utf-16, or utf-32 that are not lossy. utf-8 is the best if you are working with string that contain mostly latin characters. Then you convert the byte array to a hex string.
Conversion from hex to string: Convert the hex string into a byte array, and then you can easily convert this byte array to the original string if you know what the encoding is.

HexEncode.java:
import java.io.UnsupportedEncodingException;

public class HexEncode {

	static class BadInputStringException extends Exception {
		public BadInputStringException(String arg0) {
			super(arg0);
		}
	}
	
	private static String ENCODING = "utf-8";

	static private final char[] HEX_DIGITS = new char[] {
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
	static private char intToHexDigit(int b) {
		assert b>=0 && b<16;
		return HEX_DIGITS[b];
	}
	static private int hexDigitToInt(char hexDigit) throws BadInputStringException {
		if (hexDigit>='0' && hexDigit<='9')
			return (int)(hexDigit - '0');
		if (hexDigit>='a' && hexDigit<='f')
			return (int)(hexDigit - 'a' + 10);
		if (hexDigit>='A' && hexDigit<='F')
			return (int)(hexDigit - 'A' + 10);
		throw new BadInputStringException("Invaid hex digit: " + hexDigit);
	}

	private String asciiToHex(String ascii) throws UnsupportedEncodingException, BadInputStringException {
		byte[] encoded = ascii.getBytes(ENCODING);
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<encoded.length; i++) {
			byte b = encoded[i];
			// instead of the two lines below you could write: String.format("%02X", b)
			// but that would probably be slower
			sb.append(intToHexDigit((b >> 4) & 0xF));
			sb.append(intToHexDigit(b & 0xF));
		}
		return sb.toString();
	}

	private String hextoAscii(String hex) throws UnsupportedEncodingException, BadInputStringException {
		if (0 != (hex.length() & 1))
			throw new BadInputStringException("The hex string must contain even number of digits!");
		int encoded_len = hex.length() / 2;
		byte[] encoded = new byte[encoded_len];

		for (int i=0; i<encoded_len; i++) {
			encoded[i] = (byte)((hexDigitToInt(hex.charAt(i*2)) << 4) | hexDigitToInt(hex.charAt(i*2+1))); 
		}
		return new String(encoded, ENCODING);
	}
	
	private void run() {
		try {
			String hex = asciiToHex("TRON");
			String ascii = hextoAscii(hex);
			System.out.printf("hex: %s, decoded_hex: %s", hex, ascii);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new HexEncode().run();
	}
}


地狱pasztorpisti,
您的帖子在我的扩展为Hex Converson的Ascii中很有价值,但根据您的程序,€的十六进制转换为E282AC,而实际的十六进制值则为80 ... 您能否详细说明为什么存在差异..?

在此先感谢
hell pasztorpisti,
Your Post is Valueable in my Extended Ascii To Hex Converson but according to your program hex conversion of € is E282AC and actual hex value is different as 80 ...
Can You Please Elaborate Why There Is Differnce..?

Thanks In Advance


这篇关于如何在Java中将扩展的Ascii转换为十六进制.我正遭受这个问题的困扰.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆