Jump to content



Photo

why does this bit of code work?


  • Please log in to reply
7 replies to this topic

#1 Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 20:03

Hey guys,

 

I ran into something that has me puzzled....why does if(ch1 > 'Z') ch1 -= ' '; work? 




#2 Brian M.

Brian M.

    Neowinian Senior

  • Tech Issues Solved: 11
  • Joined: 07-January 05
  • Location: London, UK

Posted 06 October 2013 - 20:08

Because char is interchangeable with integer. They have slightly different properties in most languages, but a char and int are essentially just variables of different length.

 

One thing that puzzles me though - I may be wrong here, but I assume '' will equate to the (nul) character, which has a value of 0? Edit: didn't see the space in there!



#3 Cøi

Cøi

    The Aviator

  • Joined: 17-April 11
  • OS: Windows 6.3

Posted 06 October 2013 - 20:12

I assume it's supposed to convert lower case chars into upper case chars?

 

ASCII value for a space (' ') is 32. Integer representations of lower case chars are 32 higher than those of their equivalent upper case chars. So if you subtract 32 from a lower case char it will become an upper case char.

 

Example: A = 65, a = 97

'a' - ' ' = 'A'

97 - 32 = 65



#4 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 20:42

yes it converts lower case to upper case, that much I do know, but wasn't sure how or why it worked



#5 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 06 October 2013 - 22:04

So in it's basic form, the alphabet is represented by a computer as a list of numbers called ASCII (see here). So the char data type which represents a character of the alphabet (as well as punctuation and various other types of "symbol") is actually just a number between 0 and 255.

While this is convenient for a computer to interpret, it also allows for some weird coding like what you've demonstrated. As Cøi correctly pointed out, the space character is actually represented by the number 32 (see the link I posted earlier), so in the code you posted, you're converting a lower case character to it's upper case equivalent (the ASCII numbers for upper case letters are all 32 lower than their lower case alternatives. (A = 65, a = 97; 97 - 65 = 32).

It's worth bearing in mind that while this is possible, it's not recommended as it's confusing to read ('a' minus space is 'A'? What the heck?), so try to avoid this code if possible. It's always better to be obvious in your intentions when writing code, as it makes your code easier to read and maintain at a later date. :)

Appendix 1
ASCII is just one way of encoding characters, and its also very limited. For more advanced computer programs, Unicode is often preferable as it provides the ability to represent almost and character in any language (Chinese, Japanese, Greek, Arabic, etc), whereas ASCII cannot do this. Since you're a relative beginner though, there's no need to worry about this. Encoding strings is almost a science in its own right.

Appendix 2
As another weirdness, you can also do this:
 
unsigned int neow = 'NEOW';
unsigned int in = 'IN';

printf("neowin = %d", neow + in); // "neowin = 1313192133"
Since each character in "NEOW" and "IN" represents a single byte, 4 together represent a 4 byte integer, and therefore you can populate numbers using strings! I must insist however that you NEVER, EVER, use this code, since it's absolutely the worst code in the world.

#6 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 22:10

Thank you for your explanations!  Makes perfect sense now lol.



#7 +GreenMartian

GreenMartian

    Neowinian Senior

  • Joined: 28-August 04
  • Location: adelaide, au

Posted 07 October 2013 - 12:18

So in it's basic form, the alphabet is represented by a computer as a list of numbers called ASCII (see here).

Good old asciitable.com

I used to have that image printed out and stuck on the wall next to my monitor. It's like the multiplication table, FOR GROWNUPS!



#8 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 07 October 2013 - 12:59

It's the same as doing

'1' - '0' = 1 

Converting any char (that is a number (0-9)) into an integer is as easy as subtracting the '0' char.