The linked possible duplicate does not work for me
I got a byte array of 10 and a debug output of
"b\0\0-\0\0S\0"
where the \0 is control character (I think)
and the string result is
n
I have some textual data with a lot of characters in the 128-129 range
Particularly 150 (a control character - START OF GUARDED AREA) in a spot where I know they mean em-dash
Almost positive some data got read in as win1252 and the written as unicode
I tried getting a byte array from UTF8 and it did not work
I tried getting byte array from every enconding
In between the n and S is unicode decimal 150
Below works but I have only done a very small sampling
Encoding win1252 = Encoding.GetEncoding("Windows-1252");
bool allgood = true;
List<byte> lByte = new List<byte>();
foreach (char c in @"n S".ToCharArray())
{
if ((Int16)c > 255)
{
Debug.WriteLine("problem");
allgood = false;
break;
}
else
lByte.Add((byte)c);
}
if (allgood)
{
s1252 = win1252.GetString(lByte.ToArray());
Debug.WriteLine(s1252);
}
What is the proper way to convert from unicode to win1252?
this failed
string inputStr = @"n S";
byte[] bytes = new byte[inputStr.Length * sizeof(char)];
System.Buffer.BlockCopy(inputStr.ToCharArray(), 0, bytes, 0, bytes.Length);
s1252 = win1252.GetString(bytes);
Debug.WriteLine(s1252);
There is an extra byte 194 and the result is
n – S
this failed
Debug.WriteLine("");
unicodeBytes = unicode.GetBytes(@"n S");
foreach (byte b in unicodeBytes)
Debug.WriteLine(b.ToString() + " ub ");
// problem is here - get some good stuff but extra 0
win1252Bytes = Encoding.Convert(unicode, win1252, unicodeBytes);
char[] win1252Chars = new char[win1252.GetCharCount(win1252Bytes, 0, win1252Bytes.Length)];
Debug.WriteLine("");
foreach (char c in unicodeChars) //win1252Chars)
Debug.Write(c);
Debug.WriteLine(win1252Chars.ToString());
Debug.WriteLine("win1252Chars");