c# - 如何确定可移植类库中的当前代码页?

标签 c# encoding .net-core

我正在尝试将一个正在从标准类库读取文件的类库移植到可移植类库(.NET Standard,Version=v1.6),支持 ASP.NET Core。

我需要将字节数组的一部分转换为字符串。

使用 .NET Framework 4.5,我可以使用

检测当前代码页
public string ConvertToString(byte[] data, int count)
{
int CurrentCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;
return Encoding.GetEncoding(CurrentCodePage).GetString(data, 0, count);
}

Analyze Assembly Portability 工具给了我一条建议:“使用 System.Globalization.CultureInfo.CurrentCulture。” 但是没有关于当前代码页的信息。

如何在当前编码中将 byte[] 转换为字符串?

编辑: 我正在移植一个 zip 压缩库。在我的例子中,我需要正确地将 byte[] 转换为字符串。在其他问答(如 How convert byte array to string)中,我看到了使用编码。我意识到这是错误的做法。如何以正确的方式进行转换?

最佳答案

公共(public)字符串 ConvertToString(byte[] data, int count) {

返回 ASCIIEncoding.ASCII.GetString(数据,0,计数);

关于c# - 如何确定可移植类库中的当前代码页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39954919/

相关文章:

asp.net - IIS Express 自签名证书不受信任

c# - 类和数据类型有什么区别?

Java 字符串 HTTP 编码

ios - CGContextShowTextAtPoint 和欧元符号

powershell - 在 PowerShell 2.0 中将编码设置为 ANSI

asp.net-core - ASP.Net 5类库包中的EntityFramework命令?

c# - Entity Framework - 更改默认值

c# - 我需要一些想法来对这个 Tasked 方法进行单元测试

c# - 工作流程 4.0 和 System.Attribute

.net - 更新到 .NET Core SDK 2.1.4 后缺少 dotnet