3

I'm trying to port a class library that is reading files from a standard class library to a portable class library (.NET Standard,Version=v1.6), supporting ASP.NET Core.

I need convert a portion of a byte array to a string.

With .NET Framework 4.5 I could detect current code page with

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

A tool Analyze Assembly Portability gave me an advice: "Use System.Globalization.CultureInfo.CurrentCulture." But there is no information about current code page.

How can I convert byte[] to string in current enconding?

Edit: I'm porting a zip-compression library. And in my case I need convert byte[] to string properly. In other q&a (like How convert byte array to string) i saw using Encoding. I realized that it was the wrong approach. How can I make convert on right way?

Community
  • 1
  • 1
  • I'd argue that "current code page" is a Windows-specific concept which might not have an equivalent on all operating systems where ASP.NET Core is supported. Why do you even need "the server's current legacy ANSI codepage" in a web application? – Heinzi Oct 10 '16 at 09:21
  • there are virtually no scenarios where the current OS codepage is relevant *anyway* (you should be explicit when specifying encodings); what is the case that you need here? – Marc Gravell Oct 10 '16 at 09:22
  • I'm porting a zip-compression library. And in my case I need convert byte[] to string properly. In other q&a (like http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string) i saw using Encoding. I realized that it was the wrong approach. How can I make convert on right way? – Olesya Novikova Oct 10 '16 at 10:22

1 Answers1

-1

public string ConvertToString(byte[] data, int count) {

return ASCIIEncoding.ASCII.GetString(data,0,count); }

AKHIL KANKRAN
  • 69
  • 1
  • 7