-2

I hope I have clearly stated my goal in the topic and following is the code I am using.

#include <stdio.h>
#include <inttypes.h>
#include <iostream>
using namespace std;

int main() {
    const char *data_ptr =(char*)"test";
    const uint8_t* p = reinterpret_cast<const uint8_t*>(&data_ptr);
    uint8_t* p1= const_cast<uint8_t*>(p);
    char* p2 = reinterpret_cast<char*>(p1);
    const char *final= const_cast<const char*>(p2);
    string s1( data_ptr);
    string s( reinterpret_cast<char const*>(p1),4) ;
    cout<<"data_ptr is "<<data_ptr<<endl;
    cout<<"p "<<p<<endl;
    cout<<"p1 "<<p1<<endl;
    cout<<"p2 "<<p2<<endl;
    cout<<"final is "<<final<<endl;
    cout<<"final1 is "<<s1.size() << "<-->"<<s.size()<<endl;
    return 0;
}

and what it prints is as follows.

data_ptr is test
p X@
p1 X@
p2 X@
final is X@
final1 is 4<-->4

What should I do to get the "test" as a string.

Tharanga
  • 2,007
  • 4
  • 32
  • 50
  • Not sure what you're trying to achieve here. You're basically just casting pointers around. Have you tried `std::string(some-of-your-pointer)`? Otherwise, give a bit more context on what you want to do exactly. –  Feb 03 '16 at 08:56
  • 2
    `&data_ptr` is not a pointer to characters, it's a pointer *to the pointer* to some characters. – Some programmer dude Feb 03 '16 at 08:56
  • ^what joachim said.. also wondering where you got the `const char *data_ptr =(char*)"test";` notation, don't think I've ever seen that `(char*)` cast before? – stijn Feb 03 '16 at 08:56
  • @stijn is right about the cast. Per definition, `"test"` is constant anyway, so no cast needed. And Joachim is right about the pointer; that's why the second string (`s`) outputs garbage. –  Feb 03 '16 at 09:00
  • Yeah my mistake about the pointer. But what I really want to know is to convert a uint8_t array in to a string. I know that there is an array. What I do not know is if it is null terminated. So I was trying to is convert a string to a uint8_t and get the string back from the uint8_t I created. – Tharanga Feb 03 '16 at 09:03

4 Answers4

2

As pointed out in the comments above, your data_ptr cast in line

const uint8_t* p = reinterpret_cast<const uint8_t*>(&data_ptr);

casts from a pointer to chars (char*) to a pointer of pointer of chars (in your case uint8_t*, referencing &data_ptr, which is in turn char*). That's also why your string s1 in

string s1( data_ptr);

Contains the correct test string, while s, as initialized in

string s( reinterpret_cast<char const*>(p1),4) ;

with p1 referencing the wrong pointer to pointer to chars from

const uint8_t* p = reinterpret_cast<const uint8_t*>(&data_ptr);
uint8_t* p1= const_cast<uint8_t*>(p);

is just initialized with garbage data.

So just change your line

const uint8_t* p = reinterpret_cast<const uint8_t*>(&data_ptr);

to

const uint8_t* p = reinterpret_cast<const uint8_t*>(data_ptr);

and both, s and s1 contain your source string "test".

0

std::string has a constructor taking a char* or const char* as a parameter. It also has a function c_str() to do the conversion back to a char* so all thats needed is this:

const char* charptr = "Test";
string str(charptr);
const char* charptr2 = str.c_str();

//just to verify:
cout << charptr << "\n";
cout << str << "\n";
cout << charptr2 << "\n";

Edit: As you stated in the comments you need an uint8_t, for this just take the raw char* as it is:

const uint8_t* intptr = reinterpret_cast<const uint8_t*>(charptr);

Try it online: http://ideone.com/drJYuR

Anedar
  • 4,235
  • 1
  • 23
  • 41
0

"test" will not fit in an unsigned char. You should use the standard c functions atoi and itoa to do the conversion. You can also use these functions if you want to do it character by character.

kizeloo
  • 183
  • 8
  • He's assigning it to a `const char*`, which is a pointer to a sequence is chars (i.e., an array). There is no issue with his input not fitting into an unsigned char. –  Feb 03 '16 at 09:04
0

I am not entirely sure what you are trying to do here. However, changing

const uint8_t* p = reinterpret_cast<const uint8_t*>(&data_ptr);

to

const uint8_t* p = reinterpret_cast<const uint8_t*>(data_ptr);

would do the job for you.

Ravi Shenoy
  • 467
  • 1
  • 4
  • 14