Character codes: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (Using lang tags now.)
Line 18: Line 18:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
In ALGOL 68 the FORMAT $g$ is type aware, hence the type conversion operators ABS & REPR are used to set the type.
In ALGOL 68 the FORMAT $g$ is type aware, hence the type conversion operators ABS & REPR are used to set the type.
main:(
<lang algol> main:(
printf(($gl$, ABS "a")); # for ASCII this prints "+97" EBCDIC prints "+129" #
printf(($gl$, ABS "a")); # for ASCII this prints "+97" EBCDIC prints "+129" #
printf(($gl$, REPR 97)) # for ASCII this prints "a"; EBCDIC prints "/" #
printf(($gl$, REPR 97)) # for ASCII this prints "a"; EBCDIC prints "/" #
)
)</lang>
''Character conversions'' may be available in the ''standard preclude'' so that when
''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
a foreign tape is mounted, the characters will be converted transparently as the tape's
records are read.
records are read.
FILE tape;
<lang algol> FILE tape;
INT errno = open(tape, "/dev/tape1", stand out channel)
INT errno = open(tape, "/dev/tape1", stand out channel)
make conv(tape, ebcdic conv);
make conv(tape, ebcdic conv);
FOR record DO getf(tape, ( ~ )) OD; ~ # etc ... #
FOR record DO getf(tape, ( ~ )) OD; ~ # etc ... #</lang>
Every CHANNEL has an associated standard character conversion that can be determined
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
using the ''stand conv'' query routine and then the conversion applied to a particular
file/tape. eg.
file/tape. eg.
make conv(tape, stand conv(stand out channel))
<lang algol> make conv(tape, stand conv(stand out channel))</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 89: Line 89:
=={{header|Forth}}==
=={{header|Forth}}==
As with C, characters are just integers on the stack which are treated as ASCII.
As with C, characters are just integers on the stack which are treated as ASCII.
char a
<lang forth>char a
dup . \ 97
dup . \ 97
emit \ a
emit \ a</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
Functions ACHAR and IACHAR specifically work with the ASCII character set, while the results of CHAR and ICHAR will depend on the default character set being used.
Functions ACHAR and IACHAR specifically work with the ASCII character set, while the results of CHAR and ICHAR will depend on the default character set being used.
WRITE(*,*) ACHAR(97), IACHAR("a")
<lang fortran>WRITE(*,*) ACHAR(97), IACHAR("a")
WRITE(*,*) CHAR(97), ICHAR("a")
WRITE(*,*) CHAR(97), ICHAR("a")</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==


<lang haskell>
<pre>
import Data.Char
import Data.Char


Line 108: Line 108:
print (ord 'π') -- prints "960"
print (ord 'π') -- prints "960"
print (chr 960) -- prints "'\960'"
print (chr 960) -- prints "'\960'"
</pre>
</lang>


=={{header|J}}==
=={{header|J}}==
cc=: {&a. :: (a.&i.)
<lang j>cc=: {&a. :: (a.&i.)</lang>
Examples of use:
Examples of use:
cc 'a'
cc 'a'
Line 148: Line 148:
=={{header|Logo}}==
=={{header|Logo}}==
Logo characters are words of length 1.
Logo characters are words of length 1.
print ascii "a ; 97
<lang logo>print ascii "a ; 97
print char 97 ; a
print char 97 ; a</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 203: Line 203:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==


Console.WriteLine(Chr(97)) 'Prints a
<lang vbnet>Console.WriteLine(Chr(97)) 'Prints a
Console.WriteLine(Asc("a")) 'Prints 97
Console.WriteLine(Asc("a")) 'Prints 97</lang>

Revision as of 07:19, 12 February 2009

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

<lang 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; </lang> 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. <lang algol> main:(

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

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. <lang algol> FILE tape;

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

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. <lang algol> make conv(tape, stand conv(stand out channel))</lang>

BASIC

Works with: QuickBasic version 4.5

<lang qbasic>charCode = 97 char = "a" PRINT CHR$(charCode) 'prints a PRINT ASC(char) 'prints 97</lang>

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.

<lang 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;

}</lang>

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. <lang cpp>#include <iostream>

int main() {

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

}</lang>

C#

C# represents strings and characters internally as Unicode, so casting a char to an int returns its Unicode character encoding. <lang csharp> using System;

namespace RosettaCode.CharacterCode {

   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine((int) 'a');   //Prints "97"
           Console.WriteLine((char) 97);   //Prints "a"
       }
   }

} </lang>

Common Lisp

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

Forth

As with C, characters are just integers on the stack which are treated as ASCII. <lang forth>char a dup . \ 97 emit \ a</lang>

Fortran

Functions ACHAR and IACHAR specifically work with the ASCII character set, while the results of CHAR and ICHAR will depend on the default character set being used. <lang fortran>WRITE(*,*) ACHAR(97), IACHAR("a") WRITE(*,*) CHAR(97), ICHAR("a")</lang>

Haskell

<lang 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'"

</lang>

J

<lang j>cc=: {&a. :: (a.&i.)</lang> Examples of use:

   cc 'a'
97
   cc 97
a
   cc 'alphabet'
97 108 112 104 97 98 101 116
   cc 99+i.11
cdefghijklm
 

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. <lang java>public class Foo {

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

}</lang>

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

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

}</lang>

JavaScript

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

Logo characters are words of length 1. <lang logo>print ascii "a  ; 97 print char 97  ; a</lang>

OCaml

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

Pascal

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

Perl

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

PHP

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

Python

2.x

Here character is just a string of length 1

8-bit characters: <lang python>print ord('a') # prints "97" print chr(97) # prints "a"</lang>

Unicode characters: <lang python>print ord(u'π') # prints "960" print unichr(960) # prints "π"</lang>

3.x

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

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.

<lang ruby>print ?a, "\n" # prints "97" print 'a'[0], "\n" # prints "97" print 97.chr, "\n" # prints "a"; "91.chr" returns a string of length 1</lang>

Scheme

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

Visual Basic .NET

<lang vbnet>Console.WriteLine(Chr(97)) 'Prints a Console.WriteLine(Asc("a")) 'Prints 97</lang>