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))));
Please Help Code
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
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];
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();
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
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Originally unicode did not have more than 2^16 characters so a 16 bit char was enough to represent a unicode character (see UCS). Currently it is just a unit of an utf-16 encoding where one character might consist of a high and low surrogate pair, so 2 chars in java. Currently the unicode tables have somewhat more than 1 million characters so the only encoding that is not really an encoding but pure unicode is utf-32 where a 32 bit unit is always the unicode character (codepoint) itself.