The decimal, octal and hexadecimal representation of an integer in C++ - c++

How can I express the value of an integer using decimal, octal or hexadecimal representation?
(I would prefer only iostream usage)

Assuming you just want to see them, for your own reference. Though storing them in a variable is "just a shot away".
#include <iostream>
using namespace std;
int main () {
int n;
n=70;
cout << hex << n << endl;
cout << dec << n << endl;
cout << oct << n << endl;
return 0;
}

By "decimal integer" I hope you mean a string that uses decimal to represent an integer. Integer types, like int, do not have a base. Or if you insist that they must have a base because of their internal representation then the base is always 2. String representations of integers, now those have a base.
std::istringstream iss(std::string("123"));
int i;
if (iss >> i) {
std::cout << "read a decimal integer!\n";
std::cout << "here it is in decimal: " << i << "\n";
std::cout << "here it is in hex: " << std::hex << i << "\n";
std::cout << "here it is in octal: " << std::oct << i << "\n";
}

Related

What is the purpose of the setprecision() used in this C++ program?

This example program was created with the sole purpose of showing what setprecision and setw does. I dont understand the purpose of the third line that says "setprecision(5)". I commented the line out to see the difference but it looks the exact same. Is there no purpose?
cout << "\nSales Figures\n";
cout << "-------------\n";
cout << setprecision(5);
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;
The setprecision() function is part of the "iomanip" library and is used when you need to output a float to a certain number of decimal places. This is good for displaying money amounts and other things that typically are shown with a set number of digits after the decimal point (even if those digits are 0).
Say you have a float called price: If you stored 10.0 in that float, C++ would not know how many decimal points to output when you print into the screen; setprecision(2) would make the output 10.00.
You can find the documentation at this link: https://cplusplus.com/reference/iomanip/setprecision/.
It includes the following code as an example of how setprecision() works.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.1415
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.14159
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.14159
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.141590000
return 0;
}
Note that setprecision() is only applicable to data types with decimal points such as floats and doubles.

Converting string to double keeps rounding to the whole number

I am trying to convert a string decimal number into a double, however when I use the atof() function, my number ends up rounding to the whole number.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
The output is:
135428
I want:
135427.7
cout does that, not atof().
More precisely, operator<<, which inserts the formated data into the std::ostream.
You can use std::setprecision() from the <iomanip> standard library to print the decimals:
cout << setprecision(7) << r << endl;
or
cout << fixed << setprecision(1) << r << endl;
If you want to print the whole 135427.7000:
cout << fixed << setprecision(4) << r << endl;

How do I show the actual values in hex instead of the ASCII symbols

int main()
{
char B[76]={0};
ifstream infile;
infile.open("tworecords.dat", ios::binary);
infile.read(reinterpret_cast<char*>(B), sizeof (B));
cout << "Array B in hex" << endl;
for (int i = 0; i < 76; i++)
{
cout << hex << B[i] << " " << endl;;
}
return 0;
}
right now it reads the data correctly, but prints out the values as ASCII symbols. I would like to output the actual hex values in the file.
example:
01
3D
76
D6
etc.
Cast it to integer:
cout << hex << static_cast<int>(B[i]) << " " << endl;
Or alternatively, if you don't want to cast, just add 0:
cout << hex << B[i]+0 << " " << endl;
However you probably also want to make sure that for values below 16, a leading 0 is printed (e.g. for the newline character 0A, not just A):
cout << setfill('0') << setw(2) << hex << B[i]+0 << " " << endl;
You simply cast the number to an integer:
cout << hex << (int)B[i] << " " << endl;
the <iostream> library (actually, all stream libraries) output ascii values for char types.

How to get std::numeric_limits<char>::min() value?

How to get (correctly and/or readable value of) std::numeric_limits<char>::min()?
cout << std::numeric_limits<char>::min() << endl;
cout << std::numeric_limits<char>::max() << endl;
return
�
// some character that can't be copied here, it looks like a rectangle containing four numbers in it
You just need to convert it to something that when streamed to cout will interpret it as an integer. E.g.
#include <limits>
#include <iostream>
#include <ostream>
int main()
{
int minc = std::numeric_limits<char>::min();
unsigned maxc = std::numeric_limits<char>::max();
std::cout << minc << std::endl;
std::cout << maxc << std::endl;
}
I deliberately use unsigned for std::numeric_limits<char>::max() just in case sizeof(int) == 1 and char is unsigned.
The problem is that the standard streams will output chars as characters and not as integral values. You can force them to do that by casting to an integral type that isn't a character type:
cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;
Just cast it to an int.
cout << (int)std::numeric_limits<char>::min() << endl;
cout << (int)std::numeric_limits<char>::max() << endl;

Truncate C++ string fields generated by ostringstream, iomanip:setw

In C++ I need string representations of integers with leading zeroes, where the representation has 8 digits and no more than 8 digits, truncating digits on the right side if necessary. I thought I could do this using just ostringstream and iomanip.setw(), like this:
int num_1 = 3000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_1;
cout << "field: " << out_target.str() << " vs input: " << num_1 << endl;
The output here is:
field: 00003000 vs input: 3000
Very nice! However if I try a bigger number, setw lets the output grow beyond 8 characters:
int num_2 = 2000000000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_2;
cout << "field: " << out_target.str() << " vs input: " << num_2 << endl;
out_target.str("");
output:
field: 2000000000 vs input: 2000000000
The desired output is "20000000". There's nothing stopping me from using a second operation to take only the first 8 characters, but is field truncation truly missing from iomanip? Would the Boost formatting do what I need in one step?
I can't think of any way to truncate a numeric field like that. Perhaps it has not been implemented because it would change the value.
ostream::write() allows you to truncate a string buffer simply enough, as in this example...
int num_2 = 2000000000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_2;
cout << "field: ";
cout.write(out_target.str().c_str(), 8);
cout << " vs input: " << num_2 << endl;
If you assume that snprintf() will write as many chars at it can (I don't think this is guaranteed),
char buf[9];
snprintf(buf, 10, "%08d", num);
buf[8] = 0;
cout << std::string(buf) << endl;
I am not sure why you want 2 billion to be the same as 20 million. It may make more sense to signal an error on truncation, like this:
if (snprintf(buf, 10, "%08d", num) > 8) {
throw std::exception("oops")
}