简体   繁体   中英

Transcoding a string from UTF-8 to ASCII-8BIT in Ruby?

I have to print Ruby strings UTF-8 encoded, containing Italian language sentences, to a ESC/POS thermal printer (a printer that accept only ASCII-8BIT (1 byte) charset: http://maxdb.sap.com/doc/7_6/ca/bd35406ee32e34e10000000a155106/content.htm ).

BTW, I use Ruby 2.x (on Windows or Linux). I'm confused about how to transcode,

by example let say the string contained in a JSON UTF-8 enccoded on a remote server, or contained in a template file as:

#!/bin/env ruby
# encoding: utf-8

string = "Però non è la città di Barnabù"

I have to translate the string (accented / internationalized 2 bytes) in 1 byte ('ASCII8-BIT' encodded)

Any suggestion to how to do transaltion FROM UTF-8 TO ASCII8-BIT?

I lost myself with methods like: .force_encoding('ASCII-8BIT'), or encode(") ...

EDIT: Epson TM-T20打印机的字符集表

many thanks giorgio

I find the solution as:

string.encode "IBM437"

as said in comment, I reread the Epson TM-T20 printer; It's setted with default "code table" number: 0, meanining: "PC437: USA, Standard Europe"

In fact I understand PC437 refer to the 2Code Page 437', see: en.wikipedia.org/wiki/Code_page_437

So i found the interesting gem: github.com/ConradIrwin/encoding-codepage

showing that Code page 437 correspond to charset "IBM437"

Now printer print correctly! I answer here myself to maybe help others.

giorgio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM