Jump to content











Photo
- - - - -

YACBFC - dec2hex.cmd and hex2dec.cmd


  • Please log in to reply
10 replies to this topic

#1 was_jaclaz

was_jaclaz

    Finder

  • Advanced user
  • 7101 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
  • 7101 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
  • 12707 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
  • 7101 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
  • 7101 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
  • 16066 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
  • 16066 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
  • 16066 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

#9 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 29 February 2016 - 02:44 PM

A new kid on the block (actually only a slight mod of multiply.cmd):
 

KMG2B.cmd :
small batch file to convert numbers expressed
in Kb, Mb, Gb into bytes, overcoming the 0x07FFFFFF
limit in batch (aka 2,147,483,647)
this is about "real" K/M/G, i.e. powers of 1024
what would be now called KiB, MiB, GiB.

******** Version 1.0 29 February 2016 ********
by jaclaz

This file is licensed under my "CAREWARE" license:
http://jaclaz.altervista.org/Projects/careware.html

Usage:
KMG2B.cmd Integer[K|M|G]

:duff:

Wonko

Attached Files


  • imnothing likes this

#10 devdevadev

devdevadev

    Silver Member

  • Advanced user
  • 540 posts
  •  
    India

Posted 20 August 2017 - 04:18 PM

Is B2KMG.cmd (small batch file to convert numbers expressed in bytes into Kb, Mb, Gb) also possible ?


#11 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 23 August 2017 - 11:43 AM

 

Is B2KMG.cmd (small batch file to convert numbers expressed in bytes into Kb, Mb, Gb) also possible ?

 

Sure it is possible.



@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET N=%1
:L
SET R=
FOR /L %%? IN (14,-1,1) DO (
SET P=!N:~0,%%?!
IF !P!==%N% SET L=%%?
)
SET C=0
FOR /L %%? IN (0,1,%L%) DO (
IF %%?==%L% GOTO :O
SET /A D=10*!C!+!N:~%%?,1!
SET /A V=!D!/2
SET /A C=!D!-!V!-!V!
SET R=!R!!V!
)
:O
IF "0"=="!R:~0,1!" SET R=!R:~1!
IF NOT DEFINED R SET R=0
SET /A I+=1
IF %I% EQU 10 ECHO %R% KB
IF %I% EQU 20 ECHO %R% MB
IF %I% EQU 30 ECHO %R% GB
IF %I% LSS 30 SET N=%R%&GOTO :L

Still the whole idea of posting these little batches is that people can get inspired by this or that little trick and write his/her own batches.

 

Another approach:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET K=%1
FOR /L %%K IN (0,1,9) DO SET K=!K:%%K=%%K !

CALL :B K
CALL :B M
CALL :B G
GOTO :EOF

:B
SET M=
FOR /L %%K IN (1,1,10) DO (
SET G=
IF NOT "!K!"==" 0" CALL :K !K!
)
SET M=!K: =!
ECHO %M% %1B
GOTO :EOF


:K
SET /A B=%G%%1/2
CALL :%1
SET M=%M% %B%
IF "%M:~,3%"==" 0 " SET M=%M:~2%
SHIFT
IF NOT %1.==. GOTO :K
SET K=!M!&&SET M=
GOTO :EOF

:M
:0
:2
:4
:6
:8
SET G=
GOTO :EOF

:G
:1
:3
:5
:7
:9
SET G=1
GOTO :EOF

:duff:

Wonko






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users