16

I'm trying to convert the CP1252 encoded string Çàïèñêè ýêñïåäèòîðà to UTF-8. I have tried this command:

iconv -c -f=WINDOWS-1252 -t=UTF-8 test.txt

No luck, getting some weird results:

ÊÀÇÀÃÜ ÃÎÂÛÉ ÂÅÊ

I tried entering the same string (Çàïèñêè ýêñïåäèòîðà) here, and they are able to convert it without problems: http://www.artlebedev.ru/tools/decoder/

What is going wrong?

6 Answers 6

30

When you convert CP1252 encoded string Çàïèñêè ýêñïåäèòîðà to UTF-8 with command iconv.exe -f CP1252 -t UTF-8 test.txt >testout.txt then the source file test.txt (Hex view:

enter image description here

) will be converted into target file testout.txt (Hex view:

enter image description here

) which is UTF-8 code for Çàïèñêè ýêñïåäèòîðà.

Same garbage you put in will come the other end out. iconv's behavior is correct and as expected.

What you are perplexed by is that you don't see what you expect and that is because your input 8bit string is actually encoded in Windows-1251 (Cyrillic) Codepage.

→ So the correct code page is not CP1252 but CP1251

enter image description here

Command iconv.exe -f CP1251 -t UTF-8 test.txt >testout2.txt converts the source file test.txt into target file testout2.txt (Hex view:

enter image description here

) which is UTF-8 code for Записки экспедитора which is what your user's expect to see

9

You ned to use this one:

$ echo "Çàïèñêè ýêñïåäèòîðà" | iconv -t latin1 | iconv -f cp1251
Записки экспедитора
2
  • This doesn't answer the question. The OP specifically asked about converting from CP1252 encoding to UTF-8, and your command uses neither encoding.
    – Edward
    Sep 15, 2014 at 15:44
  • 7
    @Edward the string OP provided is in cp1251, not cp1252, I think he have made a mistake in this part
    – mega.venik
    May 30, 2016 at 17:21
4

My solution:

iconv -f windows-1252 -t utf-8 in.file -o out.file
1
  • 1
    Not working too :) for Çàïèñêè ýêñïåäèòîðà gives Çà ïèñêè ýêñïåäèòîðÃ
    – Somebody
    Dec 13, 2013 at 20:17
0

if you're using linux you should use enconv

./enconv.sh -d /home/foo/example/directory -e ".java" -f "iso-8859-1" -t "utf-8"
0
iconv -f utf8 -t cp1252 file.php | iconv -f cp1251 -t utf8 > file-utf8.php
3
  • 3
    please remember to write some explanation. Nov 5, 2015 at 10:54
  • it is THE command that works for me. The original actions to reproduce warped text are : make some proper cp-1251 textfile, and open it as cp-1252. The utf-8 is just the native universal codepage of the system, so we use it for fringe input and output. Bash pipe and stdout redirection you may figure out on your own May 30 at 10:38
  • The essense of what we do here is the CP1252 --> CP1251 re-interpretation. May 30 at 10:49
-1

try the opposite

  iconv -c -f=UTF-8 -t=WINDOWS-1252 test.txt
0

Your Answer

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

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