locked
How to set value for Platform::Array

    Question

  • I have an unsigned char array    unsigned char zh[]="test";

    Now I want to convert  it to Platform::Array,which function can i use?waiting for the answer~

    Saturday, April 14, 2012 4:11 AM

Answers

  • Thank you for your reply:-)

    I find its  Constructor      Array(T* data, unsigned int size);

    It looks convenient to use.

    At first I think it won't work,because the Intellisense tell me that the argument list is not right.But when I try

    to complie the code,it success.I'm tricked by the vs2011...

    Monday, April 16, 2012 9:32 AM
  • As you noted, you can define an array like this Array<unsigned char> or Array<unsigned char, 1>. But it seems no way to directly assign unsigned char array (such as unsigned char zh[]="test";) to Array<unsigned char>. I found no other member functions are available for this as well. You can initialize it like this Array<unsigned char>(4) {'t', 'e', 's', 't'}. But it seems not comfortable. So I advise you to define a custom function for assignment. If I missed or misunderstood something, please feel free to let me know.


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    May be this constructor can help you do the job  "Array(T* data, unsigned int size);",the code may be like the following:

    unsigned char pBuffer[]="test";
    Platform::Array<unsigned char>^ arr = ref new Platform::Array<unsigned char>(pBuffer, sizeof(pBuffer));

    the Intellisense will tell you the argument list is not right,but it really works :-)

    Tuesday, April 17, 2012 9:43 AM

All replies

  • Hi,

    Here is sample code for your reference. ConvertToArray is custom function.

    Array<unsigned char>^ ConvertToArray(unsigned char* source, int len)
    {
    	auto dest = ref new Array<unsigned char>(len);
    	for(int i=0; i<len; i++)
    	{
    		dest[i] = source[i];
    	}
    
    	return dest;
    }
    Please refer to Platform::Array Class to learn more about it.

    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 16, 2012 6:10 AM
    Moderator
  • Thank you for your reply:-)

    I find its  Constructor      Array(T* data, unsigned int size);

    It looks convenient to use.

    At first I think it won't work,because the Intellisense tell me that the argument list is not right.But when I try

    to complie the code,it success.I'm tricked by the vs2011...

    Monday, April 16, 2012 9:32 AM
  • Thanks for the report. Intellisense is still being worked on and unfortunately isn't always accurate.

    I would definitely use the Array constructor rather than explicitly copying the array.

    --Rob

    Tuesday, April 17, 2012 1:08 AM
    Owner
  • As you noted, you can define an array like this Array<unsigned char> or Array<unsigned char, 1>. But it seems no way to directly assign unsigned char array (such as unsigned char zh[]="test";) to Array<unsigned char>. I found no other member functions are available for this as well. You can initialize it like this Array<unsigned char>(4) {'t', 'e', 's', 't'}. But it seems not comfortable. So I advise you to define a custom function for assignment. If I missed or misunderstood something, please feel free to let me know.


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 17, 2012 2:10 AM
    Moderator
  • As you noted, you can define an array like this Array<unsigned char> or Array<unsigned char, 1>. But it seems no way to directly assign unsigned char array (such as unsigned char zh[]="test";) to Array<unsigned char>. I found no other member functions are available for this as well. You can initialize it like this Array<unsigned char>(4) {'t', 'e', 's', 't'}. But it seems not comfortable. So I advise you to define a custom function for assignment. If I missed or misunderstood something, please feel free to let me know.


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    May be this constructor can help you do the job  "Array(T* data, unsigned int size);",the code may be like the following:

    unsigned char pBuffer[]="test";
    Platform::Array<unsigned char>^ arr = ref new Platform::Array<unsigned char>(pBuffer, sizeof(pBuffer));

    the Intellisense will tell you the argument list is not right,but it really works :-)

    Tuesday, April 17, 2012 9:43 AM
  • Thanks for your update!

    It seems the intellisense is not accurate sometimes.


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 17, 2012 12:55 PM
    Moderator