gpt4 book ai didi

c# - 将可能以 null 结尾的 ascii byte[] 转换为字符串的最快方法?

转载 作者:IT王子 更新时间:2023-10-29 04:47:41 26 4
gpt4 key购买 nike

我需要将一个(可能)以 null 结尾的 ascii 字节数组转换为 C# 中的字符串,我发现最快的方法是使用如下所示的 UnsafeAsciiBytesToString 方法。此方法使用 String.String(sbyte*) 构造函数,该构造函数在其备注中包含警告:

“假定值参数指向一个数组,该数组表示使用默认 ANSI 代码页(即 Encoding.Default 指定的编码方法)编码的字符串。

注意:* 因为默认的 ANSI 代码页是系统相关的,所以这个构造函数从相同的带符号字节数组创建的字符串在不同的系统上可能不同。 * ...

* 如果指定的数组不是以 null 结尾的,则此构造函数的行为取决于系统。例如,这种情况可能会导致访问冲突。 *"

现在,我确信字符串的编码方式永远不会改变……但是我的应用运行的系统上的默认代码页可能会改变。那么,有什么理由让我不应该为了这个目的使用 String.String(sbyte*) 大喊大叫吗?

using System;
using System.Text;

namespace FastAsciiBytesToString
{
static class StringEx
{
public static string AsciiBytesToString(this byte[] buffer, int offset, int maxLength)
{
int maxIndex = offset + maxLength;

for( int i = offset; i < maxIndex; i++ )
{
/// Skip non-nulls.
if( buffer[i] != 0 ) continue;
/// First null we find, return the string.
return Encoding.ASCII.GetString(buffer, offset, i - offset);
}
/// Terminating null not found. Convert the entire section from offset to maxLength.
return Encoding.ASCII.GetString(buffer, offset, maxLength);
}

public static string UnsafeAsciiBytesToString(this byte[] buffer, int offset)
{
string result = null;

unsafe
{
fixed( byte* pAscii = &buffer[offset] )
{
result = new String((sbyte*)pAscii);
}
}

return result;
}
}

class Program
{
static void Main(string[] args)
{
byte[] asciiBytes = new byte[]{ 0, 0, 0, (byte)'a', (byte)'b', (byte)'c', 0, 0, 0 };

string result = asciiBytes.AsciiBytesToString(3, 6);

Console.WriteLine("AsciiBytesToString Result: \"{0}\"", result);

result = asciiBytes.UnsafeAsciiBytesToString(3);

Console.WriteLine("UnsafeAsciiBytesToString Result: \"{0}\"", result);

/// Non-null terminated test.
asciiBytes = new byte[]{ 0, 0, 0, (byte)'a', (byte)'b', (byte)'c' };

result = asciiBytes.UnsafeAsciiBytesToString(3);

Console.WriteLine("UnsafeAsciiBytesToString Result: \"{0}\"", result);

Console.ReadLine();
}
}
}

最佳答案

Oneliner(假设缓冲区实际上包含一个格式良好的空终止字符串):

String MyString = Encoding.ASCII.GetString(MyByteBuffer).TrimEnd((Char)0);

关于c# - 将可能以 null 结尾的 ascii byte[] 转换为字符串的最快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/144176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com