I am writing a code which involves converting a decimal number to binary and store the binary number. I'm not able to store the leading zeros in some of the binary number e.g 001101011 and instead it prints and stores -> 1101011. Any help would be appreciated. thanks
With my mind reading powers I'm deducing that this will help you.
printf("%08x", number);
To the best of my knowledge there is no standard data type for binary numbers in c++. So i guess you are using integers to store the binary number. So to print the leading zeroes just use this .
std::cout << std::setw(5) << std::setfill('0') << binary_number << std::endl;
See http://www.daniweb.com/software-development/cpp/threads/114864/setw-and-setfill.
Related
i was trying to convert from a char array to integers and the atoi function is working properly except when i put a zero in the first index...it didn't print it
#include<iostream>
using namespace std;
int main()
{
char arr[]= "0150234";
int num;
num=atoi(arr);
cout << num;
return 0;
}
I expect the output of 0150234 but the actual output is 150234
I think inside the atoi function you have typecasted the string to integer because of which the 0 gets removed. You can never get a 0 printed before a number since it doesn't make sense.
000001 will always be represented as 1.
I hope this clears your doubt.
Binary number representations (such as int) do not store leading 0s because there is an infinite number of them. Rather they store a fixed number of bits which may have some leading 0 bits.
You can still print the leading 0s if necessary:
std::cout << std::setw(4) << std::setfill('0') << 1 << '\n';
Output:
0001
You're confusing two ideas:
Numbers: These are abstract things. They're quantities. Your computer stores the number in a manner that you should not care about (though it's probably binary).
Representations: These are ways we display numbers to humans, like "150234", or "0x24ADA", or "one hundred and fifty thousand, two hundred and thirty four". You pick a representation when you convert to a string. When streaming to std::cout a representation is picked for you by default, but you can choose your own representation using I/O manipulators, as Maxim shows.
The variable num is a number, not a representation of a number. It does not contain the information «display this as "0150234"». That's what arr provides, because it is a string, containing a representation of a number. So, if that leading zero in the original representation is important to you, when you print num, you have to reproduce that representation yourself.
By the way…
Usually, in the programming world, and particularly in C-like source code:
When we see a string like "150234" we assume that it is the decimal (base-10) representation of a number;
When we see a string like "0x24ADA" (with a leading 0x) we assume that it is the hexadecimal (base-16) representation of a number;
When we see a string like "0150234" (with a leading 0) we assume that it is the octal (base-8) representation of a number.
So, if you do add a leading zero, you may confuse your users.
FYI the conventional base-8 representation of your number is "0445332".
Is there any elegant solution using the std C++ or Boost libraries to output a double to std::cout in a way that the following conditions are met:
scientific notation is disabled
the precision for the decimal part is 6
however, trailing 0's (for the decimal part) are not printed out
For example:
double d = 200000779998;
std::cout << `[something]` << d;
should print out exactly 200000779998. [something] should possibly be a noexcept combination of some existing manipulators.
This is not a solution to the problem:
std::cout << std::setprecision(6) << std::fixed << d;
because it prints out 200000779998.000000 with trailing 0's
Instead of using the fixed manipulator, you can try to use (abuse?) defaultfloat. As far as I understand, it chooses either fixed or scientific based on the ability to put the number within the specified precision. As a result you can set the precision to the number of digits of the integral part + the requested fractional precision (6 in your case).
double d = 200000779998;
std::cout << std::setprecision(integralDigits(d) + 6) << d << std::endl;
You can try it here.
Hard to prove a negative, but I would assume no.
The requirements are inconsistent with any normal use. Space efficiency dictates a binary format. 6 digits (decimal) of precision suggests a format intended for human readers, who can't churn through lots of data. And humans have no issue dealing with a consistent 6 digit format.
So, you're basically targeting a format that has no obvious audience, and that is why I would be surprised if there is support for that.
I have been searching and experimenting for many, many, hours now, and so far I have not been able to adapt any of the solutions I have come across to do what I want.
My goal is to take an integer (538214658) and convert it into an 8 character hex string (020148102). Then I want to drop the first two characters (0148102) and convert it back into an integer(1343746) which I am using as a key in a map array.
The solutions I've seen so far just convert an integer into hex string, but don't take into account the desired digit length.
I am able to print out just the first 6 characters using the following code:
Console_Print("%06X", form ? form->refID : 0)
So I thought that maybe I could use that technique to store it into a string, and then use iostream or sstream to convert it back to an integer, but none of my searches turned up anything I could use. And all of my experiments have failed.
Some help would be greatly appreciated.
EDIT: Below is my solution based on Klaus' suggestion:
uint32_t GetCoreRefID(TESForm* form)
{
uint32_t iCoreRefID = 0;
if (form)
{
uint32_t iRefID = (uint32_t)(form->refID);
iCoreRefID = iRefID & 0x00ffffff;
}
return iCoreRefID;
}
There is no need to convert to a string representation.
Look the following example:
int main()
{
uint32_t val = 538214658 & 0x00ffffff;
std::cout << std::hex << val << std::endl;
std::cout << std::dec << val << std::endl;
}
You have to learn that a value is still only a value and is not dependent on the representation like decimal or hex. The value stored in a memory area or a register is still the same.
As you can see in the given example I wrote your decimal value representation and remove the first two hexadecimal digits simply by do a bitwise and operation with the hexadecimal representation of a mask.
Furthermore you have to understand that the printing with cout in two different "modes" did not change the value at all and also not the internal representation. With std::dec and std::hex you tell the ostream object how to create a string from an int representation.
I am trying to set the two decimal numbers for double type data entered by the user, and I have the proper header file , but the result on the display is only integer, no decimal ?
I do really appreciate any help.
You would want to use the following format.
cout << setprecision(# of places past decimal) << fixed << varName << endl;
The fixed Input output manipulator is what tells it that you are setting the precision for the number of places after the decimal point.
inpfile>>ch;
if(ch<16) outfile<<"0×0"<<std::hex<<setprecision(2)<<(int)ch<<" ";
what does std::hex<<setprecision(2) mean?
iostreams can be manipulated to achieve the desired formatting - this is done by what at first sight looks like outputting predefined values to them as shown in our subject line of code.
std::hex displays following integer values in base16.
setprecision sets the precision for display of following floating values.
Fur further info on manipulators, start here
This line is the same as:
char ch;
inpfile>>ch;
if(ch<16)
{
outfile << "0×0" // Prints "0x0" (Ox is the standard prefix for hex numbers)
/*outfile*/ << std::hex // Tells the stream to print the next number in hex format
/*outfile*/ << setprecision(2) // Does nothing. Presumably they were trying to indicate print min of 2 characters
/*outfile*/ << (int)ch // Covert you char to an integer (so the stream will print it in hex
/*outfile*/ << " "; // Add a space for good measure.
}
Rather than setprecision(2) what was probably intended was setw(2) << setfill('0')
what does std::hex<<setprecision(2) mean?
std::hex and std::setprecision are both so-called manipulators. Applied to a stream (done by outputting them) they manipulate the stream, usually to change the stream's formatting. In particular, std::hex manipulates the stream so that values are written in hexadecimal, and std::setprecision(x) manipulates it to output numbers with x digits.
(A rather popular manipulator which you might already know about is std::endl.)
As you can see, there are manipulators that take arguments and those that take none. Also, most (in principle all) manipulators are sticky, which means their manipulation of the stream lasts until it is explicitly changed. Here is an extensive discussion about this topic.
std::hex sets the output base to hexadecimal.
setprecision has no effect on this line since it affects floating point only.