c++type-conversionc++builder-xe8

How to cast AnsiString to char?


What I'm troubling about is conversion. I'm using C++ Rad Studio XE8 (don't ask why cause I had to).

I have variables

AnsiString a = "D8";
char sz;

I want sz to look like this

char sz = 0xD8;

How do I cast the AnsiString a to char so that sz will end up equaling 0xD8?

I have tried memcpy, strcpy, etc. yet couldn't find solutions.


Solution

  • One way is:

    unsigned char sz = strtol( a.c_str(), NULL, 16 );
    

    Note that unsigned char is preferable because plain char is signed, and 0xD8 is out of range of plain char.