中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 15:59
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » [Original] GBK & UTF8 Encoding Conversion Script (CMD + GAWK)
Printable Version  18,782 / 31
Floor1 无奈何 Posted 2006-11-30 00:31
荣誉版主 Posts 356 Credits 1,338
GBK & UTF8 Encoding Conversion Script (CMD+GAWK)

Because in my actual use I needed UTF8-to-GBK encoding conversion, I wrote one with GAWK. In fact, I had already been using it in an earlier post. This time I整理ed it a bit, and it now supports bidirectional encoding conversion. I made a complete GBK-to-UTF8 conversion mapping table myself. During the process I found that there are quite a few differences between the system's conversion results and iconv's conversion results, and the mapping table uses the former.
This script supports encoding conversion through pipes and files, with the result output to the screen. There are not many parameters supported yet, but it does have parameter integrity checking and can handle unordered calls with multiple parameters. It uses a new script release method: when the source file is modified, the script will be updated automatically. There are also error messages and a dependency-file integrity checking mechanism. I hope these little tricks will be helpful to everyone in writing batch files.
GAWK download link: http://www.klabaster.com/progs/gawk32.zip
The script and conversion table are in the attachment.


  1. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. :: gbk2utf8.cmd -V0.1 -- GBK & UTF8 encoding conversion
  3. :: 无奈何@cn-dos.net - 2006-11-28 - CMD & GAWK
  4. :: Usage: gbk2utf8 /I file...
  5. :: Supported files: - gawk.exe gbk2utf8.dat
  6. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  7. @echo off
  8. setlocal
  9. set self="%~f0"
  10. set AwkScript="%temp%\%~n0%~z0.awk"
  11. set path=%path%;%~dp0;%cd%
  12. set nofile=
  13. set error=
  14. set input=

  15. ::Dependency file integrity check
  16. for %%i in (gawk.exe gbk2utf8.dat) do (
  17. @if "%%~$PATH:i" == "" (
  18. echo.The required dependency file "%%i" is missing.
  19. set nofile=1
  20. ) else ( set %%~ni="%%~$PATH:i" )
  21. )
  22. if defined nofile goto :EOF
  23. ::Update script after file changes
  24. if not exist %AwkScript% (
  25. del /q "%temp%\%~n0*.awk" 2>nul
  26. gawk "/^#<-1/,/^#>-1/{if(!/^#/)print}" %self% >%AwkScript%
  27. )

  28. :ParseLoop
  29. if "%~1" == "" goto Start
  30. if "%~1" == "?" goto SwitchH
  31. if "%~1" == "/?" goto SwitchH
  32. rem Process parameters and jump to the corresponding label.
  33. for %%s in (U u I i h H) do if "%~1"=="/%%s" goto Switch%%s
  34. if "%F_input%" == "1" (
  35. if not exist "%~1" set error=Warning: file "%~1" does not exist. & goto error
  36. set input=%input% "%~1"
  37. shift
  38. goto ParseLoop
  39. )
  40. if "%F_input%" == "-1" shift & goto ParseLoop
  41. set error=Error: incorrect parameter format - "%1" !
  42. goto error

  43. :SwitchI
  44. set F_input=1
  45. if "%~2" == "-" set F_input=-1
  46. shift
  47. goto ParseLoop

  48. :SwitchU
  49. set F=-1
  50. shift
  51. goto ParseLoop

  52. :error
  53. echo.%error%
  54. echo.
  55. :SwitchH
  56. echo.gbk2utf8 V0.1 -- GBK ^& UTF8 encoding conversion
  57. echo.
  58. echo.Usage: 1、%~n0
  59. echo. 2、%~n0 /I file...
  60. echo. 3、%~n0 /I -
  61. echo.
  62. echo.Options: /? displays this brief help, equivalent to /H .
  63. echo. /U converts UTF8 to GBK; the default is GBK to UTF8.
  64. echo. /I specifies the file to convert; “-” gets it from standard output.
  65. echo. This parameter may be omitted; by default it will be obtained from standard output.
  66. echo. When specifying a file to convert, the /I parameter cannot be omitted.
  67. goto :EOF

  68. :Start
  69. if "%input%" == "" set F_input=-1
  70. if "%F_input%" == "-1" (
  71. gawk -v F=%F% -f %AwkScript%
  72. ) else (
  73. gawk -v F=%F% -f %AwkScript% %input%
  74. )
  75. goto :EOF

  76. :AwkScript
  77. #<-1
  78. function gbk2utf8(string,flag, reg, gbkreg, utf8reg, char, result){
  79. gbkreg="|"
  80. utf8reg="||\xe0||\xf0|"
  81. reg=gbkreg
  82. if (flag==-1)
  83. reg=utf8reg
  84. RLENGTH = 1
  85. while(RLENGTH != -1){
  86. match(string,reg)
  87. char=substr(string,RSTART,RLENGTH)
  88. if (RLENGTH>1)
  89. char=charset
  90. result=result char
  91. string=substr(string,RSTART+RLENGTH)
  92. }
  93. return result
  94. }

  95. BEGIN {
  96. FS=","
  97. if (!F) F=1
  98. if (F==1) {
  99. while((getline<"gbk2utf8.dat") > 0)
  100. charset=$2
  101. }
  102. else{
  103. while((getline<"gbk2utf8.dat") > 0)
  104. charset=$1
  105. }
  106. close("gbk2utf8.dat")
  107. }
  108. {
  109. x=gbk2utf8($0,F)
  110. print x
  111. }
  112. #>-1
  113. goto :EOF
Posted by 无奈何 2006-11-30 01:02


[ Last edited by 无奈何 on 2006-11-30 at 02:04 PM ]

Attachments
gbk2utf8.zip (102.8 KiB)
Floor2 ccwan Posted 2006-11-30 00:38
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Strong post, leaving a mark. (Brother electronixtar, please don't mind the infringement ^_^) Brother Wunaihe is a mountain I can never climb over!
Floor3 zh159 Posted 2006-11-30 00:42
金牌会员 Posts 1,467 Credits 3,687
Previously, I found a GBK-Unicode encoding comparison table (with more than 7,000 comparison characters) online, and I used batch processing to calculate and generate a GBK-UTF-8 encoding comparison table. As long as I use the dictionary lookup for method, I can instantly extract the UTF-8 encoding. Let me look for it...
Floor4 vkill Posted 2006-11-30 00:46
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
Hey, I still don't understand that gawk part. I'm learning it.
Floor5 redtek Posted 2006-11-30 00:48
金牌会员 Posts 1,147 Credits 2,902
So wonderful~~~
Floor6 无奈何 Posted 2006-11-30 00:51
荣誉版主 Posts 356 Credits 1,338
ccwan, you're being too modest. I'm not a pro either.
zxcv, brother, more than 7,000 are of GB2312, the complete GBK has over 20,000, with a total of 22,046 code points.
Floor7 无奈何 Posted 2006-11-30 00:58
荣誉版主 Posts 356 Credits 1,338
RE vkill
GAWK supports multi-byte encoding. The advantage of this processing method is that as long as you write a regular expression that matches the encoding, it can be generally used for conversion of other encodings. It should be said that it is relatively easy to understand. The only thing to do is to obtain the character length and intercept it.
Floor8 zh159 Posted 2006-11-30 01:52
金牌会员 Posts 1,467 Credits 3,687
Originally posted by 无奈何 at 2006-11-29 12:51:
Brother ccwan is being modest, and I'm not a expert either.
Brother zxcv, the 7k+ ones are of GB2312, the complete GBK has over 20k, with a total of 22046 code points.

Indeed, it seems I need to find a complete GBK to UTF-8 conversion table.
Floor9 zh159 Posted 2006-11-30 02:21
金牌会员 Posts 1,467 Credits 3,687
Have obtained the complete gbk2utf8 encoding library

Found that there are many different data between this gbk2utf8 encoding library and the GB version I converted:
For example:
gbk2utf8
A1A42C C2B7
A1A52C CB89
A1A62C CB87
A1A72C C2A8

The red part is the identification of gbk

gb2utf8
a1a4 E383BB
a1a5 E08B89
a1a6 E08B87
a1a7 E082A8


It seems that there are some incorrect code comparisons in the GB part of this gbk2utf8 encoding library, and no errors have been found in the codes beyond the library GB part for the time being
Floor10 无奈何 Posted 2006-11-30 03:45
荣誉版主 Posts 356 Credits 1,338
RE zxcv

My one has a separator "," added in the middle, which is 0X2C.

Here is a piece of AWK script I wrote to generate all GBK characters. Just save it as UTF8 to get a comparison table of the two encodings.



[ Last edited by 无奈何 on 2006-11-30 at 03:50 AM ]
Floor11 zh159 Posted 2006-11-30 04:23
金牌会员 Posts 1,467 Credits 3,687
Only the following codes in the GB part cannot be found corresponding ones

A8BB C991
A8BD C584
A8BE C588
A8C0 C9A1


First try it ^_^
Floor12 electronixtar Posted 2006-11-30 07:01
铂金会员 Posts 2,672 Credits 7,493
I didn't understand it, so I'll give it a thumbs up first
Floor13 HUNRYBECKY Posted 2006-12-03 02:56
银牌会员 Posts 442 Credits 1,179
Too profound. Learning.
Floor14 tao0610 Posted 2006-12-03 02:59
高级用户 Posts 218 Credits 579
Looks like C++
Floor15 redtek Posted 2006-12-03 09:31
金牌会员 Posts 1,147 Credits 2,902
GAWK is really powerful~~~
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023