Wednesday 16 November 2011

Chars, string, binary and Rebol

Here a simple script to show the correlation between byte ad their chars representation:

Rebol [
Title: "ASCII Chart"
Author: "Sunanda"
]

hex-lo: copy [across banner "ASCII Chart" return box 25x25 red "\"]
hex-chars: "0123456789ABCDEF"

for n 1 16 1 [
append hex-lo [box 25x25 green]
append hex-lo form hex-chars/:n
]
append hex-lo 'return

for hn 0 15 1 [
append hex-lo [box 25x25 green]
append hex-lo form pick hex-chars (hn + 1)
for ln 0 15 1 [
append hex-lo [box 25x25 blue]
append hex-lo form (to-char 16 * hn + ln)
]
append hex-lo 'return
]

unview/all
view layout hex-lo

It's amazing how is simple to obtain this with Rebol. If you don't know what is an ASCII char, I'll explain in few words: every char is a PC is usually converted in a couple of hexadecimal values (like 42 or B2 ). This table explains how to make conversion, 61 is a. Look at the following example:

>> to-binary "Hello world"
== #{48656C6C6F20776F726C64}
>> to-string #{48}
== "H"
>> to-string #{65}
== "e"
>> to-string #{48656C6C6F}
== "Hello"

No comments:

Post a Comment