This question already has answers here:
uint8_t can't be printed with cout
(8 answers)
Closed 3 years ago.
I am trying to convert a string to a vector of ASCII value uint8_t
For example, if I have a string that is "500" I would want a vector that is {53, 48, 48} where these values are hex 0x35, 0x30, 0x30. This is what I am currently doing and it is not working
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string number = "500";
std::vector<std::uint8_t> bytes;
for (auto& c : number)
bytes.push_back(static_cast<std::uint8_t>(c));
for (auto& c : bytes)
std::cout << c << std::endl;
return 0;
}
But I just get 5, 0, 0 as output where I was expecting 53, 48, 48
std::cout << c << std::endl; is writing std::uint8_t as a character (see this question for details).
You have to cast it to an integer to actually get the result you want.
For example (wandbox link):
std::cout << static_cast<int>(c) << std::endl;
Related
This question already has answers here:
uint8_t can't be printed with cout
(8 answers)
Closed 4 years ago.
Why does the following code fail for the case of uint8_t?
#include <iostream>
#include <cstdint>
#include <stack>
template <typename TT>
void PrintNumberScientificNotation (TT number) {
constexpr TT kBase{10}; // Base of the numerical system.
TT xx{number}; // Number to convert.
TT exponent{}; // Exponent.
std::stack<TT> fractional_part{}; // Values in the fractional part.
do {
fractional_part.push(xx%kBase);
xx /= kBase;
exponent++;
} while (xx > kBase);
std::cout << xx << '.';
while (!fractional_part.empty()) {
std::cout << fractional_part.top();
fractional_part.pop();
}
std::cout << " x 10^" << exponent << std::endl;
}
int main () {
uint8_t number_1{255};
PrintNumberScientificNotation(number_1); // Does not work.
uint16_t number_2{255};
PrintNumberScientificNotation(number_2); // Works.
uint16_t number_3{65'535};
PrintNumberScientificNotation(number_3); // Works.
uint32_t number_4{4'294'967'295};
PrintNumberScientificNotation(number_4); // Works.
uint64_t number_5{18'446'744'073'709'551'615};
PrintNumberScientificNotation(number_5); // Works.
}
Execute: http://cpp.sh/8c72o
Output:
. x 10^
2.55 x 10^2
6.5535 x 10^4
4.294967295 x 10^9
1.8446744073709551615 x 10^19
It is my understanding that uint8_t is able to represent unsigned integer numbers up to and including 255 (UINT8_MAX). Why can I represent the maximum values for all of the other representations?
The mathematical part of your code is fine, the printing is broken.
If you use cout for uint_t, it will interpret the uint_t as a character code. That's because uint_t is a type alias for unsigned char.
A possible fix is to explicitly convert to integer:
std::cout << unsigned(xx) << '.';
while (!fractional_part.empty()) {
std::cout << unsigned(fractional_part.top());
fractional_part.pop();
}
std::cout << " x 10^" << unsigned(exponent) << std::endl;
uint8_t is treated like a char thus it will print the character with the ASCII code. You have to convert the uint8_t value to unsigned before printing it as follow:
uint8_t number_1{255};
PrintNumberScientificNotation(unsigned(number_1));
This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.
This question already has answers here:
C++ cout hex values?
(10 answers)
Closed 5 years ago.
How can you convert a decimal to hexadecimal in C++? I've had success with ultoa but that gives you a char (and using (DWORD)Buffer to std::cout just ends in gibberish). Also i need to save the new hexadecimal in a DWORD again.
My Code so far:
//Vars
char Buffer[33];
// Client.dll
DWORD d_clientDll = (DWORD)GetModuleHandleA("client.dll");
_ultoa(d_clientDll, Buffer, 16);
std::cout << Buffer << std::endl;
std::cout << d_clientDll << std::endl;
Did you try std::hex ?
std::cout << std::hex << d_clientDll << std::endl
This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
Here:
#include <iostream>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
int main(void) {
const char * str = "277499.84";
std::cout << boost::lexical_cast<double>(str) << std::endl;
std::cout << strtof(str, NULL) << std::endl;
std::cout << strtold(str, NULL) << std::endl;
std::cout << atof(str) << std::endl;
return 0;
}
output:
277500
277500
277500
277500
Why the output are not 277499.84?
It's not the operations themselves losing accuracy, but the output.
You can use the I/O manipulator std::setprecision to control the numeric precision. The following will use the full precision of a double (assuming the stream is set for decimal output).
double value = boost::lexical_cast<double>(str);
std::cout << std::setprecision( std::numeric_limits<double>::digits10 + 1 ) << value;
Or you can use std::ios_base::precision. This is useful if you want to restore the precision to the original value after.
auto old_precision = cout.precision( std::numeric_limits<double>::digits10 + 1 );
cout << value;
cout.precision( old_precision );
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Print leading zeros with C++ output operator (printf equivalent)?
#include <iostream>
#include <iomanip>
int main()
{
int n = 16;
std::cout << ???(5) << n << std::endl;
}
I want the output to be 00016
setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?
My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned short n = 0xA7;
std::cout << std::hex << ???(4) << n << std::endl;
}
and I am expecting to get this output: 00A7
You also need setfill('0').