This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1 @@
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.

View file

@ -0,0 +1,5 @@
---
category:
- Basic language learning
- String manipulation
note: Text processing

View file

@ -0,0 +1,7 @@
report zcharcode
data: c value 'A', n type i.
field-symbols <n> type x.
assign c to <n> casting.
move <n> to n.
write: c, '=', n left-justified.

View file

@ -0,0 +1,2 @@
(cw "~x0" (char-code #\a))
(cw "~x0" (code-char 97))

View file

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

View file

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

View file

@ -0,0 +1 @@
make conv(tape, stand conv(stand out channel))

View file

@ -0,0 +1,13 @@
function ord(c)
{
return chmap[c]
}
BEGIN {
for(i=0; i < 256; i++) {
chmap[sprintf("%c", i)] = i
}
print ord("a"), ord("b")
printf "%c %c\n", 97, 98
s = sprintf("%c%c", 97, 98)
print s
}

View file

@ -0,0 +1,2 @@
trace(String.fromCharCode(97)); //prints 'a'
trace("a".charCodeAt(0));//prints '97'

View file

@ -0,0 +1,6 @@
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;

View file

@ -0,0 +1,6 @@
# prints "97"
o_integer('a');
o_byte('\n');
# prints "a"
o_byte(97);
o_byte('\n');

View file

@ -0,0 +1,2 @@
MsgBox % Chr(97)
MsgBox % Asc("a")

View file

@ -0,0 +1,4 @@
charCode = 97
char = "a"
PRINT CHR$(charCode) 'prints a
PRINT ASC(char) 'prints 97

View file

@ -0,0 +1,4 @@
10 LET c = 97: REM c is a character code
20 LET d$ = "b": REM d$ holds the character
30 PRINT CHR$(c): REM this prints a
40 PRINT CODE(d$): REM this prints 98

View file

@ -0,0 +1,4 @@
charCode = 97
char$ = "a"
PRINT CHR$(charCode) : REM prints a
PRINT ASC(char$) : REM prints 97

View file

@ -0,0 +1 @@
"A". 88*1+,

View file

@ -0,0 +1,7 @@
#include <iostream>
int main() {
std::cout << (int)'a' << std::endl; // prints "97"
std::cout << (char)97 << std::endl; // prints "a"
return 0;
}

View file

@ -0,0 +1,7 @@
#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;
}

View file

@ -0,0 +1,6 @@
(print (int \a)) ; prints "97"
(print (char 97)) ; prints \a
; Unicode is also available, as Clojure uses the underlying java Strings & chars
(print (int )) ; prints 960
(print (char 960)) ; prints \π

View file

@ -0,0 +1,2 @@
console.log 'a'.charCodeAt 0 # 97
console.log String.fromCharCode 97 # a

View file

@ -0,0 +1,2 @@
(princ (char-code #\a)) ; prints "97"
(princ (code-char 97)) ; prints "a"

View file

@ -0,0 +1,13 @@
import std.stdio, std.utf;
void main() {
string test = "a";
size_t index = 0;
// Get four-byte utf32 value for index 0
// this returns dchar, so cast it to numeric.
writeln(cast(uint)test.decode(index));
// 'index' has moved to next character position in input.
assert(index == 1);
}

View file

@ -0,0 +1,2 @@
PrintLn(Ord('a'));
PrintLn(Chr(97));

View file

@ -0,0 +1,19 @@
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
aChar:Char;
aCode:Byte;
uChar:WideChar;
uCode:Word;
begin
aChar := Chr(97); Writeln(aChar);
aCode := Ord(aChar); Writeln(aCode);
uChar := WideChar(97); Writeln(uChar);
uCode := Ord(uChar); Writeln(uCode);
Readln;
end.

View file

@ -0,0 +1,5 @@
? 'a'.asInteger()
# value: 97
? <import:java.lang.makeCharacter>.asChar(97)
# value: 'a'

View file

@ -0,0 +1,8 @@
#define std'dictionary'*.
#define std'basic'*.
#symbol Program =>
[
'program'output << WideCharVar::"a" numeric.
'program'output << WideCharVar::97.
].

View file

@ -0,0 +1,4 @@
1> F = fun([X]) -> X end.
#Fun<erl_eval.6.13229925>
2> F("a").
97

View file

@ -0,0 +1,2 @@
3> $a.
97

View file

@ -0,0 +1,2 @@
printf(1,"%d\n", 'a') -- prints "97"
printf(1,"%s\n", 97) -- prints "a"

View file

@ -0,0 +1,3 @@
char a
dup . \ 97
emit \ a

View file

@ -0,0 +1,2 @@
WRITE(*,*) ACHAR(97), IACHAR("a")
WRITE(*,*) CHAR(97), ICHAR("a")

View file

@ -0,0 +1,2 @@
fmt.Println('a') // prints "97"
fmt.Println('π') // prints "960"

View file

@ -0,0 +1,16 @@
package main
import "fmt"
func main() {
// yes, there is more concise syntax, but this makes
// the data types very clear.
var b byte = 'a'
var r rune = 'π'
var s string = "aπ"
fmt.Println(b, r, s)
for _, c := range s { // this gives c the type rune
fmt.Println(c)
}
}

View file

@ -0,0 +1,3 @@
b := byte(97)
r := rune(960)
fmt.Printf("%c %c\n%c %c\n", 97, 960, b, r)

View file

@ -0,0 +1,3 @@
fmt.Println(string(97)) // prints "a"
fmt.Println(string(960)) // prints "π"
fmt.Println(string([]rune{97, 960})) // prints "aπ"

View file

@ -0,0 +1,7 @@
import Data.Char
main = do
print (ord 'a') -- prints "97"
print (chr 97) -- prints "'a'"
print (ord 'π') -- prints "960"
print (chr 960) -- prints "'\960'"

View file

@ -0,0 +1,6 @@
public class Foo {
public static void main(String[] args) {
System.out.println((int)'a'); // prints "97"
System.out.println((char)97); // prints "a"
}
}

View file

@ -0,0 +1,6 @@
public class Bar {
public static void main(String[] args) {
System.out.println((int)'π'); // prints "960"
System.out.println((char)960); // prints "π"
}
}

View file

@ -0,0 +1,2 @@
document.write('a'.charCodeAt(0)); // prints "97"
document.write(String.fromCharCode(97)); // prints "a"

View file

@ -0,0 +1,2 @@
print(string.byte("a")) -- prints "97"
print(string.char(97)) -- prints "a"

View file

@ -0,0 +1 @@
character = char(asciiNumber)

View file

@ -0,0 +1 @@
asciiNumber = double(character)

View file

@ -0,0 +1,3 @@
asciiNumber = uint16(character)
asciiNumber = uint32(character)
asciiNumber = uint64(character)

View file

@ -0,0 +1,17 @@
>> char(87)
ans =
W
>> double('W')
ans =
87
>> uint16('W')
ans =
87

View file

@ -0,0 +1,2 @@
echo ord('a'), "\n"; // prints "97"
echo chr(97), "\n"; // prints "a"

View file

@ -0,0 +1,2 @@
print ord('a'), "\n"; # prints "97"
print chr(97), "\n"; # prints "a"

View file

@ -0,0 +1,10 @@
: (char "a")
-> 97
: (char "字")
-> 23383
: (char 23383)
-> "字"
: (chop "文字")
-> ("文" "字")
: (mapcar char @)
-> (25991 23383)

View file

@ -0,0 +1,2 @@
print ord('a') # prints "97"
print chr(97) # prints "a"

View file

@ -0,0 +1,2 @@
print ord(u'π') # prints "960"
print unichr(960) # prints "π"

View file

@ -0,0 +1,4 @@
print(ord('a')) # prints "97"
print(ord('π')) # prints "960"
print(chr(97)) # prints "a"
print(chr(960)) # prints "π"

View file

@ -0,0 +1,2 @@
ascii <- as.integer(charToRaw("hello world")); ascii
text <- rawToChar(as.raw(ascii)); text

View file

@ -0,0 +1,14 @@
yyy='c' /*assign a lowercase c to YYY.*/
yyy='34'x /*assign hexadecimal 34 to YYY.*/
/*the X can be upper/lowercase.*/
yyy=x2c(34) /* (same as above) */
yyy='00110100'b /* (same as above) */
yyy='0011 0100'b /* (same as above) */
/*the B can be upper/lowercase.*/
yyy=d2c(97) /*assign decimal code 97 to YYY.*/
say yyy /*displays the value of YYY. */
say c2x(yyy) /*displays the value of YYY in hexadecimal. */
say c2d(yyy) /*displays the value of YYY in decimal. */
say x2b(c2x(yyy)) /*displays the value of YYY in binary (bit string). */
/*Note: some REXXes support the c2b bif */

View file

@ -0,0 +1,6 @@
> ?a
=> 97
> "a"[0]
=> 97
> 97.chr
=> "a"

View file

@ -0,0 +1,4 @@
> "a".ord
=> 97
> 97.chr
=> "a"

View file

@ -0,0 +1,7 @@
class MAIN is
main is
#OUT + 'a'.int + "\n"; -- or
#OUT + 'a'.ascii_int + "\n";
#OUT + CHAR::from_ascii_int(97) + "\n";
end;
end;

View file

@ -0,0 +1,5 @@
scala> 'a' toInt
res9: Int = 97
scala> 97 toChar
res10: Char = a

View file

@ -0,0 +1,7 @@
def charToInt(c: Char, next: Char): Option[Int] = (c, next) match {
case _ if (c.isHighSurrogate && next.isLowSurrogate) => Some(java.lang.Character.toCodePoint(c, next))
case _ if (c.isLowSurrogate) => None
case _ => Some(c.toInt)
}
def intToChars(n: Int): Array[Char] = java.lang.Character.toChars(n)

View file

@ -0,0 +1,2 @@
(display (char->integer #\a)) (newline) ; prints "97"
(display (integer->char 97)) (newline) ; prints "a"

View file

@ -0,0 +1,2 @@
($a asInteger) displayNl. "output 97"
(Character value: 97) displayNl. "output a"

View file

@ -0,0 +1,6 @@
# ASCII
puts [scan "a" %c] ;# ==> 97
puts [format %c 97] ;# ==> a
# Unicode is the same
puts [scan "π" %c] ;# ==> 960
puts [format %c 960] ;# ==> π