Convert `const unsigned char []' to `std::string'

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}; ...

  1. #1
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71

    Convert `const unsigned char []' to `std::string'

    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

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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 cboard_member; 07-18-2005 at 05:22 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    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

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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

  5. #5
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Error:
    Code:
    :!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
    // Edited formatting

  6. #6
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    stringstream?

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Here's a sample function using a stringstream:

    Code:
    #include <sstream>
     
    int itos (int i)
    {
     stringstream s; 
     s << i;
     return s.str ();
    }
    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.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    const unsigned char cbuffer[]={0x61,0x62,0x63,0x0};
    std::string sbuffer=reinterpret_cast<const char*>(cbuffer);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    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;
    
        }
    Hope this helps.

    @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.

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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

  11. #11
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Quote Originally Posted by hk_mp5kpdw
    Code:
    const unsigned char cbuffer[]={0x61,0x62,0x63,0x0};
    std::string sbuffer=reinterpret_cast<const char*>(cbuffer);
    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.

  12. #12
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Quote Originally Posted by dra
    stringstream?
    Since cbuffer is a byte array (i.e. it'll containt null characters), stringstream cannot be used, as a null character terminates the stream.

  13. #13
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Thanks for your input, all. It is a tricky one (at least for me).

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Why do you want to use a std::string?
    What's wrong with a vector<unsigned char> or basic_string<unsigned char>?

    gg

  15. #15
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    I'll look into those. Thanks.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 12:59 PM
  2. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21