IFPSTools.NET icon indicating copy to clipboard operation
IFPSTools.NET copied to clipboard

ReadAsciiString encoding

Open USSRcoder opened this issue 8 months ago • 2 comments

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

USSRcoder avatar May 13 '25 10:05 USSRcoder

I'll probably need to add some way to specify ANSI encoding for cases like these. Your workaround would work for this binary if your system is set to CP1251 but not otherwise.

Wack0 avatar May 13 '25 16:05 Wack0

yes, that's why I made Encoding.Default.CodePage. It's unlikely that anyone will look at this binary if it's not with encoding 1251 in the system. The second option is to leave it as is, and not convert it (Not win frendly?). You can try to determine the encoding, this is probably the best method, but it's more complicated. )

USSRcoder avatar May 13 '25 16:05 USSRcoder