Click here to Skip to main content
15,921,203 members
Home / Discussions / C#
   

C#

 
GeneralRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Richard MacCutchan20-Jan-24 0:18
mveRichard MacCutchan20-Jan-24 0:18 
GeneralRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Jens Eckervogt20-Jan-24 5:31
Jens Eckervogt20-Jan-24 5:31 
GeneralRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Dave Kreskowiak20-Jan-24 4:52
mveDave Kreskowiak20-Jan-24 4:52 
AnswerRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Gerry Schmitz19-Jan-24 16:41
mveGerry Schmitz19-Jan-24 16:41 
AnswerRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
jschell22-Jan-24 5:34
jschell22-Jan-24 5:34 
GeneralRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Jens Eckervogt22-Jan-24 14:17
Jens Eckervogt22-Jan-24 14:17 
SuggestionRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Richard Deeming29-Jan-24 3:56
mveRichard Deeming29-Jan-24 3:56 
GeneralRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Jens Eckervogt19-May-24 8:01
Jens Eckervogt19-May-24 8:01 
Hello I am back.

I have tried but I am glad because I found solution.

namespace DeafMan1983.Interop.Runtime.Utilities;

using System;
public static unsafe class UtilitiesForUTF16
{
    /*
     *  string from char* (UTF16)
     */
    public static string CharPointerToString(char* ptr)
    {
        if (ptr == null)
            return string.Empty;

<pre>
    int length = 0;
    while (ptr[length] != '\0')
    {
        length++;
    }

    return new string(ptr, 0, length);
}

/*
 *  char* (UTF16) from string
 */
public static char* StringToCharPointer(string input)

{
if (input == null)
return null;

char* utf16Ptr = stackalloc char[input.Length + 1];

fixed (char* inputPtr = input)
{
for (int i = 0; i < input.Length; i++)
{
utf16Ptr[i] = inputPtr[i];
}

inputPtr[input.Length] = '\0';
return inputPtr;
}
}

/*
* It works like strlen, but it uses only char* (UTF16)
/
public static int CharPointerLength(char
charPtrs)
{
if (charPtrs == null)
return 0;

int length = 0;
while (charPtrs[length] != '\0')
{
length++;
}

return length;
}
}


And I resolved it:
1 Test:
Shows
Console.WriteLine()
:
string string_str1 = "Hello World!";
char* char_str1 = StringToCharPointer(string_str1);
Console.WriteLine($"Result: {CharPointerToString(char_str1)}");
Console.WriteLine($"Length of CharPointer: {CharPointerLength(char_str1)}");

char* char_str2 = stackalloc char[] { 'H', 'e', 'l', 'l', 'o' };
string str2 = CharPointerToString(char_str2);
Console.WriteLine($"Result: {str2}");
Console.WriteLine($"Length of CharPointer: {CharPointerLength(char_str2)}");
Picture 1

And I tested with WinAPI ( TerraFX.Interop.Windows )
I have tested it and it won't show Chinese Language
Picture 2

I am very excited to resolve it.

But I don't know if It works fine. I hope it works anything if UTF16 says wrong like Chinese Fonts show up then we need add some Encoding....
QuestionHow to customize the default .Net Framework Validation Error Messages Pin
Fokwa Divine11-Jan-24 3:30
Fokwa Divine11-Jan-24 3:30 
AnswerRe: How to customize the default .Net Framework Validation Error Messages Pin
Pete O'Hanlon11-Jan-24 3:46
mvePete O'Hanlon11-Jan-24 3:46 
GeneralRe: How to customize the default .Net Framework Validation Error Messages Pin
Fokwa Divine11-Jan-24 4:17
Fokwa Divine11-Jan-24 4:17 
GeneralRe: How to customize the default .Net Framework Validation Error Messages Pin
Pete O'Hanlon11-Jan-24 18:56
mvePete O'Hanlon11-Jan-24 18:56 
Questiondisplay only first 10 files from each directory Pin
picasso29-Jan-24 11:59
picasso29-Jan-24 11:59 
AnswerRe: display only first 10 files from each directory Pin
jschell9-Jan-24 12:39
jschell9-Jan-24 12:39 
AnswerRe: display only first 10 files from each directory Pin
OriginalGriff9-Jan-24 19:54
mveOriginalGriff9-Jan-24 19:54 
AnswerRe: display only first 10 files from each directory Pin
Richard Deeming9-Jan-24 23:44
mveRichard Deeming9-Jan-24 23:44 
GeneralRe: display only first 10 files from each directory Pin
OriginalGriff10-Jan-24 23:27
mveOriginalGriff10-Jan-24 23:27 
GeneralRe: display only first 10 files from each directory Pin
Richard Deeming11-Jan-24 0:45
mveRichard Deeming11-Jan-24 0:45 
QuestionCompatibity Pin
PedroSini9-Jan-24 5:06
PedroSini9-Jan-24 5:06 
AnswerRe: Compatibity Pin
Pete O'Hanlon9-Jan-24 5:15
mvePete O'Hanlon9-Jan-24 5:15 
QuestionGet List Of Physical & Logical Drives Pin
Kevin Marois4-Jan-24 22:15
professionalKevin Marois4-Jan-24 22:15 
AnswerRe: Get List Of Physical & Logical Drives Pin
Richard Deeming4-Jan-24 22:50
mveRichard Deeming4-Jan-24 22:50 
GeneralRe: Get List Of Physical & Logical Drives Pin
Kevin Marois5-Jan-24 7:32
professionalKevin Marois5-Jan-24 7:32 
QuestionI need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:15
professionalglennPattonWork33-Jan-24 2:15 
AnswerRe: I need to convert a string to a float.... Pin
Ron Nicholson3-Jan-24 2:23
professionalRon Nicholson3-Jan-24 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.