1. ## Anybody willing to give simple hexadecimal help?

This has been giving me trouble over this past week, I'm desperate, can you guys help?

"write C++ code to display the hexadecimal value stored in memory for the floats q and t below,
float q = 0.1,
t = 0.5;"

I've tried all sorts of things, but the output I always get for "q" is 0.1 . Why is this? I've used the simple
std::hex << q <<
And I've even gotten fancy with that whole
setf(std::ios::hex) thing.

My MOST RECENT attempt looks like this:

#include <iostream>
#include <iomanip>

int main()

{
float number;
number = 0.5;
std::cout.setf(std::ios::hex);
std::cout << "what about the hex function?: " << number << "\n";

return (0);
}

This does nothing. Obviously, this is either WAY off, or I'm doing it wrong.

If you guys have ANY ideas, suggestions, comments, please share them! Even the smallest and seemingly insignificant responses will be greatly appreaciated.

2. Outputting values in hex only affects integer types I think... it wouldn't do anything with floats. Also, the way you were doing it you would need to clear the basefield bits, see below.

Code:
```#include <iostream>

int main()
{
int number;
number = 32;
std::cout.setf(std::ios::hex,std::ios::basefield);
std::cout << "what about the hex function?: " << number << "\n";

return (0);
}```
Should output: 20

You could also use the hex stream manipulator, but once again you would need to use this with integer types.

Code:
```#include <iostream>
#include <iomanip>

int main()
{
int number;
number = 32;
std::cout << "what about the hex function?: " << std::hex << number << "\n";

return (0);
}```

Edit: Use code tags

3. int's and float's are mostly nowadays 32 bit wide.
So trick the compiler
Code:
```float my_float=....;
int my_int = *(int*)&my_float;```
So you can use bitwise operators with the my_int variable. Or print it in hex.

4. Originally Posted by phosphorousgobu
This has been giving me trouble over this past week, I'm desperate, can you guys help?

"write C++ code to display the hexadecimal value stored in memory for the floats q and t below,
float q = 0.1,
t = 0.5;"

I've tried all sorts of things, but the output I always get for "q" is 0.1 . Why is this? I've used the simple

Two things: If you want to see the individual bytes of a variable, define a (char *) and set the pointer to point to the address of the first byte of the variable

Like this:
Code:
```  unsigned char *chpoint;
chpoint = (unsigned char *)&q;```
THen you want to step through the bytes:

Code:
```  for (int i = 0; i < sizeof(x); i++) {
cout << "<" << (int)chpoint[i] << ">";
}```
If you have previously set up cout to print hex values, you see the individual bytes in hex

Regards,

Dave

5. ## You'll probably have to write your own algorithm.

I'm about 95% sure that hk_mp5kpdw is right. The built-in hex conversion won't work with floats. In that case, you'll have to write your own algorithm.

Instead of a tenths-place, you have a sixteenths place, and you have a 256th place, etc :

So, 0.5 decimal is 0.8 hex (8 16ths = 8/16)

With a little trial & error and playing around with a calculator, I get 0.1 Decimal = 0.199 hex (approx).
That's 1*(1/16) + 9*(1/256) + 9*(1/4096)

It's probably a repeating "hexadecimal". That might be one point of the exercise... to show that some "nice" decimal numbers can't be accurately stored in binary (or hex).

Note that 0.199 hex does NOT approximate 0.200 hex... 0.1FF is close to 0.200 hex. Also, if it turns-out to be a repeating number: 0.19999 hex rounds to 0.199A.

6. Hmmm now i'm wondering what you need to do. I initially thought you needed something more like what hk_mp5kpdw was suggesting. Now I wonder if you need what Dave Evans was teaching you how to do.

In either case, if you are using the hk_mp5kpdw method, as I have dubbed it, you would NOT want to use an int as that would defeat the purpose of outputting a float as a hex number. Instead use your code but typecast the float to an int.

Code:
```#include <iostream>
#include <iomanip>

int main()

{
float number;
number = 0.5;
std::cout.setf(std::ios::hex);
std::cout << "what about the hex function?: " << (int)number << "\n";

return (0);
}```

7. Thank you all for your feedback thus far....this is the most help I've gotten on a C++ forum. You guys rock.

You guys brought up questions about 8-bit/16-bit and unsigned/signed thingers...I don't fully understand it, so lookee here....
THis is what the problem was:

"1.write C++ code to display the hexadecimal value stored in memory for each integer x, y and z below,
int x = 97,
y = -97,
z = -1;

2.write C++ code to display the hexadecimal value stored in memory for the floats q and t below,
float q = 0.1,
t = 0.5;

3.write C++ code to display the range of the following integers,
16 bit, signed and unsigned
32 bit, signed and unsigned. "

I got thru part "1" just fine...now I'm at 2. Also, he gave a link for help..

Anyways, thank you guys VERY MUCH for your help. I will try your suggestions as soon as I can get to a computer with a compiler installed...

8. could you tell me what 0.1 looks like in hex?

0x0000.1 ?!?

9. Originally Posted by master5001
Hmmm now i'm wondering what you need to do. I initially thought you needed something more like what hk_mp5kpdw was suggesting. Now I wonder if you need what Dave Evans was teaching you how to do.

In either case, if you are using the hk_mp5kpdw method, as I have dubbed it, you would NOT want to use an int as that would defeat the purpose of outputting a float as a hex number. Instead use your code but typecast the float to an int.

Code:
```#include <iostream>
#include <iomanip>

int main()

{
float number;
number = 0.5;
std::cout.setf(std::ios::hex);
std::cout << "what about the hex function?: " << (int)number << "\n";

return (0);
}```

But when you typecast your float to an int, the integer value is used in expressions (that is the float is converted to an int for cout).

Why not try it. I used your example and tacked byte-blasting code after it.

Code:
```#include <iostream>
#include <iomanip>

int main()

{
float number;
number = 1.5;
unsigned char *chpoint;
chpoint = (unsigned char *)&number;
std::cout.setf(std::ios::hex);
std::cout << "what about the hex function?: " << (int)number << "\n";

std::cout << "Here are the bytes:" << std::endl;
std::cout << " " << std::hex;
for (int i = 0; i < sizeof(number); i++) {
std::cout << "<" << (int)chpoint[i] << ">";
}
std::cout << std::endl;

return (0);
}```

One other point: if the assignment was to find out what's stored in memory, it will be different for big-endian machines (like PowerPC-based Macintoshes and some Unix boxes) and little-endian machines (with Intel/AMD. etc. PCs).

Try the byte-blasting code that I submitted with integers.

Let x = 0x12345678 and print the hex value as an int (you will get 0x12345678). This shows how the number is represented in a 32-bit register in the CPU.

Now use byte-blasting to see the following (on little-endian machines)

<78><56><34><12>

The bytes are stored in reverse order in memory.

Regards,

Dave

10. Code:
`	cout<<hex<<*(int*)&theFloat;`
check this site out for a more detailed explanation

http://www.duke.edu/~twf/cps104/floating.html#hex2dec

11. Wow...I'm so lost right now...

I looked at your examples, and they did stuff that I'm sure will help me, once I figure out how to apply them...

I went to my instructors hint page http://www.cvcc.edu/department/facst...ian/endian.cpp

...and I'm even more lost. I don't recognize half of the commands he used. <sigh> oh well.

This is unrelated, but is there a place where I can actually chat with guys who know what they are doing (like you) online, live, in real time, for no charge?

Thank you graciously for your help.

12. Dave Evans, I know man. If you read my post I clearly states that his initial explanation was ambiguous. He never said what he wanted displayed. When someone says "I need to show a float in hex" they generally mean what I suggested. When they say "I need to represent the bytes of a float in hex" they generally mean what you suggested.

13. Originally Posted by master5001
Dave Evans, I know man. If you read my post I clearly states that his initial explanation was ambiguous. He never said what he wanted displayed. When someone says "I need to show a float in hex" they generally mean what I suggested. When they say "I need to represent the bytes of a float in hex" they generally mean what you suggested.
I didn't mean to imply that I think my way is "right" and other ways are "wrong". I just meant to comment in passing that what's in memory is not necessarily what gets put into a register in the CPU (depending on byte-ordering). Lots of people have never heard of big-endian, little-endian. Lots of people don't care. They might care some day if, say, they write numeric data into a binary file a byte at a time in the order in which they appear in memory, then read them back into an other-endian machine a byte at a time.

Also the byte-banging routine can be used for data items of any size (structs, arrays, etc.) to show what's in memory. Several previous posts in this thread specifically and explicitly depend on floats and integers having the same size. Not always true.

Regards,

Dave

14. Some guy on another forum suggested this:

#include <stdio.h>

int main()
{

// PART 1
int x = 97, y = -97, z = -1;
printf("PART 1\n-----------\n");
printf("\tDECIMAL\tHEX VALUES\n--------------------------\n");
printf("x:\t%d\t%08XH\ny:\t%d\t%08XH\nz:\t%d\t%08X H\n\n\n", x, x, y, y, z, z);

// PART 2
float q = 0.1f, t = 0.5f;
printf("PART 2\n-----------\n");
printf("\tFLOAT\t\tHEX VALUES\n----------------------------------\n");
printf("q:\t%f\t%08XH\n", q, q);
printf("t:\t%f\t%08XH\n\n\n", t, t);

// PART 3
printf("PART 3\n-----------\n");
printf(" TYPE\t\tNEGATIVE\tPOSITIVE\n----------------------------------------\n");

unsigned short ushort_low = 0, ushort_high = -1; // 16 bit short
printf("unsigned short\t%u\t\t%u\n", ushort_low, ushort_high);
signed short short_high = 0, short_low;
while (--short_high < 0) 1;
short_low = short_high + 1;
printf("signed short\t%d\t\t%d\n", short_low, short_high);

unsigned int uint_low = 0, uint_high = -1; // 32 bit int
printf("unsigned int\t%u\t\t%u\n", uint_low, uint_high);
signed int int_high = 1;
while (int_high > 0) int_high *= short_high;
while (--int_high < 0);
printf("signed int\t%d\t\t%d\n", int_high+1, int_high);

getc(stdin); // HALT DEBUGGER
return 0;
}

/* Then he had this stuff at the bottom*/

edit:

bug in standard C printf on both ms and gcc

#include <stdio.h>

int main()
{
printf("q:\t%f\t%08XH\nt:\t%f\t%08XH\n\n\n", 0.1f, 0.1f, 0.5f, 0.5f);
getc(stdin);
return 0;

}

yields

q: 0.100000 A0000000H
t: 0.000000 3FE00000H

If you do it the original way, the value for t comes out 0000000H or something like that. If you do it the EDIT way (he edited his post and corrected), then it gives you the 3FE00000H answer. Bug? Sounds mean. My question is, what is that stuff? I'm not familiar with printf. I went back and pasted little tidbits of it to get a rough understanding...

printf("q:\t%f\t%08XH\nt:\t%f\t%08XH\n\n\n", 0.1f, 0.1f, 0.5f, 0.5f);

From what I see, the printf(); is what you use to print the "f".(duh). Now, let's look at the % thingies..

According to about.com, %f does floats or doubles. I see this.

So, what does \t mean?

What does %08XH mean?

Also, why is there a bug that makes it so you can't use stored integers, q and t? If you see, he used them the first time, but the output wasn't correct. He had to put 0.1f, 0.1f, 0.5f, 0.5f after the function....I think that I was supposed to print the value for a stored float, but it looks like this bug won't let me?

And finally, what is
getc(stdin); ?

You guys have been IMMENSELY helpful, and I am undyingly grateful. Thank you for putting up with my dumb questions. (I have so many dumb questions because my C++ class is, like most other college classes, a class where you do everything your self. Teach yourself from the book, do the quizes/hW by yourself during classtime so the teacher don't have to do anything, etc.) You guys are helping me LEARN. And to learn is a great thing....

Me gusta los forumos del "Cboard"

15. Originally Posted by phosphorousgobu
Some guy on another forum suggested this:

oopsie?

edit:

bug in standard C printf on both ms and gcc

#include <stdio.h>

int main()
{
printf("q:\t%f\t%08XH\nt:\t%f\t%08XH\n\n\n", 0.1f, 0.1f, 0.5f, 0.5f);
getc(stdin);
return 0;

}

yields

q: 0.100000 A0000000H
t: 0.000000 3FE00000H

This look right?
And what bug in printf would that be?

If you pass a double to printf, but the format specifier is an int, what do you expect? Printf doesn't convert anything; it doesn't even know what type of variables you actually used in the calling statement, it just gets the values from the argument stack and prints them out according to the format specifier.

Does it "look right"? That's a value judgment that I decline to make.

Does it give me any useful information? Not me (but that's just me).

Can I expect this program to give me what I expect? (A self-referential expression, hard to give a straight answer.)

Regards,

Dave