I have unsigned char*
, want to convert it to std::string
. Can you please tell me the safest way to do this?

- 124
- 1
- 10

- 2,382
- 6
- 29
- 42
-
3C++ doesn't have a byte type, so you need to tell us what the type actually is. – Yacoby Nov 04 '09 at 12:30
-
2BYTE resolves to "unsigned char" – Unicorn Nov 04 '09 at 12:33
-
4And what about `Byte`? And more importantly, what does you pointer point to? A single `Byte` value or an array of `Byte`? What do you want in your string: a textual representation of the value of the pointer or string that represents the values of the object(s) pointed to? – CB Bailey Nov 04 '09 at 12:58
6 Answers
You just needed to cast the unsigned char
into a char
as the string
class doesn't have a constructor that accepts unsigned char
:
unsigned char* uc;
std::string s( reinterpret_cast< char const* >(uc) ) ;
However, you will need to use the length argument in the constructor if your byte array contains nulls, as if you don't, only part of the array will end up in the string (the array up to the first null)
size_t len;
unsigned char* uc;
std::string s( reinterpret_cast<char const*>(uc), len ) ;
-
BYTE * pbData = "SomeString" size_t length = 10 I have the length before hand, so i used this : I used this method, "std::string(reinterpret_cast
(pbData), length );" It works, but once in while i see crash in memcpy. Tryint to find our why crash happened, though it seems working for me – Unicorn Nov 04 '09 at 12:45 -
7As a portability pedant, I want to point out that this only works on systems with 2's complement arithmetic. On 1's complement or sign-magnitude architectures, it is not safe to reinterpret_cast between `char*` and `unsigned char*` (well, it's safe, but you'll get surprising results if `char` is signed and any of the characters have the top bit set). In this case the questioner is obviously on Windows, so no problem. – Steve Jessop Nov 04 '09 at 13:52
BYTE*
is probably a typedef for unsigned char*
, but I can't say for sure. It would help if you tell us what BYTE
is.
If BYTE* is unsigned char*, you can convert it to an std::string using the std::string range constructor, which will take two generic Iterators.
const BYTE* str1 = reinterpret_cast<const BYTE*> ("Hello World");
int len = strlen(reinterpret_cast<const char*>(str1));
std::string str2(str1, str1 + len);
That being said, are you sure this is a good idea? If BYTE
is unsigned char
it may contain non-ASCII characters, which can include NULLs. This will make strlen
give an incorrect length.

- 52,325
- 13
- 128
- 140
BYTE *str1 = "Hello World";
std::string str2((char *)str1); /* construct on the stack */
Alternatively:
std::string *str3 = new std::string((char *)str1); /* construct on the heap */
cout << &str3;
delete str3;

- 21,335
- 15
- 77
- 102
-
It does not work, I get an error saying "'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'BYTE *' to 'const std::allocator<_Ty> &'" – Unicorn Nov 04 '09 at 12:29
-
1That's because `std::string` doesn't have a constructor that takes an `unsigned char*`, and so the compiler falls back on the templated constructor meant for allocators. You need to either use `std::string(Iterator begin, Iterator end)`, or `std::string(const char*, std::size_t n)` and cast the BYTE* to a char* – Charles Salvia Nov 04 '09 at 12:45
-
4`std::string &str3 = new ...`, didn't you mean `*` instead of `&`? And btw, creating std::string on the heap is usually bad idea and defeats half the purpose of having a string class with value semantics. – sbk Nov 04 '09 at 12:47
-
You're all right. I hadn't run this through the compiler as I should have. You will need the (char *) typecast, since std::string doesn't handle BYTE/unsigned char types. And the reference example was wrong; it should have been a pointer. – spoulson Nov 04 '09 at 13:29
-
This answer has lots of problems: C-style casts, using heap allocation for no reason, etc. – Charles Salvia Aug 03 '14 at 02:53
BYTE is nothing but typedef unsigned char BYTE;
You can easily use any of below constructors
string ( const char * s, size_t n );
string ( const char * s );

- 6,358
- 6
- 37
- 52
Here is the complete code
#include <bits/stdc++.h>
using namespace std;
typedef unsigned char BYTE;
int main() {
//method 1;
std::vector<BYTE> data = {'H','E','L','L','O','1','2','3'};
//string constructor accepts only const char
std::string s((const char*)&(data[0]), data.size());
std::cout << s << std::endl;
//method 2
std::string s2(data.begin(),data.end());
std::cout << s2 << std::endl;
//method 3
std::string s3(reinterpret_cast<char const*>(&data[0]), data.size()) ;
std::cout << s3 << std::endl;
return 0;
}

- 36
- 1
- 4
If has access to CryptoPP
Readable Hex String to unsigned char
std::string& hexed = "C23412341324AB";
uint8_t buffer[64] = {0};
StringSource ssk(hexed, true,
new HexDecoder(new ArraySink(buffer,sizeof(buffer))));
And back
std::string hexed;
uint8_t val[32] = {0};
StringSource ss(val, sizeof(val), true,new HexEncoder(new StringSink(hexed));
// val == buffer

- 321
- 5
- 8