genchr.cmd create any byte (makecab)

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

genchr.cmd create any byte (makecab)

Post by MigrationUser »

01 May 2014 07:48
carlos

I have this code. I love this code because I thing combine many contributions for get the creation of any byte value.

Maybe it can appear in https://ss64.com/nt/makecab.html
It uses makecab for create any byte value.

This will works from windows 2000 to windows 8.

Usage:

genchr.cmd 0
rem this will create the 0 or nul character. file 0.chr
genchr.cmd 9
rem this will create the 9 or tab character. file 9.chr

The files will have the .chr extension.

genchr.cmd

Code: Select all

REM This code creates one single byte. Parameter: <int>0-255
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8

@echo off

set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26  (type nul >t.tmp
makecab %options% /d reserveperfoldersize=%~1 t.tmp %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">t.tmp)
>nul copy /y t.tmp /a %~1.chr /b
del t.tmp
) else (copy /y nul + nul /a 26.chr /a >nul)
Last edited by carlos (02 May 2014 21:19)

----------------------------

#2 01 May 2014 08:47
bluesxman


Nice code.

Minor change to validate input and also to allow concurrent runs, like if you want to generate multiple characters in one go with something like this:

D:\Documents\Scripts\ss64\p7665>for /l %a in (0,1,255) do start "" cmd /c genchr %a

Code: Select all

@echo off
set "USAGE=echo:Usage: Supply an integer 0-255& goto :EOF"
if "%~1" equ ""      %USAGE%
set /a "val=%~1" 2>nul
if "%~1" neq "%val%" %USAGE%
if  %~1  lss 0       %USAGE%
if  %~1  gtr 255     %USAGE%

set tempfile=%1.tmp
set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26  (type nul >"%tempfile%"
makecab %options% /d reserveperfoldersize=%~1 "%tempfile%" %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">"%tempfile%")
>nul copy /y "%tempfile%" /a %~1.chr /b
del "%tempfile%"
) else (copy /y nul + nul /a 26.chr /a >nul)
Last edited by bluesxman (02 May 2014 11:02)

cmd | *sh | ruby | chef

----------------------------

#3 01 May 2014 23:38
Simon Sheppard


Very nice!
I have put a copy of this here https://ss64.com/nt/syntax-genchr.html
with links from the makecab and Ascii table pages

----------------------------

#4 02 May 2014 00:19
carlos


Thanks.
The bluexman code, need a little fix:

this

Code: Select all

if "%~1" lss "0"     %USAGE%
if "%~1" gtr "255"   %USAGE%
need be:

Code: Select all

if %~1 lss 0     %USAGE%
if %~1 gtr 255   %USAGE%
without quotes, else for example: genchr 3 print the usage text.

----------------------------

#5 02 May 2014 10:31
bluesxman
carlos wrote:

The bluexman code, need a little fix:
D'oh! Thanks for exposing the flaw in my testing. Fixed above.

cmd | *sh | ruby | chef

----------------------------

#6 02 May 2014 13:56
carlos


Thanks bluexmam. I tried send you a pm, but not found how.

Also. I want add a note about the tempfile.
It should be left as current is:
set tempfile=%1.tmp
or for avoid quote problem:
set tempfile=%~1.tmp

the decimal length of the ascii byte that you want ( %~1 ), plus ".tmp" or 4 characters.
This means, that if you create the 1 ascii, it should be: 1.tmp and if you create the 33 ascii, it should be 33.tmp
Is ok.
The tempfile name length is critical, because the filename is included in the cabinet file, and can affect the offset size, and cause that the code not generate correctly all the ascii bytes.
Actually, the code works ok: I test with a program all the ascii bytes generates, checking if the file generated is a 1 byte size and have in the content byte is the same that in the filename.

But, if you change the tempfile to other something like: _%1.temp it can cause that the generation of some bytes fail (It is only verified checking the .chr file generated, not because a warning of the batch).

Because all this explication, maybe a note about not change the tempfile name should be added, or not use a variable tempfile, for avoid the temptation of change it, that can cause corrupted .chr files.

Any change to the code, should be tested in all the 256 .chr files, having 1 byte and with the same ascii content.

I used this c code for test it: change.c
I run this in the same folder that have the .chr files.
First I delete all the .chr files, then I generate all using genchr
If you run this, and any message is printed, means that the generation is ok.

Actually, the code is ok, but because a variable is used for the temp filename, it is ok, but a note of change it can cause corrupted .chr files maybe should be added. The temp filename is relevant for the correct generation of .chr files.

Last edited by carlos (02 May 2014 13:58)

----------------------------

#7 02 May 2014 18:34
Simon Sheppard


from a brief test it looks like makecab is putting the filename at the end of the temporary file, so the filename length may not matter.

----------------------------

#8 02 May 2014 20:05
carlos


Yes, Simon. I do more test and all my test works ok changing the filenames.
I not remember why, but in previous version, the temp filename was a problem for me.
Anyways, any change to the code, need verify the correct creation and content of the .chr files.

Please, update the code on the page with the fix.

----------------------------

#9 02 May 2014 20:55
Simon Sheppard


Does the version here look OK now
https://ss64.com/nt/syntax-genchr.html

----------------------------


#14 02 Sep 2018 10:53
carlos

Simon, I posted here a new version of the genchr.cmd that solved a issue related to the codepage, when the user uses a native double byte code page like 932 or 950 the .chr files was not created correctly.
The version here have the issue fixed.

Code: Select all

:: This code creates one single byte. Parameter: <int>0-255
:: Teamwork of carlos, penpen, aGerman, dbenham, bluesxman, sst
:: Tested under Win2000, XP, Win7, Win8, Win10
:: Fixed issue on environments with dbcs codepage

@echo off
setlocal enableextensions
set "USAGE=echo:Usage: Supply an integer 0-255& goto :EOF"
if "%~1" equ "" %USAGE%
set /a "val=%~1" 2>nul
if "%~1" neq "%val%" %USAGE%
if %~1 lss 0    %USAGE%
if %~1 gtr 255  %USAGE%

for /f "tokens=*" %%a in ('chcp') do for %%b in (%%a) do set "cp=%%~nb"
set "options=/d compress=off /d reserveperdatablocksize=26"
set "cmd=cmd /d /c"
mode con cp select=437 >nul
if %~1 neq 26  (type nul >%~1.tmp
makecab %options% /d reserveperfoldersize=%~1 %~1.tmp %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">%~1.tmp)
>nul %cmd% copy /y %~1.tmp /a %~1.chr /b
del %~1.tmp
) else (%cmd% copy /y nul + nul /a 26.chr /a >nul)
mode con cp select=%cp% >nul
----------------------------

#2 03 Sep 2018 00:49
Simon Sheppard


Thanks Carlos, I have now posted this to https://ss64.com/nt/syntax-genchr.html
Post Reply