kinx

Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.

This project is maintained by Kray-G

class Iconv

Overview

class Iconv

The Iconv class is used to convert the encoding of text. The constructor will get to and from codepage like this.

var converter = new Iconv({ to: "UTF8", from: "CP932" });

“CP932” is the code page of Shift JIS.

Convert from Text to Text

For example, the following example shows converting from “CP932” to “UTF8”.

var converter = new Iconv({ to: "UTF8", from: "CP932" });
// Unary asterisk will convert a binary to a string.
var sjis = *<0x82, 0x60, 0x82, 0x61, 0x82, 0x62, 0x82, 0x63, 0x82, 0x64, 0x82, 0x65>;
var val = converter.convert(sjis);
System.println(val);    // => ABCDEF

Convert from Binary to Binary

You can also input a binary and output a binary. binmode() means an output is a binary style.

var converter = new Iconv({ to: "UTF8", from: "CP932" });
var sjis = <0x82, 0x60, 0x82, 0x61, 0x82, 0x62, 0x82, 0x63, 0x82, 0x64, 0x82, 0x65>;
converter.binmode();
var val = converter.convert(sjis);
System.println(val);    // => <0xef, 0xbc, 0xa1, 0xef, 0xbc, 0xa2, 0xef, 0xbc, 0xa3, 0xef, 0xbc, 0xa4, 0xef, 0xbc, 0xa5, 0xef, 0xbc, 0xa6>
System.println(*val);   // => ABCDEF

Examples

Example 1. Text to Text

Code

var converter = new Iconv({ to: "UTF8", from: "CP932" });
var sjis = *<0x82, 0x60, 0x82, 0x61, 0x82, 0x62, 0x82, 0x63, 0x82, 0x64, 0x82, 0x65>;
var val = converter.convert(sjis);  // val is a string.
System.println(<...val>);

Result

<0xef, 0xbc, 0xa1, 0xef, 0xbc, 0xa2, 0xef, 0xbc, 0xa3, 0xef, 0xbc, 0xa4, 0xef, 0xbc, 0xa5, 0xef, 0xbc, 0xa6>

Example 2. Text to Binary

Code

var converter = new Iconv({ to: "UTF8", from: "CP932" });
// Unary asterisk will convert a binary to a string.
var sjis = *<0x82, 0x60, 0x82, 0x61, 0x82, 0x62, 0x82, 0x63, 0x82, 0x64, 0x82, 0x65>;
converter.binmode();
var val = converter.convert(sjis);
System.println(val);

Result

<0xef, 0xbc, 0xa1, 0xef, 0xbc, 0xa2, 0xef, 0xbc, 0xa3, 0xef, 0xbc, 0xa4, 0xef, 0xbc, 0xa5, 0xef, 0xbc, 0xa6>

Example 3. Binary to Text

Code

var converter = new Iconv({ to: "UTF8", from: "CP932" });
var sjis = <0x82, 0x60, 0x82, 0x61, 0x82, 0x62, 0x82, 0x63, 0x82, 0x64, 0x82, 0x65>;
var val = converter.convert(sjis);  // val is a string.
System.println(<...val>);

Result

<0xef, 0xbc, 0xa1, 0xef, 0xbc, 0xa2, 0xef, 0xbc, 0xa3, 0xef, 0xbc, 0xa4, 0xef, 0xbc, 0xa5, 0xef, 0xbc, 0xa6>

Example 2. Binary to Binary

Code

var converter = new Iconv({ to: "UTF8", from: "CP932" });
// Unary asterisk will convert a binary to a string.
var sjis = <0x82, 0x60, 0x82, 0x61, 0x82, 0x62, 0x82, 0x63, 0x82, 0x64, 0x82, 0x65>;
converter.binmode();
var val = converter.convert(sjis);
System.println(val);

Result

<0xef, 0xbc, 0xa1, 0xef, 0xbc, 0xa2, 0xef, 0xbc, 0xa3, 0xef, 0xbc, 0xa4, 0xef, 0xbc, 0xa5, 0xef, 0xbc, 0xa6>