Jump to content











Photo
- - - - -

YACBFC - dec2hex.cmd and hex2dec.cmd


  • Please log in to reply
7 replies to this topic

#1 was_jaclaz

was_jaclaz

    Finder

  • Advanced user
  • 7,100 posts
  • Location:Gone in the mist
  •  
    Italy

Posted 04 September 2007 - 09:20 PM

There are quite a number of converting programs between dec and hex and viceversa, and also a number of batch files to do the same.

Since none I could find worked the way I wanted them to, I decided to write my own version, with input an output as I like them to be.

They work under 2K/XP (no Dos/Win9x/Me sorry).

Enjoy. :loleverybody:

jaclaz

Attached Files



#2 was_jaclaz

was_jaclaz

    Finder

  • Advanced user
  • 7,100 posts
  • Location:Gone in the mist
  •  
    Italy

Posted 05 October 2007 - 10:42 AM

Another pretty much useless batch :cheers:, multiply.cmd.

The batch language can do Integer Math quite well, though all operations have a limit fixed to hex 7FFFFFF i.e. 2147483647 decimal.

Multiply.cmd takes two numbers and multiplies them, with precision up to 15 digits:

W:\OFS\TOOLS>multiply 1024*1024
1048576


The first non numeric character found (exception made for "*") is interpreted as the thousands separator:

W:\OFS\TOOLS>multiply 1,024*1024
1,048,576

W:\OFS\TOOLS>multiply 1.024*1024
1.048.576

W:\OFS\TOOLS>multiply 1.024*1048576
1.073.741.824

W:\OFS\TOOLS>multiply 1.024*1.073.741.824
1.099.511.627.776

W:\OFS\TOOLS>multiply 1024*1024§
1§048§576


jaclaz

EDIT: A new version of multiply command uploaded, see two posts below

Attached Files



#3 pscEx

pscEx

    Platinum Member

  • Team Reboot
  • 12,436 posts
  • Location:Korschenbroich, Germany
  • Interests:What somebody else cannot do.
  •  
    European Union

Posted 12 October 2007 - 04:10 PM

Another pretty much useless batch :cheers: , multiply.cmd.

The batch language can do Integer Math quite well, though all operations have a limit fixed to hex 7FFFFFF i.e. 2147483647 decimal.

Multiply.cmd takes two numbers and multiplies them, with precision up to 15 digits:


The first non numeric character found (exception made for "*") is interpreted as the thousands separator:


jaclaz

Jaclaz,
You are great :cheers:

Until now I did not even have had the opinion, that somethig like this is possible by simple *.cmd!

Peter

#4 was_jaclaz

was_jaclaz

    Finder

  • Advanced user
  • 7,100 posts
  • Location:Gone in the mist
  •  
    Italy

Posted 14 October 2007 - 10:55 AM

Jaclaz,
You are great :cheers:

Until now I did not even have had the opinion, that somethig like this is possible by simple *.cmd!

Peter


Happy you liked it :cheers:, as always, somebody told me the magic words "you can't do that" thus giving me the needed drive to experiment a bit!" :cheers:

Please find attached:
- a revised version of multiply.cmd (with a small bug correction for multiple separators)
- a new batch, divide.cmd to perform divisions between "big" integers

:cheers:

jaclaz

Attached Files



#5 was_jaclaz

was_jaclaz

    Finder

  • Advanced user
  • 7,100 posts
  • Location:Gone in the mist
  •  
    Italy

Posted 01 December 2008 - 11:26 AM

A modified version of hex2dec.cmd, taking into account negative numbers.

Enjoy. :)

jaclaz

Attached Files



#6 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,848 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 29 March 2010 - 09:46 AM

New version of multiply.cmd.
Has better error control. (which can be removed to speed up things).
Has virtually no limit in the size of the numbers. (old version was hardcoded to 15 digits, now the limit is 100 and easily changeable).

:)
Wonko

Attached Files



#7 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,848 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 01 September 2011 - 01:39 PM

The good news are that you never end learning new things :yahoo:
There is a "normal" provision in SET /A command that will convert a Hex number (with prefix 0x) into a decimal number.
But unless going to the complex way posted before there wasn't any simple way do the opposite (convert a decimal number into a hex number).
Only a few days ago I learned some new things about Dynamic Environment Variables here:
http://www.robvander...e.com/ntset.php
Thanks to Rob Van der Woude for keeping uptodated his pages and thanks to "Smartgenius" for his contribution. :thumbup:

Here is an example script that (once removed the checks for input and whatever you don't actually *need*) will amount to a few lines:

@ECHO OFF

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

::========================================================

::| WARNING! Limit is 2147483647 i.e. 7FFFFFF WARNING! |

::========================================================

:: dec2hex.cmd

:: a simple and nice way to convert decimal numbers to hex numbers

:: by jaclaz, thanks to "SmartGenius" findings on Dynamic Environment Variables

:: and to Rob Van der Woude: http://www.robvanderwoude.com/ntset.php



:input_loop

SET Input_dec=NOTHING

SET /P Input_dec=Input a positive decimal number smaller than 2147483648 :

IF "%Input_dec%"=="NOTHING" GOTO :input_loop

IF %Input_dec% geq 2147483647 ECHO WAAAAY TOO BIG....&GOTO :input_loop

CALL :dec2hex VAR %Input_dec%

SET VAR_

ECHO :check

ECHO SET /A VAR=%VAR%

SET /A VAR=%VAR%

SET VAR

GOTO :EOF





:dec2hex

ECHO %0

CMD /C EXIT /B %2

SET "%1=%=ExitCode%"

SET %1=0x!%1!

SET %1__full=!%1!

SET %1_byte=0x!%1:~-2!

SET %1_word=0x!%1:~-4!

GOTO :EOF

While I'm here, based on the same approach, here is a "poorman's" CHRS() function for batch ;):
@ECHO OFF

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

::========================================================

::| WARNING! Limit is 2147483647 i.e. 7FFFFFF WARNING! |

::========================================================

:: chrs.cmd

:: a simple and nice way to convert decimal numbers to ASCII characters

:: by jaclaz, thanks to "SmartGenius" findings on Dynamic Environment Variables

:: and to Rob Van der Woude: http://www.robvanderwoude.com/ntset.php



:input_loop

SET Input_dec=NOTHING

SET /P Input_dec=Input a positive decimal number smaller than 256:

IF "%Input_dec%"=="NOTHING" GOTO :input_loop

IF %Input_dec% geq 256 ECHO WAAAAY TOO BIG....&GOTO :input_loop

CALL :getchar VAR %Input_dec%

ECHO %Input_dec% is ASCII character %VAR%

GOTO :EOF





:getchar

ECHO %0

CMD /C EXIT /B %2

SET "%1=%=ExitCodeAscii%"

GOTO :EOF

And a quick and dirty ASCII table generator:
@ECHO OFF

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: asciitable.cmd

:: a simple and nice way to print an ASCII table of characters

:: (only "ECHOable" characters)

:: by jaclaz, thanks to "SmartGenius" findings on Dynamic Environment Variables

:: and to Rob Van der Woude: http://www.robvanderwoude.com/ntset.php



FOR /L %%A IN (32,1,126) DO (

CALL :getchar %%A

)

GOTO :EOF





:getchar

CMD /C EXIT /B %1

SET "Line=%=ExitCode%"

SET /A Line_Dec=0x%Line%

SET "Line_hex=0x%Line:~-2%

SET "Line_char=%=ExitCodeAscii%"



ECHO %Line_dec% %Line_hex% "( %Line_char% )"

GOTO :EOF

Have fun :).

:cheers:
Wonko

#8 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 10,848 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 10 April 2014 - 02:55 PM

Just to keep everything as together as possible, an actual practical use of the %=ExitCode% variable, uppercasing hex numbers coming out as small letters from the MS tool SECINSPECT.EXE:
http://reboot.pro/to...ed-drive-image/
http://reboot.pro/to...image/?p=183287

:duff:
Wonko