convert cli::array<unsigned char> into System::String

Hi,

I have been looking around for a way to convert a cli::array<unsigned char> into a System::String.
Here is what i want to do:



private: void button2_Click(System::Object^  sender, System::EventArgs^  e)
         {
             MD5 ^ md5 = gcnew MD5CryptoServiceProvider();
             System::String ^ fileName;
             fileName = textBox2->Text::get();
             FileStream ^ fs = File::OpenRead(fileName);
             cli::array<unsigned char> ^ hash;
             hash = md5->ComputeHash(fs);
             textBox1->Text::set(hash);
             MessageBox::Show("Calcul du MD5 termine.", "MD5", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
         }

 


But i have an error while compiling at textBox1->Text::set(hash);
It is normal but i don't know how to resolve this problem.
If someone can help me please.

Anyway thanks for reading.


Answer this question

convert cli::array<unsigned char> into System::String

  • Charles Denault

    >I have been looking around for a way to convert a cli::array<unsigned char> into a System::String.

    What representation of the array do you want the string to have You could use one of the System::Text::Encoding derived classes, but since the hash could contain values that would result in non-printable characters that's not something I would recommend.

    To just get a printable string you could Base64 encode the result with System::Convert::ToBase64String(). Or you can use a System::Text::StringBuilder to build a string with the hex representation of each array element. Something like

    StringBuilder ^sb = gcnew StringBuilder(hash->Length * 2);
    for (int i = 0; i < hash->Length; i++) sb->Append("{0:X2}", hash[ i]);



  • convert cli::array<unsigned char> into System::String