“Unable to translate bytes [...] at index 0 from specified code page to Unicode.” error in .NET Core applications

  • 4 December 2023
  • 0 replies
  • 470 views

Userlevel 1
Badge

In .NET Core applications, when handling data with a character set that is not bundled with .NET Standard, you may come across the following error message: “Unable to translate bytes [..] at index x from specified code page to Unicode”. 

To resolve this issue, you will need to install the System.Text.Encoding.CodePages dependency 

  • Install-Package System.Text.Encoding.CodePages 

, and add the following line of code to your application at the start of your code:  

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); 

 

In the following example, I have used the CData ADO.NET Provider for Amazon Marketplace to create a connection to our Japanese Amazon Marketplace account in Entity Framework Core 6.0 (please consult the “Getting Started with EFCore” section of the driver’s documentation: https://cdn.cdata.com/help/ONJ/ado/pg_efCoreOverview.htm ).  

The API’s charset is charset=Windows-31J: 

 

 

This character encoding isn’t supported by default in .NET Core, hence the error: 

 

 

 

For more information about supported code page encodings in .NET Core, please refer to Microsoft’s documentation: https://learn.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=net-8.0#remarks  

In order to retrieve all the data, I added the dependency and registered the encoding: 

 

 

Please reach out to support@cdata.com if you run into any issues. 


This topic has been closed for comments