rubycharacter-encodingalpine-linuxiconv

How to add CP932 charset to iconv in Alpine


I want to convert CP932 string to UTF-8.

In Ubuntu I can convert string by a command

iconv -f CP932 -t UTF-8 [input_file]

But the iconv in Apline 3.17 fails with a error: iconv: source charset CP932: Invalid argument.

How can I add CP932 charset to iconv?

The command is called from a ruby script so I don't adhere iconv if there is better way to convert string CP932 to UTF-8 with ruby or another command line tool in Alpine.


Solution

  • Compile iconv from source rather than installing it with a package.

    1. Install the utilities necessary to build from source:
    apk add --no-cache curl build-base
    
    1. Download the source:
    cd /tmp
    curl -O https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz
    tar xvf libiconv-1.17.tar.gz
    cd libiconv-1.17/
    
    1. Configure, compile and install iconv to /usr/local/bin:
    ./configure
    make
    make install
    
    1. Remove the downloaded source:
    cd /tmp
    rm -rf libiconv-1.17/
    rm libiconv-1.17.tar.gz
    

    Now you can run iconv successfully with CP932. Here's an example:

    1. Create a new UTF-8 file:
    echo "潤" > foo
    
    1. Confirm it is UTF-8:
    file -i foo
    foo: text/plain; charset=utf-8
    
    1. Convert it to CP932:
    iconv -f UTF-8 -t CP932 foo > foo.cp932
    
    1. Confirm it has been converted:
    file -i foo.cp932
    foo.cp932: text/plain; charset=unknown-8bit
    

    and:

    cat foo.cp932
    ��
    
    1. Convert CP932 back to UTF-8:
    iconv -f CP932 -t UTF-8 foo.cp932 > foo.utf8
    
    1. Confirm it has been properly converted:
    file -i foo.utf8
    foo.utf8: text/plain; charset=utf-8
    

    and:

    cat foo.utf8
    潤
    

    and:

    md5sum foo
    54282143d705814d6ba671b783f2e0ba  foo
    
    md5sum foo.utf8
    54282143d705814d6ba671b783f2e0ba  foo.utf8