Hey all I have the following unsafe code that I am looking to have converted into something other than that.
- SecureString sString = new SecureString();
- sString.AppendChar('8');
- sString.AppendChar('1');
- sString.AppendChar('*');
- sString.AppendChar('Y');
- //...more sString.AppendChar('') here
- sString.AppendChar('Z');
- sString.AppendChar('0');
- sString.AppendChar('4');
- byte[] secureStringBytes = null;
- IntPtr unmanagedBytes = Marshal.SecureStringToGlobalAllocAnsi(sString);
- try
- {
- unsafe
- {
- byte* byteArray = (byte*)unmanagedBytes.ToPointer();
- byte* pEnd = byteArray;
- while (*pEnd++ != 0) { }
- int length = (int)((pEnd - byteArray) - 1);
- secureStringBytes = new byte[length];
- for (int i = 0; i < length; ++i)
- {
- secureStringBytes[i] = *(byteArray + i);
- }
- }
- }
- finally
- {
- Marshal.ZeroFreeGlobalAllocAnsi(unmanagedBytes);
- }
- return secureStringBytes;
Would anyone be able to help me out to change the code above so that I wont have to use /unsafe when compiling?
Tapan PatelPosted Sep 11, 2017, 5:02 PM