Results 1 to 8 of 8
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default How to convert a String into an Hexadecimal ?

    Hi!

    I have a String which represents a hexadecimal number (e.g. "5FA2"). Ideally, I would like to convert this String into an int (e.g. 0x5FA2), is there a quick way to do this ?

    I can't use the method "Integer.parseInt()" since my String represents an hexadecimal and not a decimal.

    Thanks in advance!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    13

    Default

    Read the API specs for the Integer class. There is another Integer.parseInt method that you will find interesting.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default

    Quote Originally Posted by ze snow View Post
    Hi!

    I have a String which represents a hexadecimal number (e.g. "5FA2"). Ideally, I would like to convert this String into an int (e.g. 0x5FA2), is there a quick way to do this ?

    I can't use the method "Integer.parseInt()" since my String represents an hexadecimal and not a decimal.

    Thanks in advance!
    You shouldn't have stopped reading the API documentation after you've found a method that doesn't apply. There's also an overloaded method that can do the job.

    kind regards,

    Jos
    Last edited by JosAH; 02-15-2010 at 08:37 AM. Reason: too slow again ... ;-)

  4. #4
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks guys!

    Looks like the method Integer.decode() is working :-)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default

    Quote Originally Posted by ze snow View Post
    Thanks guys!

    Looks like the method Integer.decode() is working :-)
    decode( ... ) gives you an Integer, not an int; auto(un)boxing will help you but still an additional Integer object was created (ignoring the fact of caching some Integers here). Better use the overloaded parse( ... ) method. Read the API documentation for the Integer class (again).

    kind regards,

    Jos

  6. #6
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Sorry, I just missed this other parse method.
    Indeed, that works even better, saves 1-2 lines :-)


    Thanks again JosAH!

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    13

    Default

    Quote Originally Posted by ze snow View Post
    Sorry, I just missed this other parse method.
    ...
    Thanks again JosAH!
    You also missed my reply.


    r035198x<----jealous

  8. #8
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. trying to convert an interger to a string
    By matpj in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 10:08 AM
  2. convert a really big string (len 39) to bigdecimal
    By coolsig in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2008, 02:48 AM
  3. Replies: 0
    Last Post: 04-06-2008, 07:40 PM
  4. Replies: 7
    Last Post: 01-12-2008, 09:02 PM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •