C # bytes [] to show string not cut after '\ 0'

I read something from memory (in a byte array) and then I want to convert it, but the result is something like "wanteddata \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 ..." How can I cut it into "Wantedata"? I am not sure about the size that I would like to get, so I gave the maximum size: 14. The way I read from memory and converted:

        String w="";
        ReadProcessMemory(phandle, bAddr, buffer, 14, out bytesRW);
        w = ASCIIEncoding.ASCII.GetString(buffer);
+3
source share
4 answers

Presumably, you want to delete all characters, including after the first '\ 0'. Trimthis will not do. You need to do something like this:

int i = w.IndexOf( '\0' );
if ( i >= 0 ) w = w.Substring( 0, i );
+9
source

ascii ( char), , 0

String w="";
ReadProcessMemory(phandle, bAddr, buffer, 14, out bytesRW);
int nullIdx = Array.IndexOf(buffer, 0);
nullIdx = nullIdx >= 0 ? nullIdx : buffer.Lenght;
w = ASCIIEncoding.ASCII.GetString(buffer, 0, nullIndex);

, , '/0'

+3

bytesRW - , . GetString , . bytesRW , , .

+2

, :

public static class EncodingEx
{
    /// <summary>
    /// Convert a C char* to <see cref="string"/>.
    /// </summary>
    /// <param name="encoding">C char* encoding.</param>
    /// <param name="cString">C char* to convert.</param>
    /// <returns>The converted <see cref="string"/>.</returns>
    public static string ReadCString(this Encoding encoding, byte[] cString)
    {
        var nullIndex = Array.IndexOf(cString, (byte) 0);
        nullIndex = (nullIndex == -1) ? cString.Length : nullIndex;
        return encoding.GetString(cString, 0, nullIndex);
    }
}

...

// A call
Encoding.ASCII.ReadCString(buffer)

Array.IndexOf . byte, , 0 int byte.

0

All Articles