Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does a char value also have an integer value?

 
John Quach
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a tutorial on making games, this is a ResourceManager class that handles Sprites and map loading. With the help of another class, it loads a text file that uses char values to be parsed into something like a tree, ground, rocks, and character sprites. Each image makes up the entire map.

In the loadmap() method, this is the line of code where I'm confused at.



ch is a char value. But its acting like an int value in that operation. I get that char variables have their int values. I don't know why but that's the fact. What I don't understand is how that statement is used in the "for" loop.

How does help parse each char into a tilemap image?

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Characters are represented on computers as binary values just like integers; these binary values can be treated like integers in some cases. The value "ch - 'A'" is, in this case, being used as the index into an array list of images (tiles.get(value)).

For unicode, letters 'A' through 'Z' have sequential values, and if you subtract 'A' from an uppercase letter, you get a value from 0 to 25. The same is true for Ascii, though Ascii is a 7 or 8 bit code instead of a nominally-16-bit code as unicode is. The 0-25 can then be used as an index into an array or any sequential storage.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic