There is a function/method in .net for getting the byte values for numbers. However, I am using native/vanilla c++. I want to be able to convert from a user entered float value to the bytes which an external program will accept.
Exact Example:
user enters 1.0
which is 1.0000000E+000
which MUST give the result of 00 00 80 3F
Thanks R0mai! That was exactly what I was looking for. I appreciate the complete solution as well. I tested a few inputs and it does appear to give the correct results. Now I just have to get 0 to output as 00 and convert to uppercase ie 0 0 80 3f to 00 00 80 3F. I also need to turn the 1.0f part into a variable.
Thanks for the reply hanst99 but on my system it gave me the same garbage output as other attempts have that I found on the net. Maybe I'm not using it properly. Should I be able to "cout << buf;" and get the expected result, because that doesn't work for me. Anyways thanks for the reply.
I'm going to keep the answer open for a little longer in case you guys want to make any other responses.
Don't use void main, it's not valid C++. You need to use int main().
Anyway, I don't think you can do that with unions. According to the standard, only one union member can be active at once, and accessing non-active members gives you undefined behavior. I *think* there might be an exception for char arrays like the one here but I don't know.
I've updated my code using some of everyone's suggestions. It should be noted that using my previous code doesn't output 1-9 properly as I had only fixed 0 to output as 00. The new code has been corrected. I added these functions to my program and found out that all of my integers were outputting as hex. So before leaving the function I revert back to decimal now. Included is another function for converting int to bytes as well. Thanks for the help everyone!