Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert a string which has a special character ¤. The default encoding I have is Windows-1252. And the char code is 164 ( Windows-1252) but in Ascii Extended, char code 164 is ñ ( Ascii Table). The problem i am running into is that after i have converted from Windows-1252 to Ascii, i get � in the output instead of ñ. What am i doing wrong ?

What I have tried:

Here is my code
C#
var input = "La Pe¤a";

var extAscii = Encoding.GetEncoding(437);

Encoding win1252 = Encoding.GetEncoding(1252);

var bytes1252 = win1252.GetBytes(input);

byte[] output = Encoding.Convert(Encoding.GetEncoding(1252), Encoding.GetEncoding(437),bytes1252);

Console.WriteLine(extAscii.GetString(output));
Console.ReadLine();


thanks for you help.
Posted
Updated 28-Jun-17 8:19am

The output is an interpretation of the data. Check if the problematic char isnt the value 164 or if it is an output problem. Maybe the console can only show ascii.
C#
String input = "La Pe¤a";

Encoding extAscii = Encoding.GetEncoding(437);
Encoding win1252 = Encoding.GetEncoding(1252);

var bytes1252 = win1252.GetBytes(input);

byte[] output = Encoding.Convert(win1252, extAscii, bytes1252);//use of the objects
int problem = output[5];//check for 164
Some additional sample code from Microsoft.
 
Share this answer
 
Comments
rudolph098 28-Jun-17 13:04pm    
unfortunately, i get 15
Ok, i have a solution. For now, i don't know why i get 15 when i do the following conversion below.

C#
String input = "La Pe¤a";

Encoding extAscii = Encoding.GetEncoding(437);
Encoding win1252 = Encoding.GetEncoding(1252);

var bytes1252 = win1252.GetBytes(input);

byte[] output = Encoding.Convert(win1252, extAscii, bytes1252);//use of the objects
int problem = output[5];//check for 164


so what i decided to do was to convert from asci to asci
C#
byte[] output = Encoding.Convert(extAscii, extAscii, bytes1252);//use of the objects
int problem = output[5];//check for 164


And that is how i get 164 which in ascii is ñ
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900