Page 1 of 1

[Functional code] Convert decimal number to hexadecimal

Posted: 20 Aug 2010 14:04
by carlos
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

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 20 Aug 2010 19:43
by ghostmachine4
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.

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 20 Aug 2010 19:47
by orange_batch
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.

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 20 Aug 2010 20:28
by ghostmachine4
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.

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 20 Aug 2010 20:52
by orange_batch
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?

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 20 Aug 2010 21:15
by ghostmachine4
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.

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 21 Aug 2010 12:59
by carlos
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

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 21 Aug 2010 14:21
by ghostmachine4
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.

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 21 Aug 2010 17:27
by carlos
Thanks for comments.
Anyways, this is a forum of dos batch.

Re: [Functional code] Convert decimal number to hexadecimal

Posted: 21 Aug 2010 20:56
by ghostmachine4
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.