[Functional code] Convert decimal number to hexadecimal

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Posts: 431
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

[Functional code] Convert decimal number to hexadecimal

#1 Post by carlos » 20 Aug 2010 14:04

This code convert decimal number to hexadecimal.
This code is really fast and short, it use a undocumented variable of cmd.

Note: Only works fine with positive 32 bits signed integer numbers.

Code: Select all

setlocal enabledelayedexpansion
set "hex_values=0123456789ABCDEF"
set "hex="
set /a "n=%1+0"
:_d2h
set /a "m=n%%16"
set "hex=!hex_values:~%m%,1!!hex!"
if %n% geq 16 (set /a "n/=16" & goto :_d2h)
echo.%hex%
endlocal
goto :eof
Last edited by carlos on 01 Oct 2011 11:32, edited 4 times in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: [Functional code] Convert decimal number to hexadecimal

#2 Post by ghostmachine4 » 20 Aug 2010 19:43

Its not truly functional. It breaks with bigger numbers.

Code: Select all

C:\test>test.bat 1234567890
499602D2
Press any key to continue . . .

C:\test>gawk "BEGIN{printf \"%x\n\",\"1234567890\"}"
499602d2

C:\test>test.bat 12345678901234567890
EB1F0AD2
Press any key to continue . . .

C:\test>gawk "BEGIN{printf \"%x\n\",\"12345678901234567890\"}"
ab54a98ceb1f0800


i never get tired of repeating. Use a proper tool for the job, that is both easy to use and understand without having to create obscure workarounds.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: [Functional code] Convert decimal number to hexadecimal

#3 Post by orange_batch » 20 Aug 2010 19:47

Huh. I'll be damned. I thought my dec2hex was as good as it could get. Thanks for this.

That's true ghostmachine, but it's fine if you don't need a hex number larger than it's limitation. Also using add-on tools makes software less distributable and bulkier.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: [Functional code] Convert decimal number to hexadecimal

#4 Post by ghostmachine4 » 20 Aug 2010 20:28

orange_batch wrote:That's true ghostmachine, but it's fine if you don't need a hex number larger than it's limitation.

you will never know. Always try to write resilient code, not code that breaks someday when things happen without your noticing.

Also using add-on tools makes software less distributable and bulkier.

Where did you get that notion. ?
Code created from languages like Python/Perl works on most other plaforms, Win32, linux, Mac with minimal or no changes.
Even gawk.exe that i always use. Batch has no use in modern days except for doing really simple things like copy/moving files etc. Turn to something like Powershell/vbscript,Perl/Python for better programming/scripting experiences.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: [Functional code] Convert decimal number to hexadecimal

#5 Post by orange_batch » 20 Aug 2010 20:52

ghostmachine4 wrote:
orange_batch wrote:That's true ghostmachine, but it's fine if you don't need a hex number larger than it's limitation.

you will never know. Always try to write resilient code, not code that breaks someday when things happen without your noticing.

Well for example, DOS works with 32-bit integers, which when working with file sizes in bytes limits to 2.147~ gigabytes. I split the integers and converted the sums to megabytes to support 21.47 terabytes (it could be split to support 214.7 terabytes even). Everything has a limit, but if the numbers someone is working with are in range then how high the limit is is irrelevant.

ghostmachine4 wrote:
Also using add-on tools makes software less distributable and bulkier.

Where did you get that notion. ?
Code created from languages like Python/Perl works on most other plaforms, Win32, linux, Mac with minimal or no changes.
Even gawk.exe that i always use. Batch has no use in modern days except for doing really simple things like copy/moving files etc. Turn to something like Powershell/vbscript,Perl/Python for better programming/scripting experiences.

Sorry, I mean that's if you can't include dependencies when compiled, and bulkiness depends on the language's efficiency and those dependencies. As you say, it is smarter to work in better languages.

It's funny, this is a DOS forum, but should we be helping with DOS or egging people away from it? :P

Also, which of those languages do you recommend most?

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: [Functional code] Convert decimal number to hexadecimal

#6 Post by ghostmachine4 » 20 Aug 2010 21:15

orange_batch wrote:Everything has a limit, but if the numbers someone is working with are in range then how high the limit is is irrelevant.

that's why i say, you will never know. how would you know that tomorrow you won't be working with big numbers?

It's funny, this is a DOS forum, but should we be helping with DOS or egging people away from it? :P

Yes, this is DOS (batch) forum, but all i did was point out something that is a fact, that the code breaks on big numbers (which is also not that big).
And i recommended to use tools that are better (a lot) suited for the job, without having to think up obscure workarounds (that doesn't work)


Also, which of those languages do you recommend most?

I do have my personal favourites (one of the "P"s) , but to do a recommendation here is not appropriate. This is because human beings are different. You will have to experience those programming languages to find out your own personal preferences.

carlos
Posts: 431
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: [Functional code] Convert decimal number to hexadecimal

#7 Post by carlos » 21 Aug 2010 12:59

Thanks for your comments.
I added this note:
Note: Only works fine with positive 32 bits signed integer numbers.
This is from: 0 to 2147483647 that is the maximal number that you can catch with set /a command, really the maximal number is 4294967295 but this number exceed the limit of set /a that is from -2147483648 to 2147483647
Last edited by carlos on 01 Oct 2011 11:25, edited 2 times in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: [Functional code] Convert decimal number to hexadecimal

#8 Post by ghostmachine4 » 21 Aug 2010 14:21

carlos wrote:Thanks for your comments.
I added this note:
Note: Only works fine with positive 32 bits signed integer numbers.
This is from: 0 to 2147483647 that is the maximal number that you can catch with set /a command, really the maximal number is 4294967295 but this number exceed the limit of set /a that is from -2147483648 to 2147483647

Then if you have a number from 0 to 2147483647 you can use my code that is really functional and speed with this limits, else you can use another external tool, how awk.

a tool such as awk/Perl/Python works just as fast, is cross platform and doesn't break with big numbers. I would use that instead of yours anytime. But its a good effort nonetheless for showing others about one of batch's limitations.

carlos
Posts: 431
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: [Functional code] Convert decimal number to hexadecimal

#9 Post by carlos » 21 Aug 2010 17:27

Thanks for comments.
Anyways, this is a forum of dos batch.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: [Functional code] Convert decimal number to hexadecimal

#10 Post by ghostmachine4 » 21 Aug 2010 20:56

carlos wrote:Thanks for comments.
Anyways, this is a forum of dos batch.

yes i know. Just highlighting some issues if someone wants to use your code in production.

Post Reply