返回列表 发帖

仅用批处理生成所有 ASCII 字符

对于获取 ASCII 全范围(0x00 -- 0xFF)字符, 本论坛上原本就有一篇由 plp626  所发:
扩展ASCII码字符集0x00~0xff 批处理获取函数
http://www.bathome.net/viewthread.php?tid=12347

本帖转载 dostips 上不同方式的实现
http://www.dostips.com/forum/viewtopic.php?f=3&t=5326

原文标题:
"Create nul and all ascii characters with only batch"
-- 译: 只用批处理生成 nul 和所有 ASCII 字符

其实 NUL (码值: 0) 也是一个 ASCII 字符, 只是由于它的特殊性和处理上的困难, 所以作者特别强调了一下吧.

先贴出两个提速稳定版: carlos 版 和 dbenham 版, 最后再贴出一个初始稳定版.

关于代码的详细说明, 请阅读 dostips 原帖, 转载到此, 主要为备份之用.

carlos 提速稳定版:
@echo off
:gen255
::generates 0-255 .chr files that have 1 byte
::Teamwork of carlos, penpen, aGerman, dbenham, einstein1969
::Tested under Win8, WinXP(64mb)
setlocal enableextensions disabledelayedexpansion
set "map=paddingpaddingpaddingpaddingpadding"
set "map=%map%#$%%&'()*+,-./0123456789:;<p>?@"
set "map=%map%ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"
set "map=%map%abcdefghijklmnopqrstuvwxyz{|}~"
set "map=%map: =%"
set "gchr=(type nul >%%#.tmp"
set "gchr=%gchr% & makecab /d compress=off"
set "gchr=%gchr% /d reserveperfoldersize=%%#"
set "gchr=%gchr% /d reserveperdatablocksize=26"
set "gchr=%gchr% %%#.tmp %%#.chr 1>nul"
set "gchr=%gchr% & type %%#.chr"
set "gchr=%gchr% |((for /l %%_ in (1 1 38) do pause) 1>nul"
set "gchr=%gchr% & findstr ^^^^ 1>%%#.tmp)"
set "gchr=%gchr% & copy /y %%#.tmp /a %%#.chr /b 1>nul"
set "gchr=%gchr% & del %%#.tmp)"
set "gmap=for /l %%# in (35 1 126) do"
set "gmap=%gmap% set /p "=!map:~%%#,1!" 0<nul 1>%%#.chr"
set "new_thread=start /i /w /b cmd /a /q /d /c"
if exist ???.chr del /a /f /q ???.chr
%new_thread% "for /l %%# in (127 1 169) do %gchr%"
%new_thread% "for /l %%# in (170 1 211) do %gchr%"
%new_thread% "for /l %%# in (212 1 255) do %gchr%"
for /l %%# in (0 1 34) do %gchr%
cmd /a /v:on /q /d /c "%gmap%"
:gen255w
ping -l 0 -n 1 -w 100 1.0.0.0 >nul
for %%# in (169.chr 211.chr 255.chr) do (
if not exist %%# goto :gen255w
if not "1"=="%%~z#" goto :gen255w)
for %%# in (61) do %gchr%
copy /y nul + nul /a 26.chr /a 1>nul
goto :eofCOPY
dbenham 提速稳定版:
@echo off
setlocal
:genAllChr
::This code creates 256 1 byte files, one for each possible byte value.
::This is encoded in a macro way to be called asynchronously with start cmd /c
::Teamwork of carlos, penpen, aGerman, dbenham, einstein1969
::Tested under Win7 and XP
set ^"genchr=(^
for /l %%N in (%%A !cnt! 255) do (^
  if %%N equ 26 (^
    copy /y nul + nul /a 26.chr /a ^>nul^
  ) else (if %%N geq 35 if %%N leq 126 if %%N neq 61 (^
    ^<nul set /p "=!ascii:~%%N,1!" ^>%%N.chr^
  ))^&^
  if not exist %%N.chr (^
    makecab /d compress=off /d reserveperdatablocksize=26 /d reserveperfoldersize=%%N %%A.tmp %%N.chr ^>nul^&^
    type %%N.chr ^| ((for /l %%n in (1 1 38) do pause)^>nul^&findstr "^^" ^>%%N.temp)^&^
    ^>nul copy /y %%N.temp /a %%N.chr /b^&^
    del %%N.temp^
  )^
))^&^
del %%A.tmp^"
del /f /q /a *.chr >nul 2>&1
set "ascii=                                   #$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
set /a cnt=number_of_processors
if %cnt% lss 1 set cnt=1
if %cnt% gtr 256 set cnt=256
set /a "end=cnt-1"
for /l %%A in (0 1 %end%) do (
  type nul >%%A.tmp
  if %%A equ %end% (
    cmd /q /v:on /c "%genchr%"
  ) else (
    start "" /b cmd /q /v:on /c "%genchr%"
  )
)
:genAllChr.check
for /l %%N in (0 1 %end%) do if exist %%N.tmp goto :genAllChr.check
exit /bCOPY
初始稳定版:
REM This code creates 256 files containing one single Byte each from 0x00 until 0xFF
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8
@echo off
setlocal EnableDelayedExpansion
2>nul md "characters"
pushd "characters"
>"t.tmp" type nul
set "hex=0 1 2 3 4 5 6 7 8 9 A B C D E F"
for %%A in (%hex%) do for %%B in (%hex%) do (
  set /a "N=0x%%A%%B"
  >nul makecab /d compress=off /d reserveperfoldersize=!N! /d reserveperdatablocksize=26 "t.tmp" "%%A%%B.chr"
  type "%%A%%B.chr" | ((for /l %%N in (1 1 38) do pause)>nul&findstr "^">"temp.tmp")
  >nul copy /y "temp.tmp" /a "%%A%%B.chr" /b
)
>nul copy /y nul + nul /a "1A.chr" /a
del "t.tmp" "temp.tmp"
popdCOPY

想法太妙了,真特么无所不用其极

TOP

不过我这里 makecab 输出的格式好像和那帖子说的不一样

F8.chr 的那个 reserveperfoldersize 在文件首字节,所以用顶楼的办法取不出来

而 C1.chr 则是根本找不到 0xC1,这是咋回事...

win7 旗舰 32位

TOP

回复 3# CrLf

初始稳定版

我在 Win 7 64 位上没有发现问题

TOP

type $|((for /l %%_ in (1,1,38) do pause)>nul & findstr ^^^^)>#.chrCOPY
又学到一招,可以这样来偏移字节……
但是第一个貌似在XP上跑出来结果有问题,只有部分文件是正确的。

TOP

难道是我这系统环境出问题了?结果都有问题呢。。。。

TOP

回复 3# CrLf
回复 6# amwfjhh
我在 chcp 936 下没有得到正确结果
但我的命令行设置成了 默认 chcp 437, 这时没有发现错误.
在 chcp 437 下重新运行看看

TOP

回复 7# neorobin


   
很好!正常了。有点奇怪,都是对半角字符处理,难道中文代码页下面还有隐藏的处理猫腻……

TOP

makecab
cmd /u
fc /b
to be continued...

TOP

本帖最后由 amwfjhh 于 2014-11-19 22:44 编辑

echo debug是个好同学

TOP

本帖最后由 tiandyoin 于 2023-4-8 18:11 编辑

回复 1# neorobin


    要如何合并成一个 chr 文件呢?
:merge
setlocal enabledelayedexpansion
cd.>"..\合并文件.chr"
REM copy /y *.chr /a "..\合并文件.chr" 1>nul
del /f /q "..\合并文件.chr"
for /l %%i in (0 1 9) do (
type %%i.chr>>"..\合并文件.chr" 2>nul
echo.>>"..\合并文件.chr" 2>nul
)
@rem LF 是 换行符
set LF=^
@rem 使用 ANSI 编码时,以上必须空两行,必须延迟取值 !LF!
echo.!LF!>>"..\合并文件.chr" 2>nul
for /l %%i in (11 1 12) do (
type %%i.chr>>"..\合并文件.chr" 2>nul
echo.>>"..\合并文件.chr" 2>nul
)
@rem CR 是 回车符
for /f %%C in ('copy /z "C:\Windows\System32\drivers\etc\hosts" nul') do (
set a=%%C
echo.!a!>>"..\合并文件.chr" 2>nul
)
for /l %%i in (14 1 25) do (
type %%i.chr>>"..\合并文件.chr" 2>nul
echo.>>"..\合并文件.chr" 2>nul
)
for /f %%a in (.\26.chr) do (
echo.%%a>>"..\合并文件.chr" 2>nul
)
for /l %%i in (27 1 255) do (
type %%i.chr>>"..\合并文件.chr" 2>nul
echo.>>"..\合并文件.chr" 2>nul
)
endlocal
goto :eofCOPY

TOP

返回列表