38

I know the starting address of the string(e.g., char* buf) and the max length int l; of the string(i.e., total number of characters is less than or equal to l).

What is the simplest way to get the value of the string from the specified memory segment? In other words, how to implement string retrieveString(char* buf, int l);.

EDIT: The memory is reserved for writing and reading string of variable length. In other words, int l;indicates the size of the memory and not the length of the string.

Terry Li
  • 16,870
  • 30
  • 89
  • 134

6 Answers6

28
std::string str(buffer, buffer + length);

Or, if the string already exists:

str.assign(buffer, buffer + length);

Edit: I'm still not completely sure I understand the question. But if it's something like what JoshG is suggesting, that you want up to length characters, or until a null terminator, whichever comes first, then you can use this:

std::string str(buffer, std::find(buffer, buffer + length, '\0'));
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
25
char *charPtr = "test string";
cout << charPtr << endl;

string str = charPtr;
cout << str << endl;
Taha
  • 1,086
  • 1
  • 10
  • 20
3

Use the string's constructor

basic_string(const charT* s,size_type n, const Allocator& a = Allocator());

EDIT:

OK, then if the C string length is not given explicitly, use the ctor:

basic_string(const charT* s, const Allocator& a = Allocator());
chill
  • 16,470
  • 2
  • 40
  • 44
2

There seems to be a few details left out of your explanation, but I will do my best...

If these are NUL-terminated strings or the memory is pre-zeroed, you can just iterate down the length of the memory segment until you hit a NUL (0) character or the maximum length (whichever comes first). Use the string constructor, passing the buffer and the size determined in the previous step.

string retrieveString( char* buf, int max ) {

    size_t len = 0;
    while( (len < max) && (buf[ len ] != '\0') ) {
        len++;
    }

    return string( buf, len );

}

If the above is not the case, I'm not sure how you determine where a string ends.

Amish Programmer
  • 2,051
  • 3
  • 19
  • 22
  • 1
    If the given string is guaranteed to have a NULL, all you need is `string(buf)` and you're good. If it might not, then you have to use this method. – Mooing Duck Dec 08 '11 at 23:28
1
std::string str;
char* const s = "test";

str.assign(s);

string& assign (const char* s); => signature FYR

Reference/s here.

parasrish
  • 3,864
  • 26
  • 32
0

Let,

char* rw="hii";  //This string is readable and writeable

const char* r="hello"; // This string is only readable 

we can convert char* or const char* to string with the help of string's constructor.

string string_name(parameter);

This parameter accepts both char* and const char* types .

Examples:

1) string st(rw); 

 Now string 'st', contains "hii"

2) string st(r);

 Now, string 'st' contains "hello".

In both the examples, string 'st' is writable and readable.

Vineeth Peddi
  • 486
  • 5
  • 5