locked
Help me convert this code in c++

    Question

  • I use this code to convert String to char array
    It works on vc++ clr

    1 void StringToChar(String ^s, char ch[100])
    2 {
    3  int i = 0;
    4  int c;
    5
    6  if (s->Length > 0)
    7  {
    8   for (i = 0; i < s->Length; i++)
    9   {
    10    c = s[i];
    11    ch[i] = c;
    12  }
    13   ch[i] = '\0';
    14  }
    15  else ch[0] = '\0';
    16 }

    My problem is when i use this code for my
    windows store application it doesn't work
    anymore.  Line Number 10 has an error. I'm

    stuck here.

    I need this covertion because i'm creating
    a file maintenance program using metro style.
    I use this in a struct statement like this:

    struct data{

     char last[15];
     char firsname[15];
     char mi[3];
    };

    Thanks in advance


    mjra

    Saturday, December 7, 2013 12:30 PM

Answers

  • You don't really need this function. You can get the string buffer from it's Data method. You can copy it out with normal C string handling functions if you like:

    void StringToChar(String^ s, wchar_t ch[100])
    {
    	wcscpy_s(ch, 100, s->Data);
    }
    

    If you want to convert from the default Unicode to MBCS (wchar to char) then you can use WideCharToMultiByte .

    For your specific code, the immediate problem is that the String^ s not directly an array of chars, so you can't dereference it on line 4. You can convert to the array by using its Data property. Also, Windows Store apps are Unicode, so it will be an array of wchars not an array of chars. The conversion on line 11 will overflow if any characters require two bytes. Lastly, you have a buffer overrun error here. You need to make sure you don't write past the 100 characters in the buffer, even if the string being copied is longer.

    --Rob

    • Proposed as answer by MikeBMcLMVP Friday, December 13, 2013 4:47 PM
    • Marked as answer by ziong100363 Sunday, December 15, 2013 12:12 AM
    Saturday, December 7, 2013 10:41 PM
    Owner

All replies

  • Hi mjra,

    Im not an expert on this but i think the error is in line 4. The local variable c should be of type char not int.


    lh

    Saturday, December 7, 2013 2:31 PM
  • If you use char instead of int a warning will be raised.

    The code works fine in vc++ clr and I thought this

    code will work on winrt c++

    thanks


    mjra

    Saturday, December 7, 2013 3:10 PM
  • You don't really need this function. You can get the string buffer from it's Data method. You can copy it out with normal C string handling functions if you like:

    void StringToChar(String^ s, wchar_t ch[100])
    {
    	wcscpy_s(ch, 100, s->Data);
    }
    

    If you want to convert from the default Unicode to MBCS (wchar to char) then you can use WideCharToMultiByte .

    For your specific code, the immediate problem is that the String^ s not directly an array of chars, so you can't dereference it on line 4. You can convert to the array by using its Data property. Also, Windows Store apps are Unicode, so it will be an array of wchars not an array of chars. The conversion on line 11 will overflow if any characters require two bytes. Lastly, you have a buffer overrun error here. You need to make sure you don't write past the 100 characters in the buffer, even if the string being copied is longer.

    --Rob

    • Proposed as answer by MikeBMcLMVP Friday, December 13, 2013 4:47 PM
    • Marked as answer by ziong100363 Sunday, December 15, 2013 12:12 AM
    Saturday, December 7, 2013 10:41 PM
    Owner
  • Thanks it works now.

    Best regards


    mjra

    Sunday, December 15, 2013 12:11 AM