Thursday, March 13, 2008

A Number Format And Base Converter


This tool was created to help adapt non-VB flags to VB
constants. It can handle numbers 0x03 and 03h hex, also 03L and 03& long ints,
as well as VB &3 oct, &H3 hex and 3 dec formats. It will succeed on incomplete
input and will normalize suffixes without numbers appended, so you could enter
"$" or "x" to know these are hex. This is not ground-breaking, but I decided to
improve it with some great number base and binary converters found on PSC to try
to produce a useful conversion tool. Code from other authors has been used
within their usage guidelines and to the benifit of these copyright owners, with
no intention to infringe on their copyright. Acknowledgement is also in the code
itself. This is not really intended to be "the best way" to convert number
bases, but rather a fun(tional collaboration. Working demo with conversions
funcs in 11k bas for easy re-use. 25k zip.
+++++++++++++++++++++++++++++++++++++++++++++++ For base conversion I found
clever examples like bitwise operations and look-up tables to do the
conversions, but settled for VB's built in funcs to keep it simpler - I use
Hex$(), Oct$(), and CDbl(). You could easily replace these and call out to your
own code solutions. I use CDbl() because it returns a native double and not a
variant. I also recall some limitations with CDec() in certain situations that I
cannot now reproduce? +++++++++++++++++++++++++++++++++++++++++++++++ For
numbers greater than VB Longs I decided to break the job up into &H7FFFFFFF
sized recursions! A great idea but rather tricky. I found some of Randy Birch's
code that converted an unsigned windows long into a VB double to access the 2 *
&H7FFFFFFF positive values found in "to seek into 4G files instead of the 2G
limit", but cannot find again - if this was yours let me know. From this small
code sample I was able to implement handling of unlimited sized decimals! Well,
limited by the 32 bit system anyway.
+++++++++++++++++++++++++++++++++++++++++++++++ Now for binary conversion.
Thanks heaps to MrEnigma for his binary to decimal code, as well as to Fausto C.
Arruda for his number to binary code I found in his Hex Editor. These two pieces
of code are what makes this program capable of handling decimal to binary and
binary to decimal up to around 1.79769313486231580779E+308! That's a lot of
zeros and ones! +++++++++++++++++++++++++++++++++++++++++++++++ Next was to
output positive and negative numbers correctly as binary values. The solution
was to convert negative numbers to unsigned format (unsigned and signed formats
share binary values) and display their correct binary representation. For the
unsigned to signed number conversion Adam Kachwalla's conversion code was just
the ticket. +++++++++++++++++++++++++++++++++++++++++++++++ I have also enabled
XP themes. I used a handy mod and res file I zapped out of Amer Tahir's XP
themes demo control - thanks Amer for this great straight-forward solution.
+++++++++++++++++++++++++++++++++++++++++++++++ Confirmation that the binary and
base outputs are correct would be appreciated. This project has had two lives so
there may be oversights? Bug fix 1-Mar-08: GetBase was modifying the byref input
.

Download

0 Responses: