Converting Hex Fraction to Dec

J

Jeff Dillon

How might I convert a string like 10.A (in hex) to it's decimal equivalent?
Basically I have an input string like ((1F.A + 3A.D) - 1F.E) and need to
calculate the result. Using Reflection and dynamic compilation, I was able
to create a VB.NET equivalent of the older Eval function, but it only works
with decimal numbers.

Any thoughts? I know the logic to convert, but I was hoping for some
built-in .NET methods that I could leverage

jeff
 
S

Stephany Young

The question has to asked:

Where does this 'input string' come from?

If it comes from some abstract exercise on your part then your time would be
better utilised musing in the question of 'Life, The Universe and
Everything'.

If it comes from some 'real world' data provider/producer, then you need to
asking the question of that data provider/producer because they, and only
they, can tell you what rules need to be applied and how they should be
applied. If they can't, or won't tell you, then you are, (excuse my french),
farting against thunder.

That said, I must add -

The decimal system has a widely acccepted set of rules that allow one to 'do
math' on decimal numbers, regardless of whether the operands are integral or
fractional. However, the decimal representation of a value must always be
considered in it's context. For example, the number 10.5 could represent 10
dollars and 50 cents or it could represent 10 and a half days or it could
represent 10 and a half widgets. There are accepted extensions to decimal
notation to indicate context like money being shown in conjunction with a
currency symbol and there are other extensions to indicate signage (+ and -)
while there are still other extensions to make the representation more
readable, like thousands seperators. This makes it easy to show a number
like $10,000.00 and everybody should interpret as ten thousand dollars
without any ambiguity.

Hexadecimal, on the other hand, is nothing more than a notational system and
it primary use is for representing binary values in a more human readable
form. It has no widely accepted extensions for indicating context, signage
or readability.

Take the hexadecimal value FF. What does it represent? It could represent
255 as a byte. It could represent -1 as a signed byte. It could represent 2
seperate 4-bit values of 15 each. The longer the string of hexadecimal
characters the more permuatations there are of possible meanings.

The next point is that hexadicamal notation has no concept of a 'hexadecimal
point' and therefore has no rules regarding placeholding to the right of
such a point (if it were to exist).

You cannot apply the rules that govern the use of a decimal point because
those rules relate only to the the decimal system and have no meaning in
any other 'base' system.
 
T

teslar91

I must admit, *fractional* hex is pretty weird; I've never seen it used
and can't imagine what it would be used for. But Stephany, I don't see
why you think this is so ambiguous.

It's pretty obvious to me that the data type is a hex Float. What
*unit* it represents - days, dollars, ducks - is irrelevant.

In decimal, the numbers proceeding leftwards from the decimal point
represent:
1's
10's
100's
1000's
etc.

And from the right mean:
1/10's
1/100's
1/1000's
etc.

Yes, that's all obvious - but stick with me. In hex, proceeding from
the left of the decimal point:
1's
16's
256's
4096's

So we logically extend this consistent pattern to determine what the
hex numbers to the right of the decimal represent:
1/16's
1/256's
1/4096's

There's nothing magic about the minus sign either. If present, it
would denote a negative number regardless of numerical base.

And we can do that for any number system and/or precision. 2.5
(decimal) could be exactly represented as:

10.1 (binary)
2.4 (octal)
2.8 (hexadecimal)
2.G (base-36)

Yes, I've actually used base-36; it uses the numbers and *all* the
letters. (I used it for shrinking a long decimal number, such as a
drive serial, into the smallest possible easily human-readable/typable
string for keycode-based software registration.) I have some functions
that will correctly convert decimal to/from *any* arbitrary numerical
base, from binary to base-36, and everything inbetween.

It doesn't do fractions, though! And I'm sorry, but I can't help with
the original query within the conditions specified - using a built-in
VB language trick to facilitate conversion.
 
T

teslar91

Correction: 2.5 (decimal) = 2.I (base-36). What can I say, I'm not
good at doing base-36 conversions in my head. :)
 
J

Jeff Dillon

I have no idea about your very odd response.

This is a take-home interview question, you idiot

You wasted your time (and mine) in your nonsense reply. Of course other base
systems can have decimal points

I didn't need a stupid lesson in hex numbers. Get a life
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top