# Thread: Converting Hex Characters to Ints

1. Member
Join Date
May 2012
Posts
30
Rep Power
0

## 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. ## 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. ## Re: Converting Hex Characters to Ints

Go through the methods of the Integer class.

db

4. Member
Join Date
May 2012
Posts
30
Rep Power
0

## Re: Converting Hex Characters to Ints

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

5. ## 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

6. Member
Join Date
May 2012
Posts
30
Rep Power
0

## 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!

db

8. ## 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.

9. Member
Join Date
May 2012
Posts
30
Rep Power
0

## 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. ## Re: Converting Hex Characters to Ints

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

11. ## Re: Converting Hex Characters to Ints

The Integer Class has a method called:

parseInt( String s, int radix )

2 - binary
8 - octal
16 - hex

The method will return a static int which should do what you need.

12. ## Re: Converting Hex Characters to Ints

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

parseInt( String s, int radix )

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. Member
Join Date
May 2012
Posts
30
Rep Power
0

## Re: Converting Hex Characters to Ints

Originally Posted by Norm
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.

Originally Posted by Fubarable
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. ## Re: Converting Hex Characters to Ints

Originally Posted by Grimey
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

15. ## 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.

16. Member
Join Date
May 2012
Posts
30
Rep Power
0

## Re: Converting Hex Characters to Ints

Originally Posted by Norm
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!

#### Posting Permissions

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