If I save this string to a text file;
Hello this \n is a test message
The \n character is saved as HEX [5C 6E] I would like to have it saved as [0A].
I believe this is an encoding issue?
I am using;
// 1252 is a variable in the application
Encoding codePage = Encoding.GetEncoding("1252");
Byte[] bytes = new UTF8Encoding(true).GetBytes("Hello this \\n is a test message");
Byte[] encodedBytes = Encoding.Convert(Encoding.UTF8, codePage , bytes);
All this is inside a FileStream scope and uses fs.Write to write the encodedBytes into the file.
I have tried to use \r\n but had the same result.
Any suggestions?
Thanks!
EDIT
The string is being read from a tsv file and placed into an string array. The string being read has the "\n" in it.
To read the string I use a StreamReader reader
and split at \t
At execution time, your string contains a backslash character followed by an n
. They're encoded exactly as they should be. If you actually want a linefeed character, you shouldn't be escaping the backslash in your code:
Byte[] bytes = new UTF8Encoding(true).GetBytes("Hello this \n is a test message");
That string literal uses \n
to represent U+000A, the linefeed character. At execution time, the string won't contain a backslash or an n
- it will only contain the linefeed.
However, your code is already odd in that if you want to get the encoded form of a string, there's no reason to go via UTF-8:
byte encodedBytes = codePage.GetBytes("Hello this \n is a test message");
See more on this question at Stackoverflow