UTF-8 encode and decode: Difference between revisions

→‎{{header|F_Sharp|F#}}: inserted Forth section after F#
(→‎{{header|F_Sharp|F#}}: inserted Forth section after F#)
Line 483:
for n in fN 0x1d11e do printf "%x " n -> f0 9d 84 9e
</pre>
 
=={{header|Forth}}==
Forth-2012 includes the words <code>xc!+</code> and <code>xc@+</code> that convert code point numbers into bytes and vice versa, respectively, for the encoding the system is using. If the system uses UTF-8 (e.g., in Gforth, if it is started in a UTF-8 environment), this converts between code point number and UTF-8.
 
<p>So the following code just uses these words to perform the tests.
{{works with|gforth|0.7.9_20191121}}
{{works with|lxf|1.6-982-823}}
<lang forth>
: showbytes ( c-addr u -- )
over + swap ?do
i c@ 3 .r loop ;
 
: test {: xc -- :}
xc xemit xc 6 .r xc pad xc!+ pad tuck - ( c-addr u )
2dup showbytes drop xc@+ xc <> abort" test failed" drop cr ;
 
hex
$41 test $f6 test $416 test $20ac test $1d11e test
\ can also be written as
\ 'A' test 'ö' test 'Ж' test '€' test '𝄞' test
</lang>
{{out}}
<pre>
A 41 41
ö F6 C3 B6
Ж 416 D0 96
€ 20AC E2 82 AC
𝄞 1D11E F0 9D 84 9E
</pre>
 
=={{header|Go}}==
===Implementation===
19

edits