Exemplo n.º 1
0
 private static int WriteUtf16CodePoint(
     char *begin, char *end,
     byte *dest)
 {
     return(UnicodeConverter.WriteUtf8CodePoint(
                UnicodeConverter.ReadUtf16CodePoint(ref begin, end),
                dest));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Allocates an unmanaged buffer and fills it with this string's contents,
        /// re-encoded as UTF-8. The resulting buffer is terminated by the null
        /// terminator character. The caller is responsible for freeing the buffer
        /// when it's done using it.
        /// </summary>
        /// <param name="str">The string to convert to a C-style string.</param>
        /// <returns>A C-style string for which the caller is responsible.</returns>
        internal static unsafe byte *ToCString(string str)
        {
            char *beginPtr   = str.DataPointer;
            char *endPtr     = beginPtr + str.Length;
            var   utf8Length = UnicodeConverter.GetUtf16ToUtf8BufferLength(beginPtr, endPtr);

            byte *cStr   = (byte *)Marshal.AllocHGlobal(utf8Length + 1);
            int   offset = 0;

            while (beginPtr != endPtr)
            {
                offset += UnicodeConverter.WriteUtf8CodePoint(
                    UnicodeConverter.ReadUtf16CodePoint(ref beginPtr, endPtr),
                    &cStr[offset]);
            }
            cStr[offset] = (byte)'\0';
            return(cStr);
        }