Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using iconv to convert from UTF-16LE to UTF-8

Tags:

linux

utf-16le

Hi I am trying to convert some log files from a Microsoft SQL server, but the files are encoded using UTf-16LE and iconv does not seem to be able to convert them.

I am doing:

iconv -f UTF-16LE -t UTF-8 <filename> 

I also tried to delete any carriage returns from the end of the line if there are any, but that did not fix it either. If I save it using gedit that works, but this is not a viable solution since I have hundreds of those files.

EDIT: Please see the new answer for the missing option

like image 509
laitha0 Avatar asked Jun 25 '13 01:06

laitha0


People also ask

How do I use Iconv in Linux?

iconv command is used to convert some text in one encoding into another encoding. If no input file is provided then it reads from standard input. Similarly, if no output file is given then it writes to standard output. If no from-encoding or to-encoding is provided then it uses current local's character encoding.

What is the difference between UTF 16LE and UTF-16BE?

UTF-16BE encoding is identical to the Big-Endian without BOM format of UTF-16 encoding. UTF-16LE encoding is identical to the Little-Endian with BOM format of UTF-16 encoding without using BOM.

What is utf8 UTF-16 utf32?

UTF-8 requires 8, 16, 24 or 32 bits (one to four bytes) to encode a Unicode character, UTF-16 requires either 16 or 32 bits to encode a character, and UTF-32 always requires 32 bits to encode a character.


2 Answers

I forgot the -o switch!

The final command is :

iconv -f UTF-16LE -t UTF-8 <filename> -o <new-filename> 
like image 145
laitha0 Avatar answered Sep 25 '22 08:09

laitha0


The command you specified will output to stdout. You can either use the -o parameter, or redirect your output:

with -o:

iconv -f UTF-16LE -t UTF-8 infile -o outfile 

with piping:

iconv -f UTF-16LE -t UTF-8 infile > outfile 

Both will yield the desired result.

However some versions of iconv (v1 on macOS for example) do not support the -o parameter and you will see that the converted text is echoed to stdout. In that case, use the piping option.

like image 27
A. Blesius Avatar answered Sep 23 '22 08:09

A. Blesius