Reputation:
I have a string which has a character I would like to replace.
The character has a hex value of 96
and I would like to replace that character with a hex value of 2D
. I tried to do a simple replace on the string but it didn't work because for some reason it doesn't recognize the char. And whenever I print it out it just prints a blank value:
byte testByte = byte.Parse("96", System.Globalization.NumberStyles.HexNumber);
char testChar = Convert.ToChar(testByte); // when I print this its just a blank char
So, I moved on and converted the entire string into hex, but am not sure how to convert the hex value string back to string. Here's what I have:
// using windows 1252 encoding
System.Text.Encoding windows1252Encoding = System.Text.Encoding.GetEncoding(1252);
byte[] myByte = windows1252Encoding.GetBytes(myString);
var myHexString = BitConverter.ToString(myByte);
myHexString = myHexString .Replace("96", "2D");
so at this point I have replaced the hex value 96 to 2D, but how do I convert this hex value string back to string? Any help would be great!
Upvotes: 2
Views: 1202
Reputation: 1503599
If you're actually trying to replace U+0096 with U+002D, you can use:
text = text.Replace("\u0096", "\u002d");
Note that U+0096 is "start of guarded area" though. If you actually mean "the value that is encoded in Windows 1252 as 96" then you might want:
text = text.Replace("\u2020", "\u002d");
(Based on the Wikipedia cp 1252 page, which shows 0x96 mapping to U+2020.)
Upvotes: 2
Reputation: 700820
Just replace one character with the other. You can use the \x
escape code to use a character code in hex to specify a character:
s = s.Replace('\x96', '\x2d');
Upvotes: 0