Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

ASCII Hex string to character conversion

Thread view: 
David Anderson - 23 Jul 2005 18:51 GMT
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.
Andrew Thompson - 23 Jul 2005 20:32 GMT
> 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>

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
In Hypno-Vision

Dale King - 24 Jul 2005 03:25 GMT
>>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.
[quoted text clipped - 10 lines]
>
> [ 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.

Signature

 Dale King

Dale King - 24 Jul 2005 03:57 GMT
>>> I am trying to find a way to convert a two-digit hexi-decimal
>>> String representation of an ASCII character into its ASCII character
[quoted text clipped - 16 lines]
> 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.

Signature

 Dale King

Raymond DeCampo - 24 Jul 2005 17:27 GMT
>>>> I am trying to find a way to convert a two-digit hexi-decimal
>>>> String representation of an ASCII character into its ASCII character
[quoted text clipped - 22 lines]
> 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

Signature

XML is the programmer's duct tape.

Andrew Thompson - 24 Jul 2005 18:04 GMT
...
> 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.   ;-)

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Too Hot For Radio

David Anderson - 25 Jul 2005 02:35 GMT
Thank you Dale.  That did the trick.

>>>> I am trying to find a way to convert a two-digit hexi-decimal
>>>> String representation of an ASCII character into its ASCII character
[quoted text clipped - 22 lines]
> Then use the string constructor to convert the byte data to a string using
> the ASCII encoding.
Roedy Green - 24 Jul 2005 10:15 GMT
>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

Signature

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

E.Otter - 26 Jul 2005 04:36 GMT
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[i] & 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[i] = (byte) (Integer.parseInt(chunk,16) & 0x000000FF);
  }
  decodedText= new String(rawToByte);
 }
 return decodedText;
}
Roedy Green - 26 Jul 2005 07:44 GMT
>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

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.