Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File encoding and code page recognition
#4
Macro Macro3198
Code:
Copy      Help
str path.expandpath("$desktop$\Test.txt")
_s.getfile(path) ;;cp gb2312

;Gets the code page for the text encoding
int codePage = CsFunc("" path)
out codePage

_s.ConvertEncoding(codePage 65001) ;;gb2312 to utf8
_s.findreplace("测试" "正式") ;;replace
_s.ConvertEncoding(65001 codePage) ;;UTF8 to gb2312
_s.setfile("$desktop$\Test_ok.txt")



#ret
public static class GuessCoder
{
;;;;public static int DetectCP(string file) {
;;;;;return System.Text.Encoding.GetEncoding(Detect(file)).CodePage;
;;;;}

;;;;public static string Detect(string file)
;;;;{
;;;;;;;;byte[] data=System.IO.File.ReadAllBytes(file);
;;;;;;;;if (data.Length > 2 && data[0] == 0xFF && data[1] == 0xFE){return "Unicode";}
;;;;;;;;if (data.Length > 2 && data[0] == 0xFE && data[1] == 0xFF){return "UTF-16BE";}
;;;;;;;;if (data.Length > 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF){
;;;;;;;;;;;;return "UTF-8";
;;;;;;;;}else{
;;;;;;;;;;;;int charByteCounter = 1;
;;;;;;;;;;;;byte curByte;
;;;;;;;;;;;;for (int i = 0; i < data.Length; i++)
;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;curByte = data[i];
;;;;;;;;;;;;;;;;if (charByteCounter == 1)
;;;;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;;;;if (curByte >= 0x80)
;;;;;;;;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;;;;;;;;while (((curByte <<= 1) & 0x80) != 0)
;;;;;;;;;;;;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;;;;;;;;;;;;charByteCounter++;
;;;;;;;;;;;;;;;;;;;;;;;;}
;;;;;;;;;;;;;;;;;;;;;;;;if (charByteCounter == 1 || charByteCounter > 6)
;;;;;;;;;;;;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;;;;;;;;;;;;return "GB2312";
;;;;;;;;;;;;;;;;;;;;;;;;}
;;;;;;;;;;;;;;;;;;;;}
;;;;;;;;;;;;;;;;}
;;;;;;;;;;;;;;;;else
;;;;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;;;;if ((curByte & 0xC0) != 0x80)
;;;;;;;;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;;;;;;;;return "GB2312";
;;;;;;;;;;;;;;;;;;;;}
;;;;;;;;;;;;;;;;;;;;charByteCounter--;
;;;;;;;;;;;;;;;;}
;;;;;;;;;;;;}
;;;;;;;;;;;;if (charByteCounter > 1)
;;;;;;;;;;;;{
;;;;;;;;;;;;;;;return "GB2312";
;;;;;;;;;;;;}
;;;;;;;;;;;;return "UTF-8";
;;;;;;;;}
;;;;}
}


Messages In This Thread
RE: File encoding and code page recognition - by Gintaras - 07-22-2022, 03:12 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)