The Chr Function Implementation in Windows Pure Batch Script


The Chr Function takes a integer input and convert it to a character using ASCII table lookup. The basic ASCII table defines from 1 to 128 where the characters from index 1 to 31 and 127 is non-printable.

ascii The Chr Function Implementation in Windows Pure Batch Script batch script windows windows command shell

ascii table

The extended characters are defined from 128 and upwards.

extend-ascii The Chr Function Implementation in Windows Pure Batch Script batch script windows windows command shell

extend-ascii

We can use pure Windows batch script to print the character given its ASCII table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@echo off
:: helloacm.com
:: chr function implementation using pure windows batch
 
:: define characters from 32 to 126
set alphabet= !"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
 
:: set error code for invalid inputs
set errorlevel=0
 
:: valid range should be from 32 to 126 inclusive
if "%1"=="" goto :EOF
if %1 LSS 32 goto :EOF
if %1 GTR 126 goto :EOF
 
:: call function
call :ASCII %1 chr
 
:: print result, using ^ to escape special characters 
:: such as <, > and |
echo.^%chr%
 
:: set error code on success
set errorlevel=%1
 
:: end the script
goto :EOF
 
:: sub-routine
:ASCII
    setlocal EnableDelayedExpansion
        :: get the index
        set /a var=%1-32
        :: retrieve letter
        set character=!alphabet:~%var%,1!
    :: end the routine and return result as second parameter (out)
    endlocal & set %2=^%character%
@echo off
:: helloacm.com
:: chr function implementation using pure windows batch

:: define characters from 32 to 126
set alphabet= !"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

:: set error code for invalid inputs
set errorlevel=0

:: valid range should be from 32 to 126 inclusive
if "%1"=="" goto :EOF
if %1 LSS 32 goto :EOF
if %1 GTR 126 goto :EOF

:: call function
call :ASCII %1 chr

:: print result, using ^ to escape special characters 
:: such as <, > and |
echo.^%chr%

:: set error code on success
set errorlevel=%1

:: end the script
goto :EOF

:: sub-routine
:ASCII
	setlocal EnableDelayedExpansion
		:: get the index
		set /a var=%1-32
		:: retrieve letter
		set character=!alphabet:~%var%,1!
	:: end the routine and return result as second parameter (out)
	endlocal & set %2=^%character%

Please note that we use ^ to escape special characters such as < > and |. We also check the validity of the parameter to ensure the range is from 32 to 126 inclusive.

If we can successfully find the character in the lookup table, we set the return code (%errorlevel%) to its ASCII index otherwise the return code will be zero. To use the function, we can run chr 65 that will print upper character A in the console.

To verify, we can use the following for command:

@for /L %i in (34,1,126) do @echo %i = & chr %i

And this will print something like this:

34 =
"
35 =
#
36 =
$
37 =
%
38 =
&
..
..
..
121 =
y
122 =
z
123 =
{
124 =
|
125 =
}
126 =
~

Update:
We can use the dynamic variable %=ExitCode% to get the return code in hexadecimal and similarly, we can use %=ExitCodeAscii% to print the character. So the above can be shortened to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@echo off
:: helloacm.com
:: chr function implementation using pure windows batch
 
:: set error code for invalid inputs
set errorlevel=0
 
:: valid range should be from 32 to 126 inclusive
if "%1"=="" goto :EOF
if %1 LSS 32 goto :EOF
if %1 GTR 126 goto :EOF
 
:: call function 
cmd /c exit /b %1
 
:: print result, using ^ to escape special characters 
:: such as <, > and |
echo.^%=ExitCodeAscii%
 
:: set error code on success
set errorlevel=%1
@echo off
:: helloacm.com
:: chr function implementation using pure windows batch

:: set error code for invalid inputs
set errorlevel=0

:: valid range should be from 32 to 126 inclusive
if "%1"=="" goto :EOF
if %1 LSS 32 goto :EOF
if %1 GTR 126 goto :EOF

:: call function 
cmd /c exit /b %1

:: print result, using ^ to escape special characters 
:: such as <, > and |
echo.^%=ExitCodeAscii%

:: set error code on success
set errorlevel=%1

cmd /c launch a new shell and terminates. exit /b returns the script with code.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
527 words
Last Post: Adsense 8 Years - Statistics!
Next Post: How to Print ASCII code from Character on Windows Batch Script?

The Permanent URL is: The Chr Function Implementation in Windows Pure Batch Script

Leave a Reply