Home windows How to convert files from UTF-8 to Windows-1251?

How to convert files from UTF-8 to Windows-1251?

Author

Date

Category

OS – Windows.

UPD1. I have a project on C, encoding all UTF-8 files. The bosses asked to do in Win-1251.

UPD2. Before you write here, I googled and also found a bunch of solutions on PHP and for databases, unfortunately, there is no desire to deal with it.

UPD3. About put Linux and convert it. You have come up with a cool, okay, I have a small task, and if someone needs to be converted to any database, also advise Linux?)) 😀


Answer 1, Authority 100%

for f in ~ / files / *; do.
 Cat $ F | ICONV -F UTF-8 -T CP1251 -C -O $ F.Out;
Done

Answer 2, Authority 50%

Again in PHP, using ICONV gives failures.

function cp1251_to_utf8 ($ TXT) {
  $ in_arr = array (
& amp; nbsp; chr (208), chr (192), chr (193), chr (194),
   & amp; nbsp; chr (195), chr (196), chr (197), chr (168),
   & amp; nbsp; CHR (198), CHR (199), CHR (200), CHR (201),
   & amp; nbsp; chr (202), chr (203), chr (204), chr (205),
   & amp; nbsp; chr (206), chr (207), chr (209), chr (210),
   & amp; nbsp; chr (211), chr (212), chr (213), chr (214),
   & amp; nbsp; chr (215), chr (216), chr (217), chr (218),
   & amp; nbsp; chr (219), chr (220), chr (221), chr (222),
   & amp; nbsp; chr (223), chr (224), chr (225), chr (226),
   & amp; nbsp; chr (227), chr (228), chr (229), chr (184),
   & amp; nbsp; chr (230), chr (231), chr (232), chr (233),
   & amp; nbsp; chr (234), chr (235), chr (236), chr (237),
   & amp; nbsp; chr (238), chr (239), chr (240), chr (241),
   & amp; nbsp; chr (242), chr (243), chr (244), chr (245),
   & amp; nbsp; chr (246), chr (247), chr (248), chr (249),
   & amp; nbsp; chr (250), chr (251), chr (252), chr (253),
   & amp; nbsp; chr (254), chr (255)
  );
  $ Out_Arr = Array (
   & amp; nbsp; chr (208) .chr (160), chr (208) .chr (144), CHR (208) .Chr (145),
   & amp; nbsp; chr (208) .chr (146), chr (208) .chr (147), CHR (208) .chr (148),
   & amp; nbsp; chr (208) .chr (149), chr (208) .chr (129), chr (208) .chr (150),
   & amp; nbsp; chr (208) .chr (151), chr (208) .chr (152), CHR (208) .Chr (153),
   & amp; nbsp; chr (208) .chr (154), chr (208) .chr (155), chr (208) .chr (156),
   & amp; nbsp; chr (208) .chr (157), chr (208) .chr (158), chr (208) .chr (159),
   & amp; nbsp; chr (208) .chr (161), chr (208) .chr (162), CHR (208) .chr (163),
   & amp; nbsp; chr (208) .chr (164), chr (208) .chr (165), CHR (208) .chr (166),
   & amp; nbsp; chr (208) .chr (167), chr (208) .chr (168), CHR (208) .chr (169),
   & amp; nbsp; Chr (208) .chr (170), CHR (208) .Chr (171), CHR (208) .chr (172),
   & amp; nbsp; Chr (208) .chr (173), CHR (208) .Chr (174), CHR (208) .chr (175),
   & amp; nbsp; chr (208) .chr (176), chr (208) .chr (177), CHR (208) .chr (178),
   & amp; nbsp; Chr (208) .chr (179), CHR (208) .chr (180), CHR (208) .Chr (181),
   & amp; nbsp; chr (209) .chr (145), chr (208) .chr (182), CHR (208) .chr (183),
   & amp; nbsp; chr (208) .chr (184), chr (208) .chr (185), CHR (208) .chr (186),
   & amp; nbsp; chr (208) .chr (187), chr (208) .chr (188), CHR (208) .chr (189),
   & amp; nbsp; chr (208) .chr (190), CHR (208) .Chr (191), CHR (209) .ChR (128),
   & amp; nbsp; chr (209) .chr (129), chr (209) .chr (130), chr (209) .chr (131),
   & amp; nbsp; chr (209) .chr (132), chr (209) .chr (133), chr (209) .chr (134),
   & amp; nbsp; chr (209) .chr (135), chr (209) .chr (136), chr (209) .chr (137),
   & amp; nbsp; Chr (209) .chr (138), CHR (209) .Chr (139), CHR (209) .ChR (140),
   & amp; nbsp; Chr (209) .chr (141), CHR (209) .Chr (142), CHR (209) .chr (143)
  );
  $ TXT = STR_REPLACE ($ in_arr, $ Out_Arr, $ TXT);
& amp; nbsp; Return $ TXT;
}

source


Answer 3, Authority 50%

If there are dozens of files, but a pair, you can use the standard notepad from the Windows delivery. When saving the file, select “Save As …” and select ANSI encoding. At the same time, naturally, the Russian Lalable should stand in regional settings.
Or enjoy more advanced editors. True, there is no conversion of packet.
In general, specialized Shareware programs under Windows for conversion of the pond pride.


Answer 4, Authority 25%

In the case of the installed PHP parser:

$ curdir = opndir ("...");
While (GETTYPE ($ File = Readdir ($ Curdir))! = 'Boolean')
{
  If ($ File! = "." & amp; & amp; $ file! = ".." & amp; & amp; ereg (". TXT", $ File))
  {
    $ FH = Fopen ($ File, "A +");
    $ TMP = File_Get_Contents ($ File);
    FWRITE ($ FH, ICONV ("UTF-8", "Windows-1251", $ TMP));
    FClose ($ Fh);
  }
}
closedir ($ Curdir);

Converts all * .txt files from this directory.
Of course they must be thrown over to the folder, affordable PCP)


Answer 5, Authority 25%

multiBytetowideChar (CP_UTF8, ...);
WideCHARTOMULTIBYTE (CP_ACP, ...);

as an option to heap 🙂


Answer 6, Authority 25%

you can on Perl. Download Active Perl, install. I checked on the file of 2 lines - everything is OK.
#! perl -w
Use Strict;
Use Encode;
Open (FFile, $ argv [0]);
Open (SFile, "& gt; $ argv [1]");
My $ Line;
While ($ Line = & LT; FFile & GT;) {
  Encode :: From_to ($ Line, "UTF-8", "Windows-1251");
  Print SFile "$ Line";
}
Close FFile;
Close sfile;

We save for example as convert.pl. Run from the console: perl convert.pl utf.txt win1251.txt

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions