Character codes

From Rosetta Code
Revision as of 08:30, 17 November 2008 by rosettacode>Spoon! (started new task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Character codes
You are encouraged to solve this task according to the task description, using any language you may know.

Given a character value in your language, print its code (could be ASCII code, Unicode code, or whatever your language uses). For example, the character 'a' (lowercase letter A) has a code of 97 in ASCII (as well as Unicode, as ASCII forms the beginning of Unicode). Conversely, given a code, print out the corresponding character.

C

char is already an integer type in C, and it gets automatically promoted to int. So you can use a character where you would otherwise use an integer. Conversely, you can use an integer where you would normally use a character, except you may need to cast it, as char is smaller.

<c>#include <stdio.h>

int main() {

 printf("%d\n", 'a'); /* prints "97" */
 printf("%c\n", 97); /* prints "a"; we don't have to cast because printf is type agnostic */
 return 0;

}</c>

C++

char is already an integer type in C++, and it gets automatically promoted to int. So you can use a character where you would otherwise use an integer. Conversely, you can use an integer where you would normally use a character, except you may need to cast it, as char is smaller.

In this case, the output operator << is overloaded to handle integer (outputs the decimal representation) and character (outputs just the character) types differently, so we need to cast it in both cases. <cpp>#include <iostream>

int main() {

 std::cout << (int)'a' << std::endl; // prints "97"
 std::cout << (char)97 << std::endl; // prints "a"
 return 0;

}</cpp>

Common Lisp

<lisp>(princ (char-code #\a)) ; prints "97" (princ (code-char 97)) ; prints "a"</lisp>

Haskell

import Data.Char

main = do
  print (ord 'a') -- prints "97"
  print (chr 97) -- prints "a"

Java

char is already an integer type in Java, and it gets automatically promoted to int. So you can use a character where you would otherwise use an integer. Conversely, you can use an integer where you would normally use a character, except you may need to cast it, as char is smaller.

In this case, the println method is overloaded to handle integer (outputs the decimal representation) and character (outputs just the character) types differently, so we need to cast it in both cases. <java>public class Foo {

   public static void main(String[] args) {
       System.out.println((int)'a'); // prints "97"
       System.out.println((char)97); // prints "a"
   }

}</java>

Java characters support Unicode: <java>public class Bar {

   public static void main(String[] args) {
       System.out.println((int)'π'); // prints "960"
       System.out.println((char)960); // prints "π"
   }

}</java>

JavaScript

Here character is just a string of length 1 <javascript>document.write('a'.charCodeAt(0)); // prints "97" document.write(String.fromCharCode(97)); // prints "a"</javascript>

OCaml

<ocaml>Printf.printf "%d\n" (int_of_char 'a'); (* prints "97" *) Printf.printf "%c\n" (char_of_int 97); (* prints "a" *)</ocaml>

Perl

Here character is just a string of length 1 <perl>print ord('a'), "\n"; # prints "97" print chr(97), "\n"; # prints "a"</perl>

PHP

Here character is just a string of length 1 <php>echo ord('a'), "\n"; // prints "97" echo chr(97), "\n"; // prints "a"</php>

Python

Here character is just a string of length 1 <python>print ord('a') # prints "97" print chr(97) # prints "a"</python>

Ruby

In Ruby currently characters are usually represented directly as their integer character code. Ruby has a syntax for "character literal" which evaluates directly to the integer code: ?a evaluates to the integer 97. Subscripting a string also gives just the integer code for the character.

print ?a # prints "97" print 'a'[0] # prints "97" print 97.chr # prints "a"; "91.chr" returns a string of length 1

Scheme

<scheme>(display (char->integer #\a)) (newline) ; prints "97" (display (integer->char 97)) (newline) ; prints "a"</scheme>