Page 1 of 1
Is it possible to convert a character to its ASCII value?
Posted: 07 Dec 2011 21:52
by CodedZyntaX
Hi Guys,
Is it possible to convert a character to its ASCII value using a batch file?
Re: Is it possible to convert a character to its ASCII value
Posted: 08 Dec 2011 00:41
by dbenham
Here is a pure batch solution I developed with jeb's help:
Re: new functions: :chr, :asc, :asciiMap It is a lot of code, but it performs fairly well and supports all characters except 0x00.
There is quick and dirty way to convert ascii codes between 32 and 126 into the character. For example, the code below will echo "A"
Code: Select all
cmd /c exit 65
echo %=exitcodeAscii%
Finally, there is a nifty Command Line Calculator derived from AutoIT that supports a huge range of functions, including functions that will convert between ASCII codes and ASCII characters (both directions).
http://www.brothersoft.com/command-line ... 53523.htmlAgain, this is slower than the pure batch functions because of the overhead of launching a rather large executable for each conversion. But it is very convenient to use.
Dave Benham
Re: Is it possible to convert a character to its ASCII value
Posted: 08 Dec 2011 01:09
by CodedZyntaX
Thank you for your response.
I like the sample code using cmd /c exit.
May I have sample sample usage and output of the bat file that you just posted related to my post?
And I already downloaded the command line calculator however I don't know how to use it to convert ascii code to it's value and string or char to ascii code.
CodedZyntaX
Re: Is it possible to convert a character to its ASCII value
Posted: 08 Dec 2011 07:26
by dbenham
CHARLIB - gives general help
CHARLIB HELP - lists available commands
CHARLIB HELP ASC - gives detailed help about the ASC command
Here is one simple script to convert ASCII code 65 to character A and back again
Code: Select all
call charlib chr 65 myChar
call charlib asc myChar 0 myCode
echo myChar=%myChar%
echo myCode=%myCode%
Finally, the end of CHARLIB.BAT (look at the source), is a TEST routine that exercises all the functions. Lots of examples there.
--------------------------------
I recommend renaming the command line calculator CALC.EXE to CLC.EXE so it is not confused with the Microsoft calculator.
The complete list of AutoIt functions is here:
http://www.autoitscript.com/autoit3/docs/functions.htmI think most of the functions are available in CLC.EXE
Sample usage:
To get results in variables:
Code: Select all
for /f %%A in ('clc asc("A"^)') do set "myCode=%%A"
for /f %%A in ('clc chr(65^)') do set "myChar=%%A"
The closing ) in the CLC function call must be escaped as shown.
-----------------------------------------
Dave Benham
Re: Is it possible to convert a character to its ASCII value
Posted: 08 Dec 2011 10:17
by CodedZyntaX
Thank you dbenham i appreciate your help.
All my questions regarding this are answered.
I'm hoping to get more tips from you.
Re: Is it possible to convert a character to its ASCII value
Posted: 06 Feb 2014 22:56
by unclemeat
I used this thread for a code golf challenge - you can essentially do what you're asking, take a look at the link below:
http://codegolf.stackexchange.com/quest ... 0268#20268Simplifying that a bit - pass this script a single character to be converted to it's ascii value.
Code: Select all
@echo off
setLocal enableDelayedExpansion
for /L %%a in (33,1,126) do (
cmd /c exit %%a
if "!=exitcodeAscii!"=="%~1" echo %%a
)
Re: Is it possible to convert a character to its ASCII value?
Posted: 21 Feb 2016 04:44
by ronnie2177
I believe the question was "
Is it possible to convert a character to its ASCII value using a batch file?"
So far the answers given are to display a character using it's ASCII value (code).
How do I get DOS to display "4545" when text file contents are "--"?
THanks,
Re: Is it possible to convert a character to its ASCII value?
Posted: 21 Feb 2016 16:22
by aGerman
Obviously you are looking for a hex dump function.
viewtopic.php?p=7407#p7407Regards
aGerman
Re: Is it possible to convert a character to its ASCII value?
Posted: 21 Feb 2016 17:14
by Aacini
Here it is a simpler way to do that:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for %%a in (%1) do fsutil file createnew zero.tmp %%~Za > NUL
set "hex="
set "dec="
for /F "skip=1 tokens=2" %%a in ('fc /B %1 zero.tmp') do (
set "hex=!hex!%%a"
set /A "d=0x%%a"
set "dec=!dec!!d!"
)
del zero.tmp
echo Hex: %hex%
echo Dec: %dec%
For example:
Code: Select all
C:\> echo --> file.txt
C:\> type file.txt
--
C:\> test.bat file.txt
Hex: 2D2D0D0A
Dec: 45451310
Antonio
Re: Is it possible to convert a character to its ASCII value?
Posted: 21 Feb 2016 18:46
by mirrormirror
Or use your very own ASCII.EXE util?
viewtopic.php?f=3&t=3428