@@ -245,6 +245,88 @@ A test function that prints all class members. Only available in debug mode.
245245### ` Test() `
246246A test function that runs all functions. Only available in debug mode.
247247
248+ ## Using the DLL
249+ ### C#
250+ Required namespace: System.Runtime.InteropServices
251+
252+ The Following shows how to define all conversion functions in your C# class:
253+ ``` C#
254+ // char* to char*
255+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Ansi )]
256+ private static extern IntPtr ConvertCharStringToCharStringUnsafe (byte [] input , int inputEncoding , int outputEncoding );
257+
258+ // char* to wchar_t*
259+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Ansi )]
260+ private static extern IntPtr ConvertCharStringToWcharStringUnsafe (byte [] input , int inputEncoding , int outputEncoding );
261+
262+ // char* to u32char_t*
263+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Ansi )]
264+ private static extern IntPtr ConvertCharStringToWU32charStringUnsafe (byte [] input , int inputEncoding , int outputEncoding );
265+
266+ // wchar_t* to char*
267+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Unicode )]
268+ private static extern IntPtr ConvertWcharStringToCharStringUnsafe (char [] input , int inputEncoding , int outputEncoding );
269+
270+ // wchar_t* to wchar_t*
271+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Unicode )]
272+ private static extern IntPtr ConvertWcharStringToWcharStringUnsafe (char [] input , int inputEncoding , int outputEncoding );
273+
274+ // wchar_t* to char32_t*
275+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Unicode )]
276+ private static extern IntPtr ConvertWcharStringToU32charStringUnsafe (char [] input , int inputEncoding , int outputEncoding );
277+
278+ // char32_t* to char*
279+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl )]
280+ private static extern IntPtr ConvertU32charStringToCharStringUnsafe (UInt32 [] input , int inputEncoding , int outputEncoding );
281+
282+ // char32_t* to wchar_t*
283+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl , CharSet = CharSet .Auto )]
284+ private static extern IntPtr ConvertU32charStringToWcharStringUnsafe (UInt32 [] input , int inputEncoding , int outputEncoding );
285+
286+ // char32_t* to char32_t*
287+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl )]
288+ private static extern IntPtr ConvertU32charStringToU32charStringUnsafe (UInt32 [] input , int inputEncoding , int outputEncoding );
289+ ```
290+
291+ The following functions must be used to free the allocated memory of the converted strings (output strings)
292+ ``` C#
293+ // free C++ char*/C# byte[] string
294+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl )]
295+ private static extern void FreeMemoryCharPtr (IntPtr ptr );
296+
297+ // free C++ wchar_t*/C# string
298+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl )]
299+ private static extern void FreeMemoryWcharPtr (IntPtr ptr );
300+
301+ // free C++ char32_t*/C# UInt32[] string
302+ [DllImport (" MorphText.dll" , CallingConvention = CallingConvention .Cdecl )]
303+ private static extern void FreeMemoryU32charPtr (IntPtr ptr );
304+ ```
305+
306+ Usage examples:
307+ ``` C#
308+ // signle-byte characters to C# string
309+ Byte [] utf8 = new Byte [11 ] { 0x 4d , 0x 45 , 0x 4f , 0x c3 , 0x 96 , 0x c3 , 0x 9c , 0x c3 , 0x 84 , 0x 57 , 0x 00 };
310+ IntPtr resPtr = ConvertCharStringToWcharStringUnsafe (utf8 , 1 /* utf8*/ , 2 /* utf16 little endian*/ );
311+ StringBuilder txt = new StringBuilder (Marshal .PtrToStringUni (resultPtr ));
312+ string utf16 = txt ;
313+ FreeMemoryWcharPtr (resultPtr );
314+
315+ // C# string to C# string
316+ char [] utf16BE = new char [3 ] { 0x 4700 , 0x 5300 , 0x 3300 };
317+ IntPtr resPtr = ConvertWcharStringToWcharStringUnsafe (utf8 , 3 /* utf16 big endian*/ , 2 /* utf16 little endian*/ );
318+ StringBuilder txt = new StringBuilder (Marshal .PtrToStringUni (resultPtr ));
319+ string utf16 = txt ;
320+ FreeMemoryWcharPtr (resultPtr );
321+
322+ // quatrouple-byte characters to C# string
323+ UInt32 [] utf8 = new UInt32 [4 ] { 0x 30d00000 , 0x df000000 , 0x 45f40100 , 0 };
324+ IntPtr resPtr = ConvertU32charStringToWcharStringUnsafe (utf8 , 1 /* utf8*/ , 2 /* utf16 little endian*/ );
325+ StringBuilder txt = new StringBuilder (Marshal .PtrToStringUni (resultPtr ));
326+ string utf16 = txt ;
327+ FreeMemoryWcharPtr (resultPtr );
328+ ```
329+
248330## ToDo
249331* check if double-byte characters of Shift-Jis are stored in LE on LE machines
250332* check if double-byte characters of KS X 1001 are stored in BE on BE machines and in LE on LE machines
@@ -259,6 +341,7 @@ A test function that runs all functions. Only available in debug mode.
259341* test on a big-endian system
260342 * add necessary endianness checks to UTF-16 and UTF32-operations
261343
344+
262345## Credits
263346* Lawn Meower: Idea, Code
264347* [ sozysozbot] ( https://github.com/sozysozbot ) : original KS X 1001 table
0 commit comments