2

I read data from a file and I wanted to use on of its values in a function. This is a custom function, I am programming TOOLKIT for Creo Parametric.

Well, that function is expecting a 'xrstring' but I have a std::string.

How to I convert one into another?

I've tried writing xrstring test_var = xrstring(std_var);

And it doesn't work.

//definition of xrstring

typedef const char          *xrstring;
#define xstringuninit   ((xrstring) 2)
#define xstringnil      ((const char *) 1)
#define xwstringnil     ((const wchar_t *) 1)
#define xwstringuninit     ((const wchar_t *) 2)

//definition of pfcCreateStringParamValue

pfcParamValue_ptr        pfcCreateStringParamValue (xrstring Value);

error C2664: 'pfcParamValue_ptr pfcCreateStringParamValue(xrstring)': cannot convert argument 1 from 'std::string' to 'xrstring'

5
  • 4
    Probably xrstring test_var(std_var.c_str()); Commented Oct 30, 2019 at 15:08
  • You can't implicitly convert a std::string object to a pointer to char (const or not), and you have never been able to do that. You need to explicitly do it using the c_str function. This should be well known to all who studied a little. Commented Oct 30, 2019 at 15:09
  • Note that xrstring test_var = std_var.c_str(); the test_var is only valid for as long as the life span of std_var, and only as long as std_var is not modified. If test_var needs to live longer than that, std::strdup will make a copy, but then you need to std::free that memory when done with test_var. Commented Oct 30, 2019 at 15:18
  • Thanks everyone, c_str work. However, if std_var has special characters, test_var becomes some weird characters. How can I fix this? Commented Oct 30, 2019 at 15:18
  • This is a custom function, I am programming TOOLKIT for Creo Parametric. -- I can't think of a single "toolkit" that has their own string type to not be able to take a char * to a buffer of characters. The std::string::c_str() function provides such a pointer. Commented Oct 30, 2019 at 15:18

1 Answer 1

2

The xrstring you speak of is const char * or a C-style string.

std::string comes with a convenient function to convert a std::string to C-style string - std::string::c_str()

Hence, here is the right way to do what you want to do:

xrstring test_var = std_var.c_str();

Also, there does not exist a direct const char * to std::string cast. The Standard Library string is not a mere pointer-to-char. It contains other information like string-length as well.

I personally would recommend you to declare xstring as a class if you want to do a conversion like xrstring test_var = xrstring(std_var);, and make a suitable constructor for the conversion.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that works. However, if std_var has special characters, test_var becomes some weird characters. How can I fix this?
Can you show us one such case here (a std_var with special characters and what it's test_var looks like)?
I was trying to write in a logfile to check if I was retrieving data correctly from my .csv file. I kept getting weird results whenever I started my program. I figured out through my notepad++ that it was sometimes encoding in ANSI and sometimes UTF-8. In ANSI It comes out good "Construção", in UTF-8, instead of a "çã" i have 2 black rectangles with xE7 and xE3 (can't copy it here). When I use this string in my program, I get this: ඗翷.
I guess, you might want to take a look at wchar_t and wstring to get the Unicode support in C++ and define typedef const wchar_t *xrstring; instead. I am not aware of any implementations of C++ where normal char and string are interpreted as UTF-8 by the compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.