Results 1 to 16 of 16
  1. #1
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Converting Hex Characters to Ints

    Hey!
    I am working on a game and to load the map, I load a text file of n characters by y characters. The value loaded will identify which tile (i.e. grass, dirt, etc.) to be drawn on the players screen. The problem is that I have more than 10 different tiles, and the tiles are loaded as an array. For example, tileExt[0] is water. I thought I could use letters for anything beyond 9, so I could convert a=10, b=11, c=12, etc. The text file would look something like this:

    00000000
    01341A10
    00000000

    where the 0s are the sides of the map and everything between would be other tiles. Is there a way to convert this nicely, as in converting a hexadecimal value. I know I could convert to the ASCII value then just subtact the nessecary value, but I feel like there is a more efficient solution. Any ideas?

    Thanks,
    Grimey

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting Hex Characters to Ints

    Integer.parseInt(...) has an overload that takes a second parameter, an int that acts as a radix. Pass your String as the first parameter, use 16 for the radix, and you should be good to go.

    You will likely need to extract each char in the String first in a for loop, convert that char to a String, and then use Integer.parseInt(...) to convert the String to your int.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Converting Hex Characters to Ints

    Go through the methods of the Integer class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting Hex Characters to Ints

    Alright. I will try that out! Thanks a lot! I appreciate it!

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

    Default Re: Converting Hex Characters to Ints

    Those methods most likely call the Character.digit( ... ) method; read the API documentation for the Character class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting Hex Characters to Ints

    Alright. I have created my functinos, but I am having some troubles getting the text file to load. As far as I know the function should work. The text file is located right in my source directory, but it can't find it. I could use the exact directory name, but I'd rather if I could call it locally since I am moving between different OSs as I work on this. Right now I have a string as a parameter on the file loading function which is the name. I use the name as "file.txt". Should I have something else there?
    Thanks again!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Converting Hex Characters to Ints

    Check out the tutorial on Loading Images Using getResource. The same principles apply to loading any other resource, like a text file.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Converting Hex Characters to Ints

    To see where the program is looking for the file, create a File object with the path you are using to the file and print out the File object's absolute path.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting Hex Characters to Ints

    Thanks for all the input again! I got it working. Apparently loading files is a little different than images, so for images I could just use the name, I had to use src/file.txt to get it. Thanks again!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Converting Hex Characters to Ints

    Apparently loading files is a little different than images
    There should be no difference in using a path to find different types of files. What would be different would be how you use the bytes read from different types of files
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Converting Hex Characters to Ints

    The Integer Class has a method called:

    parseInt( String s, int radix )

    The radix can be:
    2 - binary
    8 - octal
    16 - hex

    The method will return a static int which should do what you need.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting Hex Characters to Ints

    Quote Originally Posted by stchman View Post
    The Integer Class has a method called:

    parseInt( String s, int radix )

    The radix can be:
    2 - binary
    7 - octal
    16 - hex

    The method will return a static int which should do what you need.
    Hm, that looks vaguely familiar -- i.e., the first answer to this question. I'd change it though to 8 for the octal radix, not 7.

  13. #13
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting Hex Characters to Ints

    Quote Originally Posted by Norm View Post
    There should be no difference in using a path to find different types of files. What would be different would be how you use the bytes read from different types of files
    Hmmm... That's interesting then. For whatever reason, the picture worked fine without having "src/" before them. You think it would be a difference in the load functions I am using that would do that? A buddy of mine was having the same issue, where pictures he loaded fine, but he had to specify "src/" for his .txt files. Mind, our functions are pretty similar.

    Quote Originally Posted by Fubarable View Post
    Hm, that looks vaguely familiar -- i.e., the first answer to this question. I'd change it though to 8 for the octal radix, not 7.
    And that is the method I ended up using. It seems to be working. Out of curiosity, does it work for other values as well? Or just binary, octal, and hex? I'm just wondering since I have been expanding the number of tiles I have. I still have few enough that hex covers it, but in the event that I end up with more than 16, I wouldn't mind if I could use any number I wanted.

    Thanks again!

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Converting Hex Characters to Ints

    Quote Originally Posted by Grimey View Post
    Out of curiosity, does it work for other values as well? Or just binary, octal, and hex?
    What does the documentation say? You did read the javadoc, didn't you?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Converting Hex Characters to Ints

    does it work for other values as well?
    Write a small test program and see. Put the code in a loop and use the loop index as the radix.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Converting Hex Characters to Ints

    Quote Originally Posted by Norm View Post
    Write a small test program and see. Put the code in a loop and use the loop index as the radix.
    Yes, these are the habits I need to get better at. Thanks again! The game is coming along pretty nicely. I am in the midst of trying to get my bots working. Thanks for all the help again!

Similar Threads

  1. How to put Ints into Arrays..
    By NewToJava1 in forum New To Java
    Replies: 12
    Last Post: 04-21-2012, 10:08 PM
  2. Converting characters to decimal to binary
    By Majeh in forum New To Java
    Replies: 4
    Last Post: 02-04-2011, 11:06 PM
  3. Converting ints to Strings Problem
    By gkoef in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 01:07 PM
  4. Sum of odd ints 0-n
    By falkon114 in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 08:36 AM
  5. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 03:53 PM

Posting Permissions

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