I make a simple printing app where I use \n
, \t
to text format like tab or new line.
It is run perfectly when I input using hard code like this.
isi = "ARIEF - JL. ANGGA JAYA II\n\t\t\t\tSTRUK PEMBAYARAN NEXXT MEDIA\n\nIDPEL\t\t\t: 09343590348509435\nTGL. BAYAR\t\t: 2017-09-02 10:26:53 \t\t\tTAGIHAN\t: 1.000.000\nPRODUK\t\t:\t\t\t\t\tADMIN\t\t: 2.500\nNAMA\t\t\t: RYAN\t\t\t\t\tTOTAL\t\t: 1.500.000\nREF\t\t\t: ARIEF - JL. ANGGA JAYA II\n\n\t\t\tCATUR TUNGGAL KAB SLEMAN DAISTA YOGYAKARTA\n\n\t\t\tSTRUK INI MERUPAKAN BUKTI PEMBAYARAN YANG SAH\n\t\t\t HUBUNGI CUSTOMER SUPPORT LOKET PPOB ANDA\n";
and runs like this:
But when I acquire the string from base64 decode using this code:
public string base64ToString(string str)
{
byte[] data = Convert.FromBase64String(str);
return Encoding.UTF8.GetString(data).ToString();
}
it doesn't escape format anymore and get this layout:
Is there something wrong with my code? Or am I missing some basic C# knowledge?
Here is UTF-8 base64 encoded string:
QVJJRUYgLSBKTC4gQU5HR0EgSkFZQSBJSVxuXHRcdFx0XHRTVFJVSyBQRU1CQVlBUkFOIE5FWFhUIE1FRElBXG5cbklEUEVMXHRcdFx0OiAwOTM0MzU5MDM0ODUwOTQzNVxuVEdMLiBCQVlBUlx0XHQ6IDIwMTctMDktMDIgMTA6MjY6NTMgXHRcdFx0VEFHSUhBTlx0OiAxLjAwMC4wMDBcblBST0RVS1x0XHQ6XHRcdFx0XHRcdEFETUlOXHRcdDogMi41MDBcbk5BTUFcdFx0XHQ6IFJZQU5cdFx0XHRcdFx0VE9UQUxcdFx0OiAxLjUwMC4wMDBcblJFRlx0XHRcdDogQVJJRUYgLSBKTC4gQU5HR0EgSkFZQSBJSVxuXG5cdFx0XHRDQVRVUiBUVU5HR0FMIEtBQiBTTEVNQU4gREFJU1RBIFlPR1lBS0FSVEFcblxuXHRcdFx0U1RSVUsgSU5JIE1FUlVQQUtBTiBCVUtUSSBQRU1CQVlBUkFOIFlBTkcgU0FIXG5cdFx0XHRIVUJVTkdJIENVU1RPTUVSIFNVUFBPUlQgTE9LRVQgUFBPQiBBTkRBXG4=
I got that string by using this:
The tool you used was not the right tool for the job. It took the literal text you pasted in there (backslashes and all) and converted it to a base64 string. From a programmer's perspective, it changed \n
to "\\n"
. For that tool to work, you cannot enter \n
and expect to see a newline, you need to actually type in a newline character. Same for \t
, you need to type in actual tab characters (assuming that tool doesn't do any additional processing on the data).
It would just be easier to encode it in code.
string EncodeString(string str, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
return Convert.ToBase64String(encoding.GetBytes(str));
}
string DecodeString(string str, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
return encoding.GetString(Convert.FromBase64String(str));
}