Learn Objective-C: ASCII, Booleans and Characters

Type char

A variable of type char is used to store a single character. It is denoted as a character, flanked by two single quotes. (Be careful—single quotes are used for characters, double quotes signify a string.) ‘a’, ‘;’, and ‘8’ all represent character types. The first is simply the letter a. The second is the semicolon symbol; it is not the same as the one used to mark the end of a line. The last example is the character 8, not the integer 8.

Escape sequences, when enclosed in single quotes, are also considered to be char, even though they are composed of more than one symbol.

To print a character or escape sequence through NSLog, use %c.

ASCII Values

In C, chars are represented as 8-bit ASCII values. Internally, characters are stored as numbers, with a range from 0-127. This makes for interesting uses, including the following simple cypher program:

  1. #import
  2.  
  3. int main (int argc, const char * argv[]) {
  4.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    // A simple cypher program
  5.     NSLog(@"Hello");  // Original message
  6.     NSLog(@"%c%c%c%c%c", ('H' + 1), ('e' + 1), ('l' + 1), ('l' + 1), ('o' + 1));      // Cypher 1
  7.     NSLog(@"%c%c%c%c%c", (('H' * 2) % 127), (('e' * 2) % 127), (('l' * 2) % 127), (('l' * 2) % 127), (('o' * 2) % 127));   /* Cypher 2...
  8.                                                                                                                              note the use of the modulus operator
  9.                                                                                                                              to limit the domain */
  10.     [pool drain];
  11.     return 0;
  12. }

The output of this program is:

  1. Hello
  2. Ifmmp
  3.  KYY_

One thing to note before we analyze this program—the last output only displays four characters because the ASCII value of capital H is 72. The operation results in an ASCII value of 17, which is a “special”, non-printing character (see the Wikipedia link above).

Code, Demystified

The first important line prints out the NSString constant @”Hello”. Not much to it. The next line prints out five characters, as represented by the escape sequences. Looking at the arguments to NSLog, we see that we are simply taking the characters in @”Hello” and adding one to their ASCII values. If you check the output, you’ll find that Ifmmp are just the letters of Hello, but shifted “over” by one. Note that ASCII values define all capitals before lowercase, not all letters in alphabetical order; the letters A-Z come numerically before a-z.

We do the same thing on the Cypher 2 line, except that this time there is a little bit more involved. We take the ASCII value and multiply it by 2. To keep our code neat and prevent warnings, we use the modulus operator to limit the range of the result—in this case, the result can only be between 0 and 127, exactly the range of ASCII. This is another practical use of the modulus operator—to limit the range of a result to a specific set of numbers.

Note that when you write chars in your code, you must delineate them with single quotes. Those quotes do not get printed in the final output.

Type BOOL

Boolean values are very simple—they are literally a YES or NO. In other languages, including Java and plain C, they are represented as true or false. Although initially the latter may seem more reasonable, in usage the former makes more grammatical sense. For example, in the following method call,

  1. [self presentModalViewController:mvc animated:YES];

using the value YES makes much more sense from a reader’s perspective than true.

Booleans are simple true or false values; they are usually used to determine if a condition is true or not, and used to determine whether some code should be executed (for() statements). In Objective-C, the integer values 0 and 1 are synonymous, and can be used interchangeably (with a few exceptions). Their usage will be detailed in the next Lesson.