CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    71

    Help - how to convert a const char to std::string?

    Hi,

    Could someone please tell me how to convert a const char to std::string?

    I need to to something like the following code (but it doesn't work).

    Code:
    #include <string> 
     
    int main()
    {
    	const unsigned char[] cbuffer = { 0x61, 0x62, 0x63, 0x0 };
    	std::string sbuffer;
     
    	sbuffer = const_cast<std::string> (cbuffer);
    	return 0;
    }
    Thanks a lot!
    BJT

  2. #2
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Help - how to convert a const char to std::string?

    Code:
    const char cbuffer[] = "abc";
    std::string sbuffer = cbuffer;
    or simply:
    Code:
    std::string sbuffer = "abc";
    I really have no idea why you've set the value for cbuffer like this:
    Code:
    cbuffer = { 0x61, 0x62, 0x63, 0x0 };
    and not like this:
    Code:
    const char cbuffer[] = "abc";
    It's the same thing.

    By the way, as you might have noticed from what I wrote, the brackets should be placed after the name of the variable (cbuffer[]), not after the name of the type (char[]).
    Last edited by Feoggou; February 17th, 2011 at 01:25 PM.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help - how to convert a const char to std::string?

    I agree that it's a peculiar way of defining a string this should work however
    Code:
    const unsigned char cbuffer[] = { 0x61, 0x62, 0x63, 0x0 };
    string sbuffer  = ( reinterpret_cast<const char*>( cbuffer ));
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Mar 2010
    Posts
    71

    Re: Help - how to convert a const char to std::string?

    Thanks for your help. Yes, what you suggested did pass compile if I put it in a small program.

    However, my project code still cannot pass compile. I think the source, which I try to convert to a string, is probably not a const char.

    In my project code, I put:

    Code:
    ... ...
    const commonData &myData= Report.getData();
    
    string str = myData.ID;   //I try to convert myData.ID to a string. myData.ID is type of aa::bb::Id
    
    ... ...
    aa::bb::Id is said to be "Length: 32 characters", which makes me think it's a const char. Maybe I'm wrong.

    When compiling the project with the above conversion, I got the following error:

    Code:
    error: conversion from 'const aa::bb::Id' to non-scaler type 'std::basic_string<char, std:char_traits<char>, std::allocator<char> >' requested
    What does this error msg request me to use exactly for a proper conversion?

    I would appreciate it if someone could tell me what I missed, and how to properly convert myData.ID to a string.

    Thanks,
    BJT

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help - how to convert a const char to std::string?

    Check the headers to find out exactly what aa::bb:Id is.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help - how to convert a const char to std::string?

    I don't understand why the message "Error: conversion from 'const aa::bb::Id' to non-scaler type 'std::basic_string<char, std:char_traits<char>, std::allocator<char> >' requested" would lead you to ask about const char*. It's clear that's not what the compiler is saying, and it's clearly not a const char* or the compiler would be able to pass it to a string.

    Always post real error messages and ask real questions here. When you make assumptions and don't accurately describe the problem, you waste everybody's time.

  7. #7
    Join Date
    Mar 2010
    Posts
    71

    Re: Help - how to convert a const char to std::string?

    Quote Originally Posted by GCDEF View Post
    I don't understand why the message "Error: conversion from 'const aa::bb::Id' to non-scaler type 'std::basic_string<char, std:char_traits<char>, std::allocator<char> >' requested" would lead you to ask about const char*. It's clear that's not what the compiler is saying, and it's clearly not a const char* or the compiler would be able to pass it to a string.

    Always post real error messages and ask real questions here. When you make assumptions and don't accurately describe the problem, you waste everybody's time.
    Yes, it is my bad. Sorry.

    Something else suggested it was a const char. Anyway, this issue has been resolved.

    Thanks,
    BJT

  8. #8
    Join Date
    Mar 2010
    Posts
    71

    Re: Help - how to convert a const char to std::string?

    Quote Originally Posted by S_M_A View Post
    Check the headers to find out exactly what aa::bb:Id is.
    Thanks for the hint. Yes we dig in the definition structures level by level, and have found the problem.

    Thank you for your help.
    BJT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured