Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why does a char value also have an integer value?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic