Friday, 27 April 2012

Going back from a charset

Charset are useful for parse a many other things, for example:

>> temp: charset "abc"
== make bitset! #{
0000000000000000000000000E00000000000000000000000000000000000000
}
>> type? temp
== bitset!

What characters contains temp?
The following code give you a solution:
REBOL [
    Title: "Decode Charset Function"
    Date: 23-Aug-2001
    Version: 1.0.0
    File: %decode-charset.r
    Author: "Nenad Rakocevic"
    Purpose: "Converts 'charset values to something readable"
    Email: dockimbel@free.fr
    Example: {decode-charset net-utils/url-parser/alpha-num}
]
decode-charset: func [data [bitset!] /local out byte][
    out: make string! 100
    data: to-binary data
    forall data [
        byte: to-integer data/1
        repeat i 8 [
            if 1 = (1 and byte) [
                append out to-char (i - 1) + (8 * ((index? data) - 1))
            ]
            byte: to-integer (byte / 2)
        ]
    ]
    out
]


Example:

>> decode-charset temp
== "abc"
>> decode-charset net-utils/url-parser/alpha-num
== {0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}

No comments:

Post a Comment