Last active
August 29, 2015 14:11
-
-
Save hamdouni/c6fff2d4f057389487c3 to your computer and use it in GitHub Desktop.
Rebol Convert UTF8 to LATIN1 (ISO8859-1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rebol [] | |
decode: func [ | |
{ | |
Decode a UTF-8 encoded string into latin1. | |
Adapted from http://www.rebol.org/view-script.r?script=utf-8.r | |
} | |
xs [string!] | |
/local m x c result [string!] | |
][ | |
udata: [0 192 224 240 248 252] | |
result: make string! (length? xs) | |
while [not tail? xs][ | |
x: first xs | |
either x < 128 [ | |
insert tail result x | |
][ | |
m: 8 - length? find enbase/base to binary! x 2 #"0" | |
x: x xor pick udata m | |
loop m - 1 [x: 64 * x + (63 and first xs: next xs)] | |
insert tail result to char! x | |
] | |
xs: next xs | |
] | |
head result | |
] | |
t: read %utf8.txt | |
print t | |
t: decode t | |
print t | |
view layout [ h1 t btn "quit" [unview]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment