Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iconv: Converting from Windows ANSI to UTF-8 with BOM

Tags:

unicode

iconv

I want to use iconv to convert files on my Mac. The goal is to go from "Windows ANSI" to "whatever Windows Notepad saves, if you tell it to use UFT8".

This is what I want:

$ file names.csv  names.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators 

This is what I use:

$ iconv -f CP1252 -t UTF-8  names.csv > names.utf8.csv  

This is what I get (not what I want):

$ file names.utf8.csv  names.utf8.csv: UTF-8 Unicode text, with CRLF line terminators 

How do I get the BOM?

like image 911
user531912 Avatar asked Dec 06 '10 07:12

user531912


People also ask

How do I change ANSI file to UTF-8?

3. Choose "UTF-8" from the drop-down box next to "Encoding" and click "Save." Your text file will be converted and saved in the UTF-8 format, although the file extension will remain the same. You can now able open and edit the document at any time and your special characters will be preserved.

How do I convert an ANSI encoded file to UTF-8 with Notepad?

Download and install this powerful free text editor: Notepad++ Open the file you want to verify/fix in Notepad++ In the top menu select Encoding > Convert to UTF-8 (option without BOM) Save the file.


2 Answers

You can add it manually by first echoing the bytes into the file:

echo -ne '\xEF\xBB\xBF' > names.utf8.csv 

and then concatenating your required information at the end:

iconv -f CP1252 -t UTF-8  names.csv >> names.utf8.csv 

Note the >> rather than >.

like image 117
borrible Avatar answered Sep 25 '22 12:09

borrible


Note that "Windows ANSI" may not be CP1252 - that is configured by users.

like image 37
Nemanja Trifunovic Avatar answered Sep 22 '22 12:09

Nemanja Trifunovic