Advertisement
Guest User

cpchange

a guest
Jan 16th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.50 KB | None | 0 0
  1. -module(cpchange).
  2.  
  3. -export([cp866_to_cp1251/1, cp1251_to_cp866/1, utf8_to_cp866/1, utf8_to_cp1251/1, cp1251_to_utf8/1, cp866_to_utf8/1]).
  4. %-compile(export_all).
  5.  
  6. cp866_to_cp1251(Msg) ->
  7.     convert({cp866, cp1251, Msg}).
  8.    
  9. cp1251_to_cp866(Msg) ->
  10. %           что, во что, сообщение
  11.     convert({cp1251, cp866, Msg}).
  12.  
  13. utf8_to_cp1251(Msg) ->
  14.     convert({utf8, cp1251, Msg}).
  15.    
  16. utf8_to_cp866(Msg) ->
  17.     convert({utf8, cp866, Msg}).
  18.    
  19. cp866_to_utf8(Msg) ->
  20.     convert({cp866, utf8, Msg}).
  21.    
  22. cp1251_to_utf8(Msg) ->
  23.     convert({cp1251, utf8, Msg}).
  24.    
  25. convert(Data) ->
  26. %         №АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяЁё откр" закр" (в cp866 открывающиесяи закрывающиеся кавычки отсутствуют) длинный- точкаСписка
  27.     CP1251 = [185,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,168,184,171,187,151,149,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,9,10,13],
  28.     CP866 = [252,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,60,62,196,254,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,9,10,13],
  29.     UTF8 = [8470,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,1103,1025,1105,171,187,8212,8226,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,9,10,13],
  30.  
  31.     case Data of
  32. %   что, во что, сообщение
  33.         {cp1251, cp866, Msg} ->
  34.             F = fun(Val) -> lists:nth(string:chr(CP1251, Val), CP866) end,
  35.             lists:map(F, Msg);
  36.        
  37.         {cp866, cp1251, Msg} ->
  38.             F = fun(Val) -> lists:nth(string:chr(CP866, Val), CP1251) end,
  39.             lists:map(F, Msg);
  40.        
  41.         {utf8, cp866, Msg} ->
  42.             F = fun(Val) -> lists:nth(string:chr(UTF8, Val), CP866) end,
  43.             lists:map(F, Msg);
  44.  
  45.         {utf8, cp1251, Msg} ->
  46.             F = fun(Val) -> lists:nth(string:chr(UTF8, Val), CP1251) end,
  47.             lists:map(F, Msg);
  48.  
  49.         {cp1251, utf8, Msg} ->
  50.             F = fun(Val) -> lists:nth(string:chr(CP1251, Val), UTF8) end,
  51.             lists:map(F, Msg);
  52.  
  53.         {cp866, utf8, Msg} ->
  54.             F = fun(Val) -> lists:nth(string:chr(CP866, Val), UTF8) end,
  55.             lists:map(F, Msg)
  56.  
  57.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement