What is the best way to convert an `const unsigned char []' to `std::string'?
example:
Code:const unsigned char cbuffer[]={0x61,0x62,0x63,0x0}; std::string sbuffer=cbuffer; // error
This is a discussion on Convert `const unsigned char []' to `std::string' within the C++ Programming forums, part of the General Programming Boards category; What is the best way to convert an `const unsigned char []' to `std::string'? example: Code: const unsigned char cbuffer[]={0x61,0x62,0x63,0x0}; ...
What is the best way to convert an `const unsigned char []' to `std::string'?
example:
Code:const unsigned char cbuffer[]={0x61,0x62,0x63,0x0}; std::string sbuffer=cbuffer; // error
Hmm I tried it myself in the hope I could help you. I tried using const_cast, static_cast and reinterpret_cast (in that order) with no avail.
I'd like to know how to do this too
Here's how I tried:
Code:#include <string> int main() { const unsigned char[] cbuffer = { 0x61, 0x62, 0x63, 0x0 }; std::string sbuffer; sbuffer = const_cast<std::string> (cbuffer); return 0; }
Last edited by ahluka; 07-18-2005 at 04:22 AM.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
the only way i can think out is to assign cbuffer to a char array, and then use the char array to initialize the string
??Code:std::string sbuffer (cbuffer);
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Error:
// Edited formattingCode::!make 2>&1| tee /tmp/v748926/2 g++ -O3 -Wall -pedantic -ansi -c main.cpp main.cpp: In function `int main()': main.cpp:9: error: conversion from `const unsigned char[4]' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested *** Error code 1
Here's a sample function using a stringstream:
It (should) convert an integer to a string, I just typed it off the top of my head, so it probably has a buggy or two.Code:#include <sstream> int itos (int i) { stringstream s; s << i; return s.str (); }![]()
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Code:const unsigned char cbuffer[]={0x61,0x62,0x63,0x0}; std::string sbuffer=reinterpret_cast<const char*>(cbuffer);
I used to be an adventurer like you... then I took an arrow to the knee.
Hope this helps.Code:#include <iostream> #include <string> #include <sstream> using namespace std; int main(){ stringstream s; char array[] = { 'a', 'b', 'c' }; s << array; string d = s.str(); cout << d; }
@ahluka, that looks like it came from Bjarne Stroustrup's FAQ http://www.research.att.com/~bs/bs_f...#int-to-string
lol just kidding.
Yeah I know I was just saving him the trouble of looking... Actually I lied about the typing off the top of my head bit, I copied and pasted![]()
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
It's important to note that the cast must remain unsigned, otherwise it'll overflow the characters. However, having the cast, as above, unsigned results in an error.Originally Posted by hk_mp5kpdw
Since cbuffer is a byte array (i.e. it'll containt null characters), stringstream cannot be used, as a null character terminates the stream.Originally Posted by dra
Thanks for your input, all. It is a tricky one (at least for me).
Why do you want to use a std::string?
What's wrong with a vector<unsigned char> or basic_string<unsigned char>?
gg