DosTips.com

A Forum all about DOS Batch
It is currently 26 May 2016 22:31

All times are UTC-06:00




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
PostPosted: 07 Dec 2011 21:52 
Offline

Joined: 07 Dec 2011 14:34
Posts: 15
Hi Guys,

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


Top
   
PostPosted: 08 Dec 2011 00:41 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1638
Location: United States (east coast)
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


Top
   
PostPosted: 08 Dec 2011 01:09 
Offline

Joined: 07 Dec 2011 14:34
Posts: 15
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


Top
   
PostPosted: 08 Dec 2011 07:26 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1638
Location: United States (east coast)
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


Top
   
PostPosted: 08 Dec 2011 10:17 
Offline

Joined: 07 Dec 2011 14:34
Posts: 15
Thank you dbenham i appreciate your help.

All my questions regarding this are answered.

I'm hoping to get more tips from you.


Top
   
PostPosted: 06 Feb 2014 22:56 
Offline

Joined: 06 Feb 2014 22:47
Posts: 1
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
)


Top
   
PostPosted: 21 Feb 2016 04:44 
Offline

Joined: 21 Feb 2016 04:31
Posts: 1
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,


Top
   
PostPosted: 21 Feb 2016 16:22 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1987
Location: Germany
Obviously you are looking for a hex dump function.
viewtopic.php?p=7407#p7407

Regards
aGerman


Top
   
PostPosted: 21 Feb 2016 17:14 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1137
Location: México City, México
Here it is a simpler way to do that:

Code:
@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:
C:\> echo --> file.txt

C:\> type file.txt
--

C:\> test.bat file.txt
Hex: 2D2D0D0A
Dec: 45451310

Antonio


Top
   
PostPosted: 21 Feb 2016 18:46 
Offline

Joined: 08 Feb 2016 20:25
Posts: 70
Or use your very own ASCII.EXE util?
viewtopic.php?f=3&t=3428


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot] and 4 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® Forum Software © phpBB Limited