Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UTF8 to ANSI with Ruby

I have a Ruby script that generates a UTF8 CSV file remotely in a Linux machine and then transfers the file to a Windows machine thru SFTP.

I then need to open this file with Excel, but Excel doesn't get UTF8, so I always need to open the file in a text editor that has the capability to convert UTF8 to ANSI.

I would love to do this programmatically using Ruby and avoid the manual conversion step. What's the easiest way to do it?

PS: I tried using iconv but had no success.

like image 571
Dema Avatar asked Nov 04 '08 20:11

Dema


People also ask

How do I change ANSI in UTF-8?

Try Settings -> Preferences -> New document -> Encoding -> choose UTF-8 without BOM, and check Apply to opened ANSI files . That way all the opened ANSI files will be treated as UTF-8 without BOM.

Is UTF-8 the same as ANSI?

ANSI and UTF-8 are both encoding formats. ANSI is the common one byte format used to encode Latin alphabet; whereas, UTF-8 is a Unicode format of variable length (from 1 to 4 bytes) which can encode all possible characters.

Is UTF-8 A superset of ANSI?

ANSI and UTF-8 are two character encoding schemes that are widely used at one point in time or another. The main difference between them is use as UTF-8 has all but replaced ANSI as the encoding scheme of choice. UTF-8 was developed to create a more or less equivalent to ANSI but without the many disadvantages it had.

How do I encode in Ruby?

Ruby defaults to UTF-8 as its encoding so if it is opening up files from the operating system and the default is different from UTF-8, it will transcode the input from that encoding to UTF-8. If this isn't desirable, you may change the default internal encoding in Ruby with Encoding.


2 Answers

ascii_str = yourUTF8text.unpack("U*").map{|c|c.chr}.join

assuming that your text really does fit in the ascii character set.

like image 123
AShelly Avatar answered Sep 30 '22 05:09

AShelly


I finally managed to do it using iconv, I was just messing up the parameters. So, this is how you do it:


require 'iconv'

utf8_csv = File.open("utf8file.csv").read

# gotta be careful with the weird parameters order: TO, FROM !
ansi_csv = Iconv.iconv("LATIN1", "UTF-8", utf8_csv).join

File.open("ansifile.csv", "w") { |f| f.puts ansi_csv }

That's it!

like image 37
Dema Avatar answered Sep 30 '22 05:09

Dema