3

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: charset table of Epson TM-T20 printer

many thanks giorgio

5
  • 1
    There is no such thing as ASCII8-Bit AFAICT. What you linked is one of many charsets based on ASCII, defining 128 additional characters with the eigth bit set. Find out the actual charset you want, and it should be simple. Apr 22, 2014 at 14:16
  • By quick glance, the table you linked to represents ISO 8859-1.
    – tripleee
    Apr 22, 2014 at 14:40
  • string.encode('iso-8859-1') returns "Per\xF2 non \xE8 la citt\xE0 di Barnab\xF9" is that what you want?
    – Uri Agassi
    Apr 22, 2014 at 14:42
  • 8859-1 is ancient, it dates back from before the introduction of the Euro. 8859-15 is the replacement, it is largely identical, except it has the € symbol at the code point where -1 has the international currency symbol (which almost nobody ever seriously uses anyway). Apr 22, 2014 at 14:48
  • sorry, the table I linked is evidently not that of Epson TM-T20 printer I'm using. I so edited my post inserting a photo of charset table; by example "è" corrspond to \x8a ; how know the encoding ? Maybe a CP437 ? en.wikipedia.org/wiki/Code_page_437 Interesting that gem github.com/ConradIrwin/encoding-codepage showing that Code page 437 correspond to charset IBM437 ... Now printer print correctly ... nevertheless I0m still confused about code pages, charsets encodings... Apr 22, 2014 at 15:06

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.