Is it possible to convert a character to its ASCII value?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
CodedZyntaX
Posts: 15
Joined: 07 Dec 2011 14:34

Is it possible to convert a character to its ASCII value?

#1 Post by CodedZyntaX » 07 Dec 2011 21:52

Hi Guys,

Is it possible to convert a character to its ASCII value using a batch file?

dbenham
Expert
Posts: 2018
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Is it possible to convert a character to its ASCII value

#2 Post by dbenham » 08 Dec 2011 00:41

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.html
Again, 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

CodedZyntaX
Posts: 15
Joined: 07 Dec 2011 14:34

Re: Is it possible to convert a character to its ASCII value

#3 Post by CodedZyntaX » 08 Dec 2011 01:09

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

dbenham
Expert
Posts: 2018
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Is it possible to convert a character to its ASCII value

#4 Post by dbenham » 08 Dec 2011 07:26

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.htm
I think most of the functions are available in CLC.EXE

Sample usage:

Code: Select all

clc asc("A")
clc chr(65)

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

CodedZyntaX
Posts: 15
Joined: 07 Dec 2011 14:34

Re: Is it possible to convert a character to its ASCII value

#5 Post by CodedZyntaX » 08 Dec 2011 10:17

Thank you dbenham i appreciate your help.

All my questions regarding this are answered.

I'm hoping to get more tips from you.

unclemeat
Posts: 1
Joined: 06 Feb 2014 22:47

Re: Is it possible to convert a character to its ASCII value

#6 Post by unclemeat » 06 Feb 2014 22:56

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#20268

Simplifying 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
)

ronnie2177
Posts: 1
Joined: 21 Feb 2016 04:31

Re: Is it possible to convert a character to its ASCII value?

#7 Post by ronnie2177 » 21 Feb 2016 04:44

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,

aGerman
Expert
Posts: 3178
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Is it possible to convert a character to its ASCII value?

#8 Post by aGerman » 21 Feb 2016 16:22

Obviously you are looking for a hex dump function.
viewtopic.php?p=7407#p7407

Regards
aGerman

Aacini
Expert
Posts: 1470
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Is it possible to convert a character to its ASCII value?

#9 Post by Aacini » 21 Feb 2016 17:14

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

mirrormirror
Posts: 127
Joined: 08 Feb 2016 20:25

Re: Is it possible to convert a character to its ASCII value?

#10 Post by mirrormirror » 21 Feb 2016 18:46

Or use your very own ASCII.EXE util?
viewtopic.php?f=3&t=3428

Post Reply