Thread: How do you concatenate a char to a string

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    How do you concatenate a char to a string

    i am trying to concatenate an unsigned char to a string but its not working
    Code:
    int main(){
    
    string s= "ABCDE";
    unsigned char a='vv;
    s=s+a;
    
    
    }
    Last edited by sigur47; 02-19-2012 at 09:53 AM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    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
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Serapth View Post
    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
    }
    it works fine if its a char but once i use an unsigned char ,it does not work

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    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));
    i am using it for characters that are in the range 0-255 thats why i am using unsigned char.I wont know the range because the characters are being fetched from a file

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, then instead of using a std::string, use a std::vector<unsigned char>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Ah, then instead of using a std::string, use a std::vector<unsigned char>.
    Thanks

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Or use wstring.

    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
    }
    Although frankly, wstring is always such a pain in the .... to work with.

    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 10:33 AM. Reason: Removed C style cast... bad habit.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Serapth
    Or use wstring.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Can't basic_string<unsigned char> be used ?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elkvis
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 ?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Aren't user defined literals about adding custom suffixes to data ?
    C++11 - Wikipedia, the free encyclopedia
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenate string and int
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 01-28-2010, 04:43 AM
  2. concatenate 2 const char * problem
    By tempster09 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 09:23 PM
  3. Concatenate a String?
    By sharkbate24 in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2009, 05:16 PM
  4. concatenate int onto char
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 08-06-2008, 09:54 AM
  5. convert int to char and concatenate
    By robasc in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 06:08 AM