new functions: :chr, :asc, :asciiMap
Posted: 31 Mar 2011 05:12
Here is code I developed for 3 new functions to support bi-directional conversion between numeric ASCII codes and characters. I think I saw routines with similar functionality some where that relied on DEBUG and temp files. These routines do not need either.
There are 10 characters that are problematic when trying to put them in a variable that are not supported. Jeb has some tricks for some (all?) of them. But I'm not sure it would be productive for this application.
code...
and output...
Dave Benham
There are 10 characters that are problematic when trying to put them in a variable that are not supported. Jeb has some tricks for some (all?) of them. But I'm not sure it would be productive for this application.
code...
Code: Select all
@echo off
setlocal disableDelayedExpansion
for /l %%n in (0,1,255) do (
call :chr %%n char
if not errorlevel 1 (
call :asc char 0 n
call echo "%%n:%%char%%:%%n%%"
) else echo chr %%n produced above error
)
exit /b
:asc StrVar IntVal [RtnVar]
::
:: Computes the ASCII code for a specified character within the string
:: contained by variable StrVar. The position within the string is specified
:: by the IntVal argument. A non-negative value is relative to the beginning
:: of the string, with 0 specifying the first character. A negative value is
:: relative to the end of the string, with -1 specifying the last character.
::
:: Sets RtnVar=result
:: or displays result if RtnVar not specified
::
:: IntVal may be passed as a variable without enclosing the name in quotes.
:::
::: Dependencies - asciiMap
:::
setlocal disableDelayedExpansion
set /a n=%~2 2>nul
call set "chr=%%%~1:~%n%,1%%"
call :asciiMap ascii
setlocal enableDelayedExpansion
if "!chr!"==" " set /a rtn=32&goto :asc.end
set rtn=
for /l %%n in (0,1,255) do if "!ascii:~%%n,1!"=="!chr!" set rtn=%%n
:asc.end
(endlocal & rem -- return values
endlocal
if "%~3" neq "" (set %~3=%rtn%) else (echo:%rtn%)
)
exit /b
:chr IntVal [RtnVar]
::
:: Converts ASCII code IntVal into the corresponding character.
::
:: Sets RtnVar=result
:: or displays result if RtnVar not specified
::
:: IntVal must be a value between 0 and 255.
::
:: Aborts with an error message to stderr and errorlevel 1 if IntVal
:: corresponds to one of the following problematic characters:
::
:: Dec Hex Oct Char
:: --- ---- ---- ----
:: 0 0x00 00 NUL (null)
:: 3 0x03 03 ETX (end of text)
:: 8 0x08 010 BS (backspace)
:: 9 0x09 011 TAB (horizontal tab)
:: 10 0x0A 012 LF (line feed)
:: 11 0x0B 013 VT (vertical tab)
:: 12 0x0C 014 FF (form feed)
:: 13 0x0D 015 CR (carriage return)
:: 26 0x1A 032 SUB (substitute)
:: 127 0x7F 0177 DEL (delete)
::
:: IntVal may be passed as a variable without enclosing the name in quotes.
:::
::: Dependencies - asciiMap
:::
setlocal disableDelayedExpansion
set /a n=%~1 2>nul
if %n%==32 set "c= "&goto :chr.end
call :asciiMap map
call set "c=%%map:~%n%,1%%"
if "%c%%c%"==" " (
echo ERROR: Problematic ASCII Code >&2
exit /b 1
)
:chr.end
(endlocal & rem -- return values
if "%~2" neq "" (set %~2=^%c%) else (echo:^%c%)
)
exit /b
:asciiMap rtnVar
::
:: Sets variable rtnVar to a 256 character string containing the complete
:: extended ASCII character set except a space has been substituted for each
:: of the following problematic characters:
::
:: Dec Hex Oct Char
:: --- ---- ---- ----
:: 0 0x00 00 NUL (null)
:: 3 0x03 03 ETX (end of text)
:: 8 0x08 010 BS (backspace)
:: 9 0x09 011 TAB (horizontal tab)
:: 10 0x0A 012 LF (line feed)
:: 11 0x0B 013 VT (vertical tab)
:: 12 0x0C 014 FF (form feed)
:: 13 0x0D 015 CR (carriage return)
:: 26 0x0A 032 SUB (substitute)
:: 127 0x7F 0177 DEL (delete)
:::
::: Dependencies - <none>
:::
set %~1= !^"#$%%^&'^(^)*+,-./0123456789:;^<=^>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^^_`abcdefghijklmnopqrstuvwxyz{^|}~ €‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
exit /b
and output...
Code: Select all
ERROR: Problematic ASCII Code
chr 0 produced above error
"1::1"
"2::2"
ERROR: Problematic ASCII Code
chr 3 produced above error
"4::4"
"5::5"
"6::6"
"7::7"
ERROR: Problematic ASCII Code
chr 8 produced above error
ERROR: Problematic ASCII Code
chr 9 produced above error
ERROR: Problematic ASCII Code
chr 10 produced above error
ERROR: Problematic ASCII Code
chr 11 produced above error
ERROR: Problematic ASCII Code
chr 12 produced above error
ERROR: Problematic ASCII Code
chr 13 produced above error
"14::14"
"15::15"
"16::16"
"17::17"
"18::18"
"19::19"
"20::20"
"21::21"
"22::22"
"23::23"
"24::24"
"25::25"
ERROR: Problematic ASCII Code
chr 26 produced above error
"27::27"
"28::28"
"29::29"
"30::30"
"31::31"
"32: :32"
"33:!:33"
"34:":34"
"35:#:35"
"36:$:36"
"37:%:37"
"38:&:38"
"39:':39"
"40:(:40"
"41:):41"
"42:*:42"
"43:+:43"
"44:,:44"
"45:-:45"
"46:.:46"
"47:/:47"
"48:0:48"
"49:1:49"
"50:2:50"
"51:3:51"
"52:4:52"
"53:5:53"
"54:6:54"
"55:7:55"
"56:8:56"
"57:9:57"
"58:::58"
"59:;:59"
"60:<:60"
"61:=:61"
"62:>:62"
"63:?:63"
"64:@:64"
"65:A:65"
"66:B:66"
"67:C:67"
"68:D:68"
"69:E:69"
"70:F:70"
"71:G:71"
"72:H:72"
"73:I:73"
"74:J:74"
"75:K:75"
"76:L:76"
"77:M:77"
"78:N:78"
"79:O:79"
"80:P:80"
"81:Q:81"
"82:R:82"
"83:S:83"
"84:T:84"
"85:U:85"
"86:V:86"
"87:W:87"
"88:X:88"
"89:Y:89"
"90:Z:90"
"91:[:91"
"92:\:92"
"93:]:93"
"94:^:94"
"95:_:95"
"96:`:96"
"97:a:97"
"98:b:98"
"99:c:99"
"100:d:100"
"101:e:101"
"102:f:102"
"103:g:103"
"104:h:104"
"105:i:105"
"106:j:106"
"107:k:107"
"108:l:108"
"109:m:109"
"110:n:110"
"111:o:111"
"112:p:112"
"113:q:113"
"114:r:114"
"115:s:115"
"116:t:116"
"117:u:117"
"118:v:118"
"119:w:119"
"120:x:120"
"121:y:121"
"122:z:122"
"123:{:123"
"124:|:124"
"125:}:125"
"126:~:126"
ERROR: Problematic ASCII Code
chr 127 produced above error
"128:€:128"
"129::129"
"130:‚:130"
"131:ƒ:131"
"132:„:132"
"133:…:133"
"134:†:134"
"135:‡:135"
"136:ˆ:136"
"137:‰:137"
"138:Š:138"
"139:‹:139"
"140:Œ:140"
"141::141"
"142:Ž:142"
"143::143"
"144::144"
"145:‘:145"
"146:’:146"
"147:“:147"
"148:”:148"
"149:•:149"
"150:–:150"
"151:—:151"
"152:˜:152"
"153:™:153"
"154:š:154"
"155:›:155"
"156:œ:156"
"157::157"
"158:ž:158"
"159:Ÿ:159"
"160: :160"
"161:¡:161"
"162:¢:162"
"163:£:163"
"164:¤:164"
"165:¥:165"
"166:¦:166"
"167:§:167"
"168:¨:168"
"169:©:169"
"170:ª:170"
"171:«:171"
"172:¬:172"
"173::173"
"174:®:174"
"175:¯:175"
"176:°:176"
"177:±:177"
"178:²:178"
"179:³:179"
"180:´:180"
"181:µ:181"
"182:¶:182"
"183:·:183"
"184:¸:184"
"185:¹:185"
"186:º:186"
"187:»:187"
"188:¼:188"
"189:½:189"
"190:¾:190"
"191:¿:191"
"192:À:192"
"193:Á:193"
"194:Â:194"
"195:Ã:195"
"196:Ä:196"
"197:Å:197"
"198:Æ:198"
"199:Ç:199"
"200:È:200"
"201:É:201"
"202:Ê:202"
"203:Ë:203"
"204:Ì:204"
"205:Í:205"
"206:Î:206"
"207:Ï:207"
"208:Ð:208"
"209:Ñ:209"
"210:Ò:210"
"211:Ó:211"
"212:Ô:212"
"213:Õ:213"
"214:Ö:214"
"215:×:215"
"216:Ø:216"
"217:Ù:217"
"218:Ú:218"
"219:Û:219"
"220:Ü:220"
"221:Ý:221"
"222:Þ:222"
"223:ß:223"
"224:à:224"
"225:á:225"
"226:â:226"
"227:ã:227"
"228:ä:228"
"229:å:229"
"230:æ:230"
"231:ç:231"
"232:è:232"
"233:é:233"
"234:ê:234"
"235:ë:235"
"236:ì:236"
"237:í:237"
"238:î:238"
"239:ï:239"
"240:ð:240"
"241:ñ:241"
"242:ò:242"
"243:ó:243"
"244:ô:244"
"245:õ:245"
"246:ö:246"
"247:÷:247"
"248:ø:248"
"249:ù:249"
"250:ú:250"
"251:û:251"
"252:ü:252"
"253:ý:253"
"254:þ:254"
"255:ÿ:255"
Dave Benham