Results 1 to 16 of 16
- 05-30-2012, 05:31 PM #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
-
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.
- 05-30-2012, 05:41 PM #3
Re: Converting Hex Characters to Ints
Go through the methods of the Integer class.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-30-2012, 05:43 PM #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!
- 05-30-2012, 06:12 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-31-2012, 05:24 PM #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!
- 05-31-2012, 06:11 PM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-31-2012, 06:53 PM #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.
If you don't understand my response, don't ignore it, ask a question.
- 06-01-2012, 05:16 PM #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!
- 06-01-2012, 05:32 PM #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 filesApparently loading files is a little different than imagesIf you don't understand my response, don't ignore it, ask a question.
- 06-01-2012, 11:29 PM #11
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.
-
Re: Converting Hex Characters to Ints
- 06-02-2012, 06:25 AM #13
Member
- Join Date
- May 2012
- Posts
- 30
- Rep Power
- 0
Re: Converting Hex Characters to Ints
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.
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!
- 06-02-2012, 06:44 AM #14
- 06-02-2012, 02:34 PM #15
Re: Converting Hex Characters to Ints
Write a small test program and see. Put the code in a loop and use the loop index as the radix.does it work for other values as well?If you don't understand my response, don't ignore it, ask a question.
- 06-03-2012, 07:01 PM #16
Member
- Join Date
- May 2012
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
How to put Ints into Arrays..
By NewToJava1 in forum New To JavaReplies: 12Last Post: 04-21-2012, 10:08 PM -
Converting characters to decimal to binary
By Majeh in forum New To JavaReplies: 4Last Post: 02-04-2011, 11:06 PM -
Converting ints to Strings Problem
By gkoef in forum New To JavaReplies: 7Last Post: 12-06-2010, 01:07 PM -
Sum of odd ints 0-n
By falkon114 in forum New To JavaReplies: 7Last Post: 12-06-2010, 08:36 AM -
Help with ints
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 04-02-2010, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks