Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to represent a fraction in HEX?

Reply
Thread Tools

How to represent a fraction in HEX?

 
 
Steve
Guest
Posts: n/a
 
      04-15-2004
I am having trouble finding the answer to this question, I'm thinking
the solution must be blindingly obvious, so therefore of course I
cannot see it.

I wish to take a fraction in DEC say .4 and convert it to a HEX value.
I also need to find a way to determine if a number is a fraction in
HEX and convert it back to DEC.

(This would be from a disassembly, and we are learning this for
troubleshooting purposes)

My instructor showed us this, but he has got to be the most boring
person on earth and to be honest I think I dozed off for a second. I
really want to be a proffesional computer programmer, and nothing
about this is listed in the book. But we have a homework assignment
and I have 3 days to get this done. There is a list of 50 numbers in
both HEX and DEC. I won't list them here cuz I want to solve the
problems myself, I just happen to need to be pointed in the right
direction for solving them, a formula or something would be nice.

BTW I have tried the calc.exe HEX function, and when I select .4 then
switch to HEX I get 0, which I know cannot be the right answer (does
explain alot of problems with windows though

I know that the problem is related to disassembly, but the course is
C++ so I'm really hoping to not got flamed for an irrelevant post.

Thank you in advance for your replies... BTW the email is made up, so
only reply here... Thank you
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      04-15-2004

"Steve" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I am having trouble finding the answer to this question, I'm thinking
> the solution must be blindingly obvious, so therefore of course I
> cannot see it.
>
> I wish to take a fraction in DEC say .4 and convert it to a HEX value.
> I also need to find a way to determine if a number is a fraction in
> HEX and convert it back to DEC.
>
> (This would be from a disassembly, and we are learning this for
> troubleshooting purposes)
>
> My instructor showed us this, but he has got to be the most boring
> person on earth and to be honest I think I dozed off for a second. I
> really want to be a proffesional computer programmer, and nothing
> about this is listed in the book. But we have a homework assignment
> and I have 3 days to get this done. There is a list of 50 numbers in
> both HEX and DEC. I won't list them here cuz I want to solve the
> problems myself, I just happen to need to be pointed in the right
> direction for solving them, a formula or something would be nice.
>
> BTW I have tried the calc.exe HEX function, and when I select .4 then
> switch to HEX I get 0, which I know cannot be the right answer (does
> explain alot of problems with windows though
>
> I know that the problem is related to disassembly, but the course is
> C++ so I'm really hoping to not got flamed for an irrelevant post.
>
> Thank you in advance for your replies... BTW the email is made up, so
> only reply here... Thank you


This depends on which representation you use for storing numbers, for
example floating point or fixed point. Then if you choose floating point,
there are a number of ways of representing them. Now, the standard does not
say which of these have to be used, otherwise people would have to designt
hardware around the language!
The reason for calc not showing you the answer is because hex is almost
always used for displaying integers, although there is no reason it can't be
used for anything else as it is just a short-hand representation for binary.
So the short answer is, you will have to program your own.
Allan


 
Reply With Quote
 
 
 
 
Till Crueger
Guest
Posts: n/a
 
      04-15-2004
On Wed, 14 Apr 2004 22:54:15 +0000, Steve wrote:

> I am having trouble finding the answer to this question, I'm thinking
> the solution must be blindingly obvious, so therefore of course I cannot
> see it.
>
> I wish to take a fraction in DEC say .4 and convert it to a HEX value. I
> also need to find a way to determine if a number is a fraction in HEX
> and convert it back to DEC.


[snip]

> I won't list them here cuz I want to solve the problems myself, I just
> happen to need to be pointed in the right direction for solving them, a
> formula or something would be nice.


There is a simple algorithm you can use for conversion of fractions. I can
give you the algorithm, so you can implement ist for yourself.

100 multiply the number by the base you are converting to
200 take the number greater than one, and use it as the
next digit in your converted number
300 discard anything greater than one
400 repeat until satisfied or left with zero

Example:
..4 * 16 = 6.4 (result 0.6)
..4 * 16 = 6.4 (result 0.66)
....


Example 2:
0.21875 * 16 = 3.5 (result 0.3)
0.5 * 16 = 8 (result 0.3
done

When you have figured out how fractions work, it should be fairly easy to
see how this algorithm works.
Till

--
Please add "Salt and Peper" to the subject line to bypass my spam filter

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      04-15-2004
On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++, http://www.velocityreviews.com/forums/(E-Mail Removed)
(Steve) wrote,
>BTW I have tried the calc.exe HEX function, and when I select .4 then
>switch to HEX I get 0, which I know cannot be the right answer (does
>explain alot of problems with windows though


A lot of people apparently think there is something about base 16 that
makes it apply only to integers, unlike base 10. At least you are ahead
of them. OK, I will take pity on your plight, even though I failed to
see anything whatsoever regarding C++ in your question. Don't let it
happen again.

In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
number of hundredths, 'y' is the number of thousandths, and z is the
number of ten thousandths.

If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
4096ths, and 'z' the number of 1/(16^4) or 65536ths.

So, how many 16ths is 0.40 decimal, and what is the remainder?
How many 256ths is the remainder? And so on.

You might be likely to see hex fractions related to C++ code if you dump
float or double numbers in hex.

 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      04-15-2004
>
> A lot of people apparently think there is something about base 16 that
> makes it apply only to integers, unlike base 10. At least you are ahead
> of them. OK, I will take pity on your plight, even though I failed to
> see anything whatsoever regarding C++ in your question. Don't let it
> happen again.
>
> In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> number of hundredths, 'y' is the number of thousandths, and z is the
> number of ten thousandths.
>
> If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
>
> So, how many 16ths is 0.40 decimal, and what is the remainder?
> How many 256ths is the remainder? And so on.
>
> You might be likely to see hex fractions related to C++ code if you dump
> float or double numbers in hex.
>


I agree, however it depends on what type of number is being represented.
e.g. 0xFF can be:

1) 255 if we are using an obvious represntation for an unsigned 8-bit type

2) -127 if we are using an obvious representation for a signed 8-bit type

3) 127.9375 if we are using a fixed point 8-bit type with 7bit before the
'.' and 1 bit after

My point is, there is not only one way of doing this, things get even more
difficult if floating points are considered.
Allan


 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      04-15-2004

"David Harmon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++, (E-Mail Removed)
> (Steve) wrote,
> >BTW I have tried the calc.exe HEX function, and when I select .4 then
> >switch to HEX I get 0, which I know cannot be the right answer (does
> >explain alot of problems with windows though

>
> A lot of people apparently think there is something about base 16 that
> makes it apply only to integers, unlike base 10. At least you are ahead
> of them. OK, I will take pity on your plight, even though I failed to
> see anything whatsoever regarding C++ in your question. Don't let it
> happen again.
>
> In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> number of hundredths, 'y' is the number of thousandths, and z is the
> number of ten thousandths.
>
> If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
>
> So, how many 16ths is 0.40 decimal, and what is the remainder?
> How many 256ths is the remainder? And so on.
>
> You might be likely to see hex fractions related to C++ code if you dump
> float or double numbers in hex.
>


I agree, however it depends on what type of number is being represented.
e.g. 0xFF can be:

1) 255 if we are using an obvious represntation for an unsigned 8-bit type

2) -127 if we are using an obvious representation for a signed 8-bit type

3) 127.9375 if we are using a fixed point 8-bit type with 7bit before the
'.' and 1 bit after

My point is, there is not only one way of doing this, things get even more
difficult if floating points are considered.
This, unfortunately, is not part of the C++ language, however if you defined
your own representation or decided on one to use, then tried to implement it
in C++, we would be able to help you.
HTH
Allan



 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      04-16-2004
"Allan Bruce" <(E-Mail Removed)> wrote in message news:<c5lurt$l7g$(E-Mail Removed)2surf.net>...
> "David Harmon" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++, (E-Mail Removed)
> > (Steve) wrote,
> > >BTW I have tried the calc.exe HEX function, and when I select .4 then
> > >switch to HEX I get 0, which I know cannot be the right answer (does
> > >explain alot of problems with windows though

> >
> > A lot of people apparently think there is something about base 16 that
> > makes it apply only to integers, unlike base 10. At least you are ahead
> > of them. OK, I will take pity on your plight, even though I failed to
> > see anything whatsoever regarding C++ in your question. Don't let it
> > happen again.
> >
> > In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> > number of hundredths, 'y' is the number of thousandths, and z is the
> > number of ten thousandths.
> >
> > If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> > number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> > 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
> >
> > So, how many 16ths is 0.40 decimal, and what is the remainder?
> > How many 256ths is the remainder? And so on.
> >
> > You might be likely to see hex fractions related to C++ code if you dump
> > float or double numbers in hex.
> >

>
> I agree, however it depends on what type of number is being represented.
> e.g. 0xFF can be:
>
> 1) 255 if we are using an obvious represntation for an unsigned 8-bit type
>
> 2) -127 if we are using an obvious representation for a signed 8-bit type
>
> 3) 127.9375 if we are using a fixed point 8-bit type with 7bit before the
> '.' and 1 bit after
>
> My point is, there is not only one way of doing this, things get even more
> difficult if floating points are considered.
> This, unfortunately, is not part of the C++ language, however if you defined
> your own representation or decided on one to use, then tried to implement it
> in C++, we would be able to help you.
> HTH
> Allan


I guess to simplfy the question

float MyVar = 0.4;

What should I be looking for in disassembly.
Everytime I try to calculate 0.4 DEC to HEX I keep coming up with 0x0
which HAS to be wrong. So can someone please show me what that would
look like in a memory map and how to convert it into a hex value? I
really don't know where to look, nor where to go.

I don't need a program here, just a formula would be nice.
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-16-2004
Steve wrote:
>
> I don't need a program here, just a formula would be nice.


Here is the way I was taught to generate base 16 floating point numbers
(using your example to 6 places).

0.4 x 2 = 0.8
HexDigit = Int(0. = 0 *
Next multiplier = Frac(0. = 0.8

0.8 x 2 = 1.6
HexDigit = Int(1.6) = 1 *
Next multiplier = Frac(1.6) = 0.6


0.6 x 2 = 1.2
HexDigit = Int(1.2) = 1 *
Next multiplier = Frac(1.2) = 0.2

0.2 x 2 = 0.4
HexDigit = Int(0.4) = 0 *
Next multiplier = Frac(0.4) = 0.4

0.4 x 2 = 0.8
HexDigit = Int(0. = 0 *
Next multiplier = Frac(0. = 0.8

0.8 x 2 = 1.6
HexDigit = Int(1.6) = 1 *
Next multiplier = Frac(1.6) = 0.6

Now gather up the hexdigits that you generated in order (the ones with
the * next to them. This will be your fractional hex answer:

0.4 (decimal) = 0.011001 (hex)

Hopefully you see the pattern. I don't have time to write the general
formula, but hopefully you understand.

Paul
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-16-2004
Paul wrote:

> Hopefully you see the pattern. I don't have time to write the general
> formula, but hopefully you understand.
>
> Paul

oops, sorry. Forgot that this is only the binary. The hex is one more
step, which I will leave to you for an exercise.

Paul McKenzie
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-16-2004
Paul wrote:

> Steve wrote:
>
>>
>> I don't need a program here, just a formula would be nice.

>
>
> Here is the way I was taught to generate base 16 floating point numbers
> (using your example to 6 places).
>
> 0.4 x 2 = 0.8
> HexDigit = Int(0. = 0 *
> Next multiplier = Frac(0. = 0.8
>
> 0.8 x 2 = 1.6
> HexDigit = Int(1.6) = 1 *
> Next multiplier = Frac(1.6) = 0.6
>
>
> 0.6 x 2 = 1.2
> HexDigit = Int(1.2) = 1 *
> Next multiplier = Frac(1.2) = 0.2
>
> 0.2 x 2 = 0.4
> HexDigit = Int(0.4) = 0 *
> Next multiplier = Frac(0.4) = 0.4
>
> 0.4 x 2 = 0.8
> HexDigit = Int(0. = 0 *
> Next multiplier = Frac(0. = 0.8
>
> 0.8 x 2 = 1.6
> HexDigit = Int(1.6) = 1 *
> Next multiplier = Frac(1.6) = 0.6
>
> Now gather up the hexdigits that you generated in order (the ones with
> the * next to them. This will be your fractional hex answer:
>
> 0.4 (decimal) = 0.011001 (hex)
>
> Hopefully you see the pattern. I don't have time to write the general
> formula, but hopefully you understand.
>
> Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
The Power of the Pixel; 5 Years of Advertising at Only a Fraction of the Normal Cost. walter.alex@gmail.com HTML 4 11-20-2005 11:18 PM
How to MULTIPLY by fraction ?? (making variable iir) Jam VHDL 2 09-15-2004 11:44 AM
String Fraction to Number Peter Java 6 05-06-2004 08:24 PM
Re: fraction reducing funtion Sam Holden C++ 2 08-04-2003 04:45 AM



Advertisments