How to convert a byte array to a hexadecimal string in visual C ++?

Method Declaration:

//some.h void TDES_Decryption(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length); 

I call this method from the following code:

 //some.c extern "C" __declspec(dllexport) bool _cdecl OnDecryption(LPCTSTR stringKSN, LPCTSTR BDK){ TDES_Decryption(m_Track1Buffer, m_cryptoKey, init_vector, len); return m_Track1Buffer; } bool _cdecl OnDecryption (LPCTSTR stringKSN, LPCTSTR BDK) { //some.c extern "C" __declspec(dllexport) bool _cdecl OnDecryption(LPCTSTR stringKSN, LPCTSTR BDK){ TDES_Decryption(m_Track1Buffer, m_cryptoKey, init_vector, len); return m_Track1Buffer; } len); //some.c extern "C" __declspec(dllexport) bool _cdecl OnDecryption(LPCTSTR stringKSN, LPCTSTR BDK){ TDES_Decryption(m_Track1Buffer, m_cryptoKey, init_vector, len); return m_Track1Buffer; } 

Where the data type m_Track1Buffer is BYTE m_Track1Buffer[1000]; Now I want to make some changes in the method described above, that is, want to return String in hex instead of Byte . How do I convert this m_Track1Buffer in Hex string

+9
source share
5 answers

As you mentioned c ++, here is the answer. Iomanip used to store integers in hexadecimal form in stringstream.

 #include <sstream> #include <iomanip> std::string hexStr(BYTE *data, int len) { std::stringstream ss; ss << std::hex; for( int i(0) ; i < len; ++i ) ss << std::setw(2) << std::setfill('0') << (int)data[i]; return ss.str(); } , int len) #include <sstream> #include <iomanip> std::string hexStr(BYTE *data, int len) { std::stringstream ss; ss << std::hex; for( int i(0) ; i < len; ++i ) ss << std::setw(2) << std::setfill('0') << (int)data[i]; return ss.str(); } ) #include <sstream> #include <iomanip> std::string hexStr(BYTE *data, int len) { std::stringstream ss; ss << std::hex; for( int i(0) ; i < len; ++i ) ss << std::setw(2) << std::setfill('0') << (int)data[i]; return ss.str(); } 
+14
source

This code converts a fixed-size array of 100 bytes in the sixth row:

 BYTE array[100]; char hexstr[201]; int i; for (i=0; i<ARRAY_SIZE(array); i++) { sprintf(hexstr+i*2, "%02x", array[i]); } hexstr[i*2] = 0; i ++) { BYTE array[100]; char hexstr[201]; int i; for (i=0; i<ARRAY_SIZE(array); i++) { sprintf(hexstr+i*2, "%02x", array[i]); } hexstr[i*2] = 0; 
+10
source

Here is a slightly more flexible version (use uppercase characters? Insert spaces between bytes?) That can be used with regular arrays and various standard containers:

 #include <string> #include <sstream> #include <iomanip> template<typename TInputIter> std::string make_hex_string(TInputIter first, TInputIter last, bool use_uppercase = true, bool insert_spaces = false) { std::ostringstream ss; ss << std::hex << std::setfill('0'); if (use_uppercase) ss << std::uppercase; while (first != last) { ss << std::setw(2) << static_cast<int>(*first++); if (insert_spaces && first != last) ss << " "; } return ss.str(); } TInputIter last, bool use_uppercase = true, bool insert_spaces = false) #include <string> #include <sstream> #include <iomanip> template<typename TInputIter> std::string make_hex_string(TInputIter first, TInputIter last, bool use_uppercase = true, bool insert_spaces = false) { std::ostringstream ss; ss << std::hex << std::setfill('0'); if (use_uppercase) ss << std::uppercase; while (first != last) { ss << std::setw(2) << static_cast<int>(*first++); if (insert_spaces && first != last) ss << " "; } return ss.str(); } first ++); #include <string> #include <sstream> #include <iomanip> template<typename TInputIter> std::string make_hex_string(TInputIter first, TInputIter last, bool use_uppercase = true, bool insert_spaces = false) { std::ostringstream ss; ss << std::hex << std::setfill('0'); if (use_uppercase) ss << std::uppercase; while (first != last) { ss << std::setw(2) << static_cast<int>(*first++); if (insert_spaces && first != last) ss << " "; } return ss.str(); } 

Usage example (simple array):

 uint8_t byte_array[] = { 0xDE, 0xAD, 0xC0, 0xDE, 0x00, 0xFF }; auto from_array = make_hex_string(std::begin(byte_array), std::end(byte_array), true, true); assert(from_array == "DE AD C0 DE 00 FF"); 0xAD, 0xC0, 0xDE, 0x00, 0xFF}; uint8_t byte_array[] = { 0xDE, 0xAD, 0xC0, 0xDE, 0x00, 0xFF }; auto from_array = make_hex_string(std::begin(byte_array), std::end(byte_array), true, true); assert(from_array == "DE AD C0 DE 00 FF"); (byte_array), std :: end (byte_array), true, true); uint8_t byte_array[] = { 0xDE, 0xAD, 0xC0, 0xDE, 0x00, 0xFF }; auto from_array = make_hex_string(std::begin(byte_array), std::end(byte_array), true, true); assert(from_array == "DE AD C0 DE 00 FF"); 

Example of use ( std::vector ):

 // fill with values from the array above std::vector<uint8_t> byte_vector(std::begin(byte_array), std::end(byte_array)); auto from_vector = make_hex_string(byte_vector.begin(), byte_vector.end(), false); assert(from_vector == "deadc0de00ff"); , byte_vector.end (), false); // fill with values from the array above std::vector<uint8_t> byte_vector(std::begin(byte_array), std::end(byte_array)); auto from_vector = make_hex_string(byte_vector.begin(), byte_vector.end(), false); assert(from_vector == "deadc0de00ff"); 
+7
source

how about using boost library like this (excerpt taken from http://theboostcpplibraries.com/boost.algorithm ):

 #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } 'C', '+', '+'}; #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } char> {std :: cout, ""}); #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } "; #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } << '\ n'; #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } ' #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } << '\ n'; #include <boost/algorithm/hex.hpp> #include <vector> #include <string> #include <iterator> #include <iostream> using namespace boost::algorithm; int main() { std::vector<char> v{'C', '+', '+'}; hex(v, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string s = "C++"; std::cout << hex(s) << '\n'; std::vector<char> w{'4', '3', '2', 'b', '2', 'b'}; unhex(w, std::ostream_iterator<char>{std::cout, ""}); std::cout << '\n'; std::string t = "432b2b"; std::cout << unhex(t) << '\n'; } 
0
source

Using stringstream , sprintf and other functions in the cycle not just C ++. It's terrible for performance, and such functions are usually often cause (unless you write some things in the magazine).

Here is one way to do it. Record directly into the buffer std::string is not recommended, because the concrete implementation of std :: string can behave in a different way, and then it does not work, but we thus avoid a copy of the entire buffer:

 #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } vector <uint8_t> & input) #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } 0123456789ABCDEF"; #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } not be avoided for std :: string. #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } * #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } copying the whole buffer. #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } ) #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } 0x0F]; #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } { #include <iostream> #include <string> #include <vector> std::string bytes_to_hex_string(const std::vector<uint8_t> &input) { static const char characters[] = "0123456789ABCDEF"; // Zeroes out the buffer unnecessarily, can't be avoided for std::string. std::string ret(input.size() * 2, 0); // Hack... Against the rules but avoids copying the whole buffer. char *buf = const_cast<char *>(ret.data()); for (const auto &oneInputByte : input) { *buf++ = characters[oneInputByte >> 4]; *buf++ = characters[oneInputByte & 0x0F]; } return ret; } int main() { std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 }; std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl; } 
0
source

All Articles