Character codes

From Rosetta Code
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.

Ada

<ada> with Ada.Text_IO; use Ada.Text_IO;

procedure Char_Code is begin

  Put_Line (Character'Val (97) & " =" & Integer'Image (Character'Pos ('a')));

end Char_Code; </ada> The predefined language attributes S'Pos and S'Val for every discrete subtype, and Character is such a type, yield the position of a value and value by its position correspondingly. Sample output.

a = 97

ALGOL 68

In ALGOL 68 the FORMAT $g$ is type aware, hence the type conversion operators ABS & REPR are used to set the type.

main:(
  printf(($gl$, ABS "a")); # for ASCII this prints "+97" EBCDIC prints "+129" #
  printf(($gl$, REPR 97))  # for ASCII this prints "a"; EBCDIC prints "/" #
)

Character conversions may be available in the standard preclude so that when a foreign tape is mounted, the characters will be converted transparently as the tape's records are read.

FILE tape;
INT errno = open(tape, "/dev/tape1", stand out channel)
make conv(tape, ebcdic conv);
FOR record DO getf(tape, ( ~ )) OD; ~ # etc ... #

Every CHANNEL has an associated standard character conversion that can be determined using the stand conv query routine and then the conversion applied to a particular file/tape. eg.

make conv(tape, stand conv(stand out channel))

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>

Forth

As with C, characters are just integers on the stack which are treated as ASCII.

char a
dup .    \ 97
emit     \ a

Haskell

import Data.Char

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

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>

Logo characters are words of length 1.

print ascii "a    ; 97
print char 97     ; a

OCaml

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

Pascal

<pascal>writeln(ord('a')); writeln(chr(97));</pascal>

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>

For Unicode: <python>print ord(u'π') # prints "960" print unichr(960) # prints "π"</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>