Skip to content

ReadAsciiString encoding #5

@USSRcoder

Description

@USSRcoder

Doesn't correctly (becomes ??) decode strings in cp1251 encoding.
Here my version:

internal static string ReadAsciiString(this BinaryReader br, uint length, int position = -1)
    {
        if (length == 0) return string.Empty;
        const int MAX_LENGTH = 0x40000000;
        if (length > MAX_LENGTH) throw new ArgumentOutOfRangeException(nameof(length));
        Span<byte> span = stackalloc byte[(int)length];
        br.Read(span, position);
        //unsafe
        //{
        //    return Encoding.ASCII.GetString((byte*)Unsafe.AsPointer(ref span[0]), (int)length);
        //}
        //return System.String(((byte*)Unsafe.AsPointer(ref span[0]), (int)length));
        var enc = Encoding.GetEncoding(Encoding.Default.CodePage);
        return enc.GetString(span.ToArray());
    }

This works for my system (in cp1251) and returns in UTF-8 (not ANSI 1251) - this is what we need on windows? );
for cp1251 (ANSI), just copy the binary bytes, without conversion (Like php version).

headers0.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions