ASCII Hex string to character conversion

D

David Anderson

Hi,

I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character value.

For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

Any Ideas? Thank you.
 
A

Andrew Thompson

I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character value.

How hard have you looked? This is pretty basic stuff.
For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

Any Ideas? Thank you.

String.substring() in a loop to break it into parts,
prepend each part with \u00, add them back together.

[ Voila.. ]

BTW - beginner questions are best handled on c.l.j.h.
<http://www.physci.org/codes/javafaq.jsp#cljh>
 
D

Dale King

Andrew said:
I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character value.


How hard have you looked? This is pretty basic stuff.

For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

Any Ideas? Thank you.


String.substring() in a loop to break it into parts,
prepend each part with \u00, add them back together.

[ Voila.. ]

I agree its basic, but your answer doesn't make sense to me. You want to
break it into pairs then parse the strings using something like
Integer.parseInt( s, 16 ) to turn that into a numeric value which you
can then cast to a char and add to a StringBuffer.
 
D

Dale King

Dale said:
Andrew said:
I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character
value.



How hard have you looked? This is pretty basic stuff.

For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

Any Ideas? Thank you.



String.substring() in a loop to break it into parts, prepend each part
with \u00, add them back together.

[ Voila.. ]


I agree its basic, but your answer doesn't make sense to me. You want to
break it into pairs then parse the strings using something like
Integer.parseInt( s, 16 ) to turn that into a numeric value which you
can then cast to a char and add to a StringBuffer.

Of course after posting that, I realized that was not the right answer.
You want to create a byte array, use Integer.parseInt( s, 16 ) to
convert each pair into a value that you cast to byte and store in the
byte array. Then use the string constructor to convert the byte data to
a string using the ASCII encoding.
 
R

Roedy Green

I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character value.

For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

see http://mindprod.com/jgloss/hex.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Raymond DeCampo

Dale said:
Dale said:
Andrew said:
On Sat, 23 Jul 2005 11:51:54 -0600, David Anderson wrote:


I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character
value.




How hard have you looked? This is pretty basic stuff.


For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

Any Ideas? Thank you.




String.substring() in a loop to break it into parts, prepend each
part with \u00, add them back together.

[ Voila.. ]



I agree its basic, but your answer doesn't make sense to me. You want
to break it into pairs then parse the strings using something like
Integer.parseInt( s, 16 ) to turn that into a numeric value which you
can then cast to a char and add to a StringBuffer.


Of course after posting that, I realized that was not the right answer.
You want to create a byte array, use Integer.parseInt( s, 16 ) to
convert each pair into a value that you cast to byte and store in the
byte array. Then use the string constructor to convert the byte data to
a string using the ASCII encoding.

I'm thinking that this was one of Andrew's "funny" answers, given the
lack of evidence of any attempt by the OP. In particular, the OP asked
for "any ideas", he didn't specify good ones.

Ray
 
A

Andrew Thompson

.
I'm thinking that this was one of Andrew's "funny" answers, given the
lack of evidence of any attempt by the OP. In particular, the OP asked
for "any ideas", he didn't specify good ones.

LOL ..yeah. Without further comment, I'll go with that. ;-)
 
D

David Anderson

Thank you Dale. That did the trick.


Dale King said:
Dale said:
Andrew said:
On Sat, 23 Jul 2005 11:51:54 -0600, David Anderson wrote:


I am trying to find a way to convert a two-digit hexi-decimal
String representation of an ASCII character into its ASCII character
value.



How hard have you looked? This is pretty basic stuff.


For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

Any Ideas? Thank you.



String.substring() in a loop to break it into parts, prepend each part
with \u00, add them back together.

[ Voila.. ]


I agree its basic, but your answer doesn't make sense to me. You want to
break it into pairs then parse the strings using something like
Integer.parseInt( s, 16 ) to turn that into a numeric value which you can
then cast to a char and add to a StringBuffer.

Of course after posting that, I realized that was not the right answer.
You want to create a byte array, use Integer.parseInt( s, 16 ) to convert
each pair into a value that you cast to byte and store in the byte array.
Then use the string constructor to convert the byte data to a string using
the ASCII encoding.
 
E

E.Otter

Do a search on google for "Base64.java". You should find plenty of examples
including at least one public domain utility at sourceforge.net. The Base64
scheme can encode/decode anything to ascii characters. Its been around for
years. Its pretty much the standard.

If for some reason that doesn't float your boat, use this as a starting
point. It essentially produces 2 "hex" characters for every byte so its
extremely inefficient. Base64 is much more efficient. I had to remove some
company specific stuff so no gaurantees that this compiles/works.

public static String encodeHexString(String sourceText) {

Byte[] rawData = sourceText.getBytes();
StringBuffer hexText= new StringBuffer();
String initialHex = null;
int initHexLength=0;

for(int i=0; i<rawData.length; i++) {
int positiveValue = rawData & 0x000000FF;
initialHex = Integer.toHexString(positiveValue);
initHexLength=initialHex.length();
while(initHexLength++ < 2) {
hexText.append("0");
}
hexText.append(initialHex);
}
return hexText.toString();
}
public static String decodeHexString(String hexText) {

String decodedText=null;
String chunk=null;

if(hexText!=null && hexText.length()>0) {
int numBytes = hexTextlength()/2;

byte[] rawToByte = new byte[numBytes];
int offset=0;
int bCounter=0;
for(int i =0; i <numBytes; i++) {
chunk = hexText.substring(offset,offset+2);
offset+=2;
rawToByte = (byte) (Integer.parseInt(chunk,16) & 0x000000FF);
}
decodedText= new String(rawToByte);
}
return decodedText;
}
 
R

Roedy Green

For example, the ASCII hex String "4A415641" would convert to the
String "JAVA".

You pluck out pairs, convert to int with radix 16, building up a char
array, then convert to string.

For details see http://mindprod.com/jgloss/conversion.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Joined
Jan 2, 2010
Messages
1
Reaction score
0
import java.io.*;
import java.lang.*;

public class HexaToChar{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the hexa number:");
String str= bf.readLine();
int i= Integer.parseInt(str,16);
System.out.println("Decimal:="+ i);
char c = (char)i;
System.out.println("Char is:=" + c);
}
}


There's a way to turn a hex value into ASCII
 
Joined
Dec 25, 2009
Messages
3
Reaction score
0
Hi guys,
I'm using an online tool to convert Hexadecimal to ASCII when I'm in doubt
Here it is: stringfunction.com/hex-string.html :veryprou:
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top