i am trying to concatenate an unsigned char to a string but its not workingCode:int main(){ string s= "ABCDE"; unsigned char a='vv; s=s+a; }
This is a discussion on How do you concatenate a char to a string within the C++ Programming forums, part of the General Programming Boards category; i am trying to concatenate an unsigned char to a string but its not working Code: int main(){ string s= ...
i am trying to concatenate an unsigned char to a string but its not workingCode:int main(){ string s= "ABCDE"; unsigned char a='vv; s=s+a; }
Last edited by sigur47; 02-19-2012 at 08:53 AM.
Code:#include <string> #include <iostream> int main(int argc, char* argv[]) { std::string stringDemo = "Hello"; char c = 'v'; stringDemo += c; std::cout << stringDemo; // prints Hellov stringDemo.push_back(c); std::cout << stringDemo; // prints Hellovv }
Why do you want to use an unsigned char?
Anyway, if you know that the value of the unsigned char will be in the range of char, the solution is to cast:
Code:stringDemo.push_back(static_cast<char>(c));
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Ah, then instead of using a std::string, use a std::vector<unsigned char>.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Or use wstring.
Although frankly, wstring is always such a pain in the .... to work with.Code:#include <string> #include <iostream> int main(int argc, char* argv[]) { std::wstring stringDemo = L"Hello"; unsigned char c = 'v'; stringDemo += c; std::wcout << stringDemo; // prints Hellov }
Using wstring will allow you to do this though:
Code:std::wstring stringDemo = L"Hello"; unsigned char c = static_cast<unsigned char>(199); stringDemo += c; std::wcout << stringDemo; // prints Hello╟
Last edited by Serapth; 02-19-2012 at 09:33 AM. Reason: Removed C style cast... bad habit.
I may be wrong, but in theory, the range of a wchar_t may well be the same as that of a signed char, since they have the same constraints on implementation defined limits. So, I reason if that if you want to store unsigned chars, then store unsigned chars.Originally Posted by Serapth
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Can't basic_string<unsigned char> be used ?
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
I'm not sure that basic_string is portable or standard-compliant. to my knowledge, string and wstring are specified by the standard, but no mention of how they must be implemented. I may be wrong about this.
basic_string is part of the standard library, but then the stuff like string literals won't exist for basic_string<unsigned char> (except maybe now there's some user defined literals thing, but I'm not familiar with that at all).Originally Posted by Elkvis
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
basic_string is standard, but char_traits is not standard for unsigned char. Thus, you probably won't be able to use basic_string with unsigned char.
You can supply your own traits struct/class if you want, though.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
This works, but I couldn't get it working with any sort of streams.
How would I use custom traits ?
Code:basic_string<unsigned char> s; s+=static_cast<unsigned char>(200);
Aren't user defined literals about adding custom suffixes to data ?
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !