Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why does a char value also have an integer value?

 
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?

 
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.

 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic