Nexus.Client.Games.Morrowind.PluginManagement.Boss.StringMarshaler.GetStringBytes C# (CSharp) Method

GetStringBytes() public static method

Reads the bytes representing a string from the given pointer.
public static GetStringBytes ( IntPtr pNativeData, Encoding p_encEncoding ) : List
pNativeData System.IntPtr The pointer to the string whose bytes are to be read.
p_encEncoding System.Text.Encoding The encoding of the string whose bytes are to be read.
return List
		public static List<byte> GetStringBytes(IntPtr pNativeData, Encoding p_encEncoding)
		{
			List<byte> lstString = new List<byte>();
			Int32 intOffset = 0;
			byte bteCharacter = 0;
			//this works for UTF8, does it work for other encodings? Not all encoding as
			// null-byte terminated
			while ((bteCharacter = Marshal.ReadByte(pNativeData, intOffset++)) != 0)
				lstString.Add(bteCharacter);
			return lstString;
		}

Usage Example

Example #1
0
        /// <summary>
        /// Marshals the given pointer to a string.
        /// </summary>
        /// <param name="pNativeData">The pointer to the data to marshal to a string.</param>
        /// <param name="p_intSize">The length of the array to marshal.</param>
        /// <returns>The marshaled string.</returns>
        public string[] MarshalNativeToManaged(IntPtr pNativeData, Int32 p_intSize)
        {
            if (pNativeData == IntPtr.Zero)
            {
                return(null);
            }

            string[] strStrings = new string[p_intSize];
            for (Int32 i = 0; i < p_intSize; i++)
            {
                IntPtr      ptrString = Marshal.ReadIntPtr(pNativeData, i * IntPtr.Size);
                List <byte> lstString = StringMarshaler.GetStringBytes(ptrString, m_encEncoding);
                strStrings[i] = m_encEncoding.GetString(lstString.ToArray(), 0, lstString.Count);
            }
            return(strStrings);
        }