As it may be the case that more and more text files get distributed in Unicode,
it may be useful to
know how to convert e.g. UTF-16 to Windows Latin-1 (Codepage 1252) or even from
any encoding to any
other (e.g. Cp850 to Cp1252 and vice-versa).
As Java supports Unicode and codepage/charset translations it is quite easy to
take advantage of it
from ooRexx (via BSF4ooRexx). Just define a routine named e.g. "iconv" like:
-- Recode Rexx string from one encoding to another
::routine iconv
use strict arg str, fromCp, toCp
-- create Java String using Rexx string encoded in the fromCP codepage
jstr=.java.lang.String~new(bsfRawBytes(str),fromCp)
-- get Java byte array in desired toCp codepage and return it as Rexx
string (encoded in toCp)
return bsfRawBytes(jstr~getBytes(toCp))
The arguments "fromCp" and "toCp" may be any string that is defined in
<https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html>. As
you will see all
important sets are defined there.
If you have a need to translate e.g. from "UTF-16" to "Cp1252" then invoke it
with
strNew=iconv(str, "UTF-16", "Cp1252") -- recode the Rexx string referenced
by "str"
If you are in western Europe and have a need for DOS-ASCII files encoded in
Cp850 to convert to
Windows Latin-1 (Cp1252) then invoke with
strNew=iconv(str, "Cp850", "Cp1252") -- recode the Rexx string referenced
by "str"
This should also work fast on very large strings due to employing the external
BSF Rexx function
BsfRawBytes(), which either translates a Rexx string into a Java byte array or
a Java byte array
into a Rexx string using native APIs.
---
The next beta version of BSF4ooRexx will supply a public routine named
"bsf.iconv(str, fromCp,
toCp)" as the need to do conversions from/to Unicode has increased in the past
years considerably.
---rony
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Oorexx-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/oorexx-devel