View unanswered posts | View active topics It is currently 24 Apr 2014 04:59



Post new topic Reply to topic  [ 10 posts ] 
[Functional code] Convert decimal number to hexadecimal 
Author Message

Joined: 20 Aug 2010 13:57
Posts: 314
Location: Chile
Post [Functional code] Convert decimal number to hexadecimal
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:
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.



20 Aug 2010 14:04
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post Re: [Functional code] Convert decimal number to hexadecimal
Its not truly functional. It breaks with bigger numbers.

Code:
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.


20 Aug 2010 19:43
Profile
Expert

Joined: 01 Aug 2010 17:13
Posts: 442
Location: Canadian Pacific
Post Re: [Functional code] Convert decimal number to hexadecimal
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.


20 Aug 2010 19:47
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post Re: [Functional code] Convert decimal number to hexadecimal
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.

Quote:
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.


20 Aug 2010 20:28
Profile
Expert

Joined: 01 Aug 2010 17:13
Posts: 442
Location: Canadian Pacific
Post Re: [Functional code] Convert decimal number to hexadecimal
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:
Quote:
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?


20 Aug 2010 20:52
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post Re: [Functional code] Convert decimal number to hexadecimal
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?

Quote:
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)


Quote:
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.


20 Aug 2010 21:15
Profile

Joined: 20 Aug 2010 13:57
Posts: 314
Location: Chile
Post Re: [Functional code] Convert decimal number to hexadecimal
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.



21 Aug 2010 12:59
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post Re: [Functional code] Convert decimal number to hexadecimal
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.


21 Aug 2010 14:21
Profile

Joined: 20 Aug 2010 13:57
Posts: 314
Location: Chile
Post Re: [Functional code] Convert decimal number to hexadecimal
Thanks for comments.
Anyways, this is a forum of dos batch.


21 Aug 2010 17:27
Profile WWW

Joined: 12 May 2006 01:13
Posts: 315
Post Re: [Functional code] Convert decimal number to hexadecimal
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.


21 Aug 2010 20:56
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Darkbatcher, MSN [Bot] and 13 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 © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.