Getting a buffer into a stringstream in hex representation:
#include <sstream>
#include <iomanip>
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (int i = 0; i < 32; ++i)
{
ss << std::setw(2) << static_cast<unsigned>(buffer[i]);
}
Inputting data to stringstream with hexadecimal representation
First, the std::hex
format modifier applies to integers, not to characters. Since you are trying to print unsigned char
, the format modifier is not applied. You can fix this by casting to int
instead. In your first example, it works because the literal 100
is interpreted as an integer. If you replace 100
with e.g. static_cast<unsigned char>(100)
, you would no longer get the hexadecimal representation.
Second, std::hex
is not enough, since you likely want to pad each character to a 2-digit hex value (i.e. F
should be printed as 0F
). You can fix this by also applying the format modifiers std::setfill('0')
and std::setw(2)
(reference, reference).
Applying these modifications, your code would then look like this:
#include <iomanip>
...
buffer << std::hex << std::setfill('0') << std::setw(2);
for (size_t i = 0; i < SHA256::DIGESTSIZE; i++) {
buffer << static_cast<int>(*(digest+i));
}
How to save hex representation of binary file into a std::string?
You don't have a std::string
; you have an std::stringstream
. And you can't "print" a stringstream, but you can obtain the std::string
representation of its buffer, using the str()
member function.
You probably meant, then:
std::cout << buffer.str();
There are cleaner ways to do this, but the above will get you started.
As an aside, your loop is wrong. You're checking for EOF too soon.
Wrong values in raw data to hex string convertion?
OK Guys, sorry for the delay, I ran into the weekend and had some FIFA World Cup games to watch.
I was getting the same result no matter the change I made, so I decided to make some changes in the code before and I switched the input param from
std::string ByteUtil::rawByteStringToHexString(std::string str)
to
std::string ByteUtil::rawByteStringToHexString(vector<unsigned char> v)
and also kept the changes suggested by @sharth. Now I am getting the correct result.
Thank you all for the help!!
Integer to hex string in C++
Use <iomanip>
's std::hex
. If you print, just send it to std::cout
, if not, then use std::stringstream
std::stringstream stream;
stream << std::hex << your_int;
std::string result( stream.str() );
You can prepend the first <<
with << "0x"
or whatever you like if you wish.
Other manips of interest are std::oct
(octal) and std::dec
(back to decimal).
One problem you may encounter is the fact that this produces the exact amount of digits needed to represent it. You may use setfill
and setw
this to circumvent the problem:
stream << std::setfill ('0') << std::setw(sizeof(your_type)*2)
<< std::hex << your_int;
So finally, I'd suggest such a function:
template< typename T >
std::string int_to_hex( T i )
{
std::stringstream stream;
stream << "0x"
<< std::setfill ('0') << std::setw(sizeof(T)*2)
<< std::hex << i;
return stream.str();
}
c++ stringstream read doesn't seem to read the whole buffer
stringstream
is not eligible for parsing the character representation into a value in byte.
You may use something lik strtol
to actually parse the string into value.
#include<string>
#include<sstream>
#include<iomanip>
#include<iostream>
int main() {
std::string line = "fa0834dd";
for(int i = 0; i < line.length(); i += 2) {
std::string ss = line.substr(i,2);
std::cout << ss << " ";
std::uint8_t byte = static_cast<std::uint8_t>(strtol(ss.c_str(), NULL, 16));
std::cout << std::hex << static_cast(byte) << std::endl;
}
}
Ref post
Converting unsigned char * to hexstring
The output of an unsigned char
is like the output of a char
which obviously does not what the OP expects.
I tested the following on coliru:
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Output of (unsigned char)0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned char)0xc0 << '\n';
return 0;
}
and got:
Output of (unsigned char)0xc0: 0�
This is caused by the std::ostream::operator<<()
which is chosen out of the available operators. I looked on cppreference
- operator<<(std::basic_ostream) and
- std::basic_ostream::operator<<
and found
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
unsigned char ch );
in the former (with a little bit help from M.M).
The OP suggested a fix: bit-wise And with 0xff
which seemed to work. Checking this in coliru.com:
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Output of (unsigned char)0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (0xff & (unsigned char)0xc0) << '\n';
return 0;
}
Output:
Output of (unsigned char)0xc0: c0
Really, this seems to work. Why?
0xff
is an int
constant (stricly speaking: an integer literal) and has type int
. Hence, the bit-wise And promotes (unsigned char)0xc0
to int
as well, yields the result of type int
, and hence, the std::ostream::operator<<
for int
is applied.
This is an option to solve this. I can provide another one – just converting the unsigned char
to unsigned
.
Where the promotion of unsigned char
to int
introduces a possible sign-bit extension (which is undesired in this case), this doesn't happen when unsigned char
is converted to unsigned
. The output stream operator for unsigned
provides the intended output as well:
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Output of (unsigned char)0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned)(unsigned char)0xc0 << '\n';
const unsigned char c = 0xc0;
std::cout << "Output of unsigned char c = 0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned)c << '\n';
return 0;
}
Output:
Output of (unsigned char)0xc0: c0
Output of unsigned char c = 0xc0: c0
Live Demo on coliru
Related Topics
Visual Studio 2013 Error Ms8020 Build Tools V140 Cannot Be Found
C++, Function Pointer to the Template Function Pointer
C++ Bitfield Packing with Bools
Why Does Not a Template Template Parameter Allow 'Typename' After the Parameter List
C++ Bool Returns 0 1 Instead of True False
C++ Template Instantiation: Avoiding Long Switches
Why Does the "Static" Keyword Have So Many Meanings in C and C++
Undefined Reference to Static Variable
How to Generate a Random Number Using the C++11 Standard Library
Why am I Not Provided with a Default Copy Constructor from a Volatile
Convert a Number to a String Literal with Constexpr
Difference Between Global Non-Throwing ::Operator New and Std::Malloc
Cross-Platform Equivalent to Windows Events