View unanswered posts | View active topics It is currently 21 Oct 2014 12:20



Post new topic Reply to topic  [ 6 posts ] 
Is it possible to convert a character to its ASCII value? 
Author Message

Joined: 07 Dec 2011 14:34
Posts: 15
Post Is it possible to convert a character to its ASCII value?
Hi Guys,

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


07 Dec 2011 21:52
Profile
Expert

Joined: 12 Feb 2011 21:02
Posts: 1229
Location: United States (east coast)
Post Re: Is it possible to convert a character to its ASCII value
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:
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


08 Dec 2011 00:41
Profile

Joined: 07 Dec 2011 14:34
Posts: 15
Post Re: Is it possible to convert a character to its ASCII value
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


08 Dec 2011 01:09
Profile
Expert

Joined: 12 Feb 2011 21:02
Posts: 1229
Location: United States (east coast)
Post Re: Is it possible to convert a character to its ASCII value
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:
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:
clc asc("A")
clc chr(65)

To get results in variables:
Code:
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


08 Dec 2011 07:26
Profile

Joined: 07 Dec 2011 14:34
Posts: 15
Post Re: Is it possible to convert a character to its ASCII value
Thank you dbenham i appreciate your help.

All my questions regarding this are answered.

I'm hoping to get more tips from you.


08 Dec 2011 10:17
Profile

Joined: 06 Feb 2014 22:47
Posts: 1
Post Re: Is it possible to convert a character to its ASCII value
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:
@echo off
setLocal enableDelayedExpansion
for /L %%a in (33,1,126) do (
    cmd /c exit %%a
    if "!=exitcodeAscii!"=="%~1" echo %%a
)


06 Feb 2014 22:56
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.