0

I have this base-64 string:

oKQmwbpPwZGxUGeWQNBucVmNy1JVsxM9uNivn6hpkia+15y3yIemkW8GXW423krf8WNk6yebQixZW78EpPMMtzldQbbsaEmd4aUDCwp78ivOuh83nC8fHy2gwkm5NcS7aaGm2KxkUsWa5ouNUa7BUWPZ3F7LXFR/SLjZRTMY8u7hwYEQCmUQk/zNXsLyHHwZQiOjZfXdb1nC4vu1LItxaw==

I have to convert it to something like this:

oKQmwbpPwZGxUGeWQNBucVmNy1JVsxM9uNivn6hpkia+15y3yIemkW8GXW423krf8WNk6yebQixZ\nW78EpPMMtzldQbbsaEmd4aUDCwp78ivOuh83nC8fHy2gwkm5NcS7aaGm2KxkUsWa5ouNUa7BUWPZ\n3F7LXFR/SLjZRTMY8u7hwYEQCmUQk/zNXsLyHHwZQiOjZfXdb1nC4vu1LItxaw\u003d\u003d\n

But I can't do it.

It seems so easy but I tried everything I knew, like this methods:

HttpUtility.UrlEncode

HttpUtility.HtmlDecode

Regex.Escape

even:

.Replace(System.Environment.NewLine, "\n").Replace("=", "u003d")

But none of them works!!!!

davide-pi
  • 310
  • 1
  • 9
Mohammad
  • 2,724
  • 6
  • 29
  • 55
  • 2
    This souunds like an X/Y problem. You have [tag:json] tagged. How are you going to use it? There might be a simpler way in the larger context. You might not actually need to escape that character. – madreflection Oct 28 '20 at 19:40
  • i should wrap this string in json object. like this {"data":"string"} – Mohammad Oct 28 '20 at 19:43
  • 3
    Use a JSON serializer. It will encode what it needs to, and that character (`=`) doesn't need it. – madreflection Oct 28 '20 at 19:43
  • Does this answer your question? [Json.NET serialize object with root name](https://stackoverflow.com/questions/16294963/json-net-serialize-object-with-root-name) What you'll do is even simpler since you have just a string, not a Car object. – madreflection Oct 28 '20 at 19:45
  • The `=` character doesn't need to be encoded, but your new lines might need to be. The very popular JSON framework Newtonsoft will do it for you, e.g. `var s = JsonConvert.ToString(yourBase64String);` See https://stackoverflow.com/questions/1242118/how-to-escape-json-string for more options. – Cᴏʀʏ Oct 28 '20 at 19:47
  • dear @madreflection. the json serializer is the problem . i don't want o convert u003d to =. but it does any way. – Mohammad Oct 28 '20 at 19:47
  • 1
    You need to tell us what you need to accomplish in addition to just giving an example of the output. It seems you have added newlines inside the string there, can you explain why? And according to which rules? – Lasse V. Karlsen Oct 28 '20 at 20:33
  • 1
    Is this input a “string literal” by chance? No JSON serializer should transform the 6-character \u003d sequence in that manner when *serializing*.. and when de-serializing, it’s the only appropriate action. However, if it appears in a “string literal” the transformation might already have been applied to the original string. – user2864740 Oct 28 '20 at 20:33

1 Answers1

1

If I skip the json part and assume you want to insert the pattern "\n" and "\u003d" in that original base-64 string then you need to escape in the Replace statement, as below enter image description here

The code for 2 scenarios

    string t1 = @"oKQmwbpPwZGxUGeWQNBucVmNy1JVsxM9uNivn6hpkia+15y3yIemkW8GXW423krf8WNk6yebQixZ
W78EpPMMtzldQbbsaEmd4aUDCwp78ivOuh83nC8fHy2gwkm5NcS7aaGm2KxkUsWa5ouNUa7BUWPZ
3F7LXFR / SLjZRTMY8u7hwYEQCmUQk / zNXsLyHHwZQiOjZfXdb1nC4vu1LItxaw == ";

    var b1 = t1.Contains(Environment.NewLine);  //return true
    // The line below will insert the pattern "\n" and "\u003d"
    string t2 = t1.Replace(System.Environment.NewLine, "\\n").Replace("=", "\\u003d");

    // otherwise (Environment.NewLine is \r\n)
    string t3 = t1.Replace(System.Environment.NewLine, "\n").Replace("=", "u003d");
    var b2 = t3.Contains(Environment.NewLine);  //return false