-
February 17th, 2011, 01:12 PM
#1
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
-
February 17th, 2011, 01:16 PM
#2
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.
-
February 17th, 2011, 01:27 PM
#3
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 ));
-
February 17th, 2011, 02:08 PM
#4
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
-
February 17th, 2011, 02:15 PM
#5
Re: Help - how to convert a const char to std::string?
Check the headers to find out exactly what aa::bb:Id is.
-
February 17th, 2011, 02:20 PM
#6
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.
-
February 17th, 2011, 02:38 PM
#7
Re: Help - how to convert a const char to std::string?
Originally Posted by GCDEF
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
-
February 17th, 2011, 02:39 PM
#8
Re: Help - how to convert a const char to std::string?
Originally Posted by S_M_A
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|