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 / May 2005

Tip: Looking for answers? Try searching our database.

convert string to hex

Thread view: 
yuvalken@gmail.com - 16 May 2005 11:48 GMT
Hi,

How do I convert string to hexadecimal?

For example "ab cd" -> 61 62 20 63 64 .

Thanks,

P.
Thomas Schodt - 16 May 2005 12:52 GMT
> How do I convert string to hexadecimal?
>
> For example "ab cd" -> 61 62 20 63 64 .

I can make a guess at what you are asking for.

  String abcd = "ab cd";
  byte[] bytes = abcd.getBytes("US-ASCII");

If you have "exotic" characters in your string
maybe you would rather use

  char[] chars = abcd.toCharArray();
Pete Barrett - 16 May 2005 19:32 GMT
>Hi,
>
>How do I convert string to hexadecimal?
>
>For example "ab cd" -> 61 62 20 63 64 .

Integer has a toHexString() static method, and it's not difficult to
iterate through a string or array, converting each value as you come
to it. Ther are a couple of minor problems which I'll leave you to
solve for yourself - (i) the integer cast from a char, byte, or short
will be sign extended; and (ii) toHexString doesn't pad with 0s.

Pete Barrett


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.