|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『楼 主』:
[原创]GBK & UTF8 编码互转脚本 (CMD+GAWK)
使用 LLM 解释/回答一下
GBK & UTF8 编码互转脚本 (CMD+GAWK)
因为我的实际应用中需要 UTF8 到 GBK 的编码转换,所以就用 GAWK 写了一个,事实上在我早先的帖子中已经在使用。这次整理了一下,已支持编码的双向转换,自己制作了完整的 GBK 到 UTF8 的转码对照表,制作中发现系统的转码结果和 iconv 的转码结果有不少差异,对照表采用的是前者。
本脚本支持管道和文件的编码转换,结果输出到屏幕。现在参数支持不多,但是有参数完整性的检测功能,可以完成多参数的无序调用。采用了新的脚本释放方法,源文件有改动时会自动更新脚本。还有错误消息和依赖文件完整性检查机制。希望这些小技巧对大家编写批处理有所帮助。
GAWK 下载链接: http://www.klabaster.com/progs/gawk32.zip
脚本及转码表见附件。
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- :: gbk2utf8.cmd -V0.1 -- GBK & UTF8 编码互转
- :: 无奈何@cn-dos.net - 2006-11-28 - CMD & GAWK
- :: 用法:gbk2utf8 /I file...
- :: 支持文件: - gawk.exe gbk2utf8.dat
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- @echo off
- setlocal
- set self="%~f0"
- set AwkScript="%temp%\%~n0%~z0.awk"
- set path=%path%;%~dp0;%cd%
- set nofile=
- set error=
- set input=
- ::依赖文件完整性检查
- for %%i in (gawk.exe gbk2utf8.dat) do (
- @if "%%~$PATH:i" == "" (
- echo.程序所依赖文件 "%%i" 缺失。
- set nofile=1
- ) else ( set %%~ni="%%~$PATH:i" )
- )
- if defined nofile goto :EOF
- ::文件改动后更新脚本
- if not exist %AwkScript% (
- del /q "%temp%\%~n0*.awk" 2>nul
- gawk "/^#<-1/,/^#>-1/{if(!/^#/)print}" %self% >%AwkScript%
- )
- :ParseLoop
- if "%~1" == "" goto Start
- if "%~1" == "?" goto SwitchH
- if "%~1" == "/?" goto SwitchH
- rem 参数处理并转到相应标签。
- for %%s in (U u I i h H) do if "%~1"=="/%%s" goto Switch%%s
- if "%F_input%" == "1" (
- if not exist "%~1" set error=警告:文件 "%~1" 不存在。 & goto error
- set input=%input% "%~1"
- shift
- goto ParseLoop
- )
- if "%F_input%" == "-1" shift & goto ParseLoop
- set error=错误: 参数格式不正确 - "%1" !
- goto error
- :SwitchI
- set F_input=1
- if "%~2" == "-" set F_input=-1
- shift
- goto ParseLoop
- :SwitchU
- set F=-1
- shift
- goto ParseLoop
- :error
- echo.%error%
- echo.
- :SwitchH
- echo.gbk2utf8 V0.1 -- GBK ^& UTF8 编码互转
- echo.
- echo.用法:1、%~n0
- echo. 2、%~n0 /I file...
- echo. 3、%~n0 /I -
- echo.
- echo.选项: /? 显示本简短帮助,等价命令 /H 。
- echo. /U UTF8 转码为 GBK ,默认设置为 GBK 转码为 UTF8 。
- echo. /I 指定转换文件,“-”号从标准输出获得。
- echo. 本参数可以空缺,缺省将从标准输出获得。
- echo. 指定转换文件时,/I 参数不可省略。
- goto :EOF
- :Start
- if "%input%" == "" set F_input=-1
- if "%F_input%" == "-1" (
- gawk -v F=%F% -f %AwkScript%
- ) else (
- gawk -v F=%F% -f %AwkScript% %input%
- )
- goto :EOF
- :AwkScript
- #<-1
- function gbk2utf8(string,flag, reg, gbkreg, utf8reg, char, result){
- gbkreg="|"
- utf8reg="||\xe0||\xf0|"
- reg=gbkreg
- if (flag==-1)
- reg=utf8reg
- RLENGTH = 1
- while(RLENGTH != -1){
- match(string,reg)
- char=substr(string,RSTART,RLENGTH)
- if (RLENGTH>1)
- char=charset
- result=result char
- string=substr(string,RSTART+RLENGTH)
- }
- return result
- }
- BEGIN {
- FS=","
- if (!F) F=1
- if (F==1) {
- while((getline<"gbk2utf8.dat") > 0)
- charset=$2
- }
- else{
- while((getline<"gbk2utf8.dat") > 0)
- charset=$1
- }
- close("gbk2utf8.dat")
- }
- {
- x=gbk2utf8($0,F)
- print x
- }
- #>-1
- goto :EOF
无奈何发表于 2006-11-30 01:02
Last edited by 无奈何 on 2006-11-30 at 02:04 PM ]
附件
1: gbk2utf8.zip (2006-11-30 14:06, 102.8 KiB, 下载附件所需积分 1 点
,下载次数: 259)
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-11-30 00:31 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
强贴留名。(electronixtar兄莫怪侵权^_^)
无奈何兄是我永不能翻越的高山啊!
Strong post, leaving a mark. (Brother electronixtar, please don't mind the infringement ^_^) Brother Wunaihe is a mountain I can never climb over!
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-11-30 00:38 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
以前我在网上找到一个 GBK-Unicode 的编码对照表(7k多对照字符),自己用批处理计算生成了 GBK-UTF-8 编码对照表,只要用查字典的 for 方式,就可以瞬间提取 UTF-8 编码,先找找看...
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...
|
|
2006-11-30 00:42 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
哎,gawk那段还是看不懂,学习中
Hey, I still don't understand that gawk part. I'm learning it.
|
|
2006-11-30 00:46 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
太精彩了~~~
So wonderful~~~
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-30 00:48 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
ccwan 兄过谦了,我也不是高手。
zxcv 兄,7k 多的是 GB2312 的,完整 GBK 有 2w 多,共 22046 个码位。
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.
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-11-30 00:51 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
RE vkill
GAWK 支持多字节编码,这种处理方法的好处是只要编写匹配编码的正则表达式就可通用于其他其他编码的转换。应该说也是比较好理解的,唯一做的就是获取字符长度并截取。
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.
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-11-30 00:58 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by 无奈何 at 2006-11-29 12:51:
ccwan 兄过谦了,我也不是高手。
zxcv 兄,7k 多的是 GB2312 的,完整 GBK 有 2w 多,共 22046 个码位。
确实如此,看来得找一个完整 GBK 的算一个 GBK-UTF-8 对照表了
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.
|
|
2006-11-30 01:52 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
已经获得了完整的 gbk2utf8 编码库
发现这个gbk2utf8编码库和我转换的 GB 版有很多数据不同:
例:
gbk2utf8
A1A42C C2B7
A1A52C CB89
A1A62C CB87
A1A72C C2A8
红色部分是gbk的识别
gb2utf8
a1a4 E383BB
a1a5 E08B89
a1a6 E08B87
a1a7 E082A8
好像这个gbk2utf8编码库GB部分的部分编码对照有误,超出库GB部分的编码暂时未发现错误
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
|
|
2006-11-30 02:21 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
RE zxcv
我的那个中间加了分隔符 “,” 也就是 0X2C 。
给你一段我写的生成 GBK 所有字符的 AWK 脚本。自己另存为 UTF8 就可以获得两个编码的对照表了。
BEGIN{
#GBK/1: A1A1-A9FE 图形符号区GB 2312 非汉字符号区
for (i=0xa1;i<=0xa9;i++)
for (j=0xa1;j<=0xfe;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/2: B0A1-F7FE 汉字区GB 2312 汉字区
for (i=0xb0;i<=0xf7;i++)
for (j=0xa1;j<=0xfe;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/3: 8140-A0FE 汉字区
for (i=0x81;i<=0xa0;i++)
for (j=0x40;j<=0xfe;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/4: AA40-FEA0 汉字区
for (i=0xaa;i<=0xfe;i++)
for (j=0x40;j<=0xa0;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/5: A840-A9A0 图形符号区
for (i=0xa8;i<=0xa9;i++)
for (j=0x40;j<=0xa0;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
}
Last edited by 无奈何 on 2006-11-30 at 03:50 AM ]
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.
BEGIN{
#GBK/1: A1A1-A9FE Graphic symbol area GB 2312 non-Chinese character symbol area
for (i=0xa1;i<=0xa9;i++)
for (j=0xa1;j<=0xfe;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/2: B0A1-F7FE Chinese character area GB 2312 Chinese character area
for (i=0xb0;i<=0xf7;i++)
for (j=0xa1;j<=0xfe;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/3: 8140-A0FE Chinese character area
for (i=0x81;i<=0xa0;i++)
for (j=0x40;j<=0xfe;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/4: AA40-FEA0 Chinese character area
for (i=0xaa;i<=0xfe;i++)
for (j=0x40;j<=0xa0;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
#GBK/5: A840-A9A0 Graphic symbol area
for (i=0xa8;i<=0xa9;i++)
for (j=0x40;j<=0xa0;j++)
if (j!=0x7f) printf("%c%c\n",i,j)
}
Last edited by 无奈何 on 2006-11-30 at 03:50 AM ]
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-11-30 03:45 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
GB部分的编码只剩下面的找不到对应的
A8BB C991
A8BD C584
A8BE C588
A8C0 C9A1
先试试了^_^
Only the following codes in the GB part cannot be found corresponding ones
A8BB C991
A8BD C584
A8BE C588
A8C0 C9A1
First try it ^_^
|
|
2006-11-30 04:23 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
没看懂,顶一个先
I didn't understand it, so I'll give it a thumbs up first
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2006-11-30 07:01 |
|
|
HUNRYBECKY
银牌会员
    
积分 1179
发帖 442
注册 2006-9-9
状态 离线
|
|
2006-12-3 02:56 |
|
|
tao0610
高级用户
    朦胧的世界
积分 579
发帖 218
注册 2006-10-24
状态 离线
|
|
2006-12-3 02:59 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
GAWK真是强大啊~~~
GAWK is really powerful~~~
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-12-3 09:31 |
|
|