C# Convert a Unicode escaped to a string - c#

I have a text such as "abc\u001357". It's a parameter in json object which I send from client to server via http.
How can I convert that text to a string like "abc%1357". I tried with Regex to replace characters. But it doesn't work
Thanks for advance

Try this code
var str = Uri.EscapeUriString("abc\u001357");

Related

How to keep Json.Encode/Decode from converting characters into chinese letters in C#?

so first off, i know next to nothing about encoding, so thats probably the problem.
What I'm trying to do is Decode and Encode a String with Json.Decode and Json.Encode. So in the end, there should be the same string as before.
So a part of my target string has the following values in it :
"\":\"\u6b22\u8fce\u6765\u5230\" etc you get the idea. So now i convert the String like this:
DynamicJsonObject jsonObject = Json.Decode(Raw);
String newRaw = Json.Encode(jsonObject);
Now newRaw should be the same as Raw, am i right? Well the characters i mentioned look like this in newRaw:
欢迎来到《奇异人生:暴风前夕》。要及时获得最新
How can i fix that? I know that Raw has UTF8 encoding, but converting newRaw to UTF8 did not help! Thanks in advance.
"\u6b22" is a string constant that contains a single character: the unicode codepoint 6b22, a simplified Chinese character. This isn't a question of Json, but a question of c# string escape sequences.

JSON encode URLs in C# like what PHP does

When I use json_encode function in PHP to encode an Object in json format, it will change urls in output to an string with escape characters like http:\/\/example.com\/apps\/images\/image01.jpg however C# return a url as is i.e. http://example.com/apps/images/image01.jpg and will not add any escape characters. Since I'm going to implement a web-service just same as my old PHP web service, I want to know how can I encode a url string in C# to be the same as PHP string.
You don't need to do anything. If you write your string i.e. to file (or console) from PHP and from C# you will get the same results without changing anything.
If you really want to replace / by \/ use String.Replace() method:
string likePhp = strCSharp.Replace("/", #"\/");
You shouldn't need escaped slashes, so tell PHP not to escape them with JSON_UNESCAPED_SLASHES. Why try and hack the C# JSON to escape the slashes when it's not needed.:
echo json_encode('http://example.com/apps/images/image01.jpg', JSON_UNESCAPED_SLASHES);
Yields:
http://example.com/apps/images/image01.jpg

Query String issue when it contains Arabic text

I am trying to get query string from url With this code:
this.site_query = Request.Url.Query;
When I have get url:
http://localhost:1751/ar/search?q=سيارة
It gives me blow output in code:
http://localhost:1751/ar/Search?q=%D8%B3%D9%8A%D8%A7%D8%B1%D8%A9&Location=%D8%A3%D8%A8%D9%87%D8%A7,Abha
But I need Arabic text that I send in query string. When query string contains text in English then in c# it is correct.
There is nothing wrong with the second URL you have shown in your answer, it's just being URL encoded due to the limitations of what characters are allowed in URLs.
If you wish to get parts of the query string in code, you can use code like this:
var query = Request.QueryString["q"];
Additionally, if you are building your URLs in code, you should always URL encode and values that may contain non standard characters:
var urlEncodedValue = HttpUtility.UrlEncode(someValue);
As others said already: it's an encoded URL. You can decode with
var decodedUrl = HttpUtility.UrlDecode(url);
or
var decodedUrl = Uri.UnescapeDataString(url);
Is that what you need? If not, show us your expected output.
For this use
string name = HttpUtility.UrlEncode(Encrypt(txtName.Text.Trim()));
string technology = HttpUtility.UrlEncode(Encrypt(ddlTechnology.SelectedItem.Value));
for encoding url.

Unicode literal string

I'm sending some JSON in an HTTP POST request. Some of the text within the JSON object is supposed to have superscripts.
If I create my string in C# like this:
string s = "here is my superscript: \u00B9";
... it converts the \u00B9 to the actual superscript 1, which breaks my JSON. I want the \u00B9 to show up exactly as I write it in the the string, not as a superscript.
If I add an escape character, then it shows up like:
"here is my superscript: \\u00B9"
I don't want to use an escape character, but I also don't want it to be converted to the actual superscript. Is there a way to have C# not do Unicode conversion and leave it as literally: "\u00B9"?
If I understand your question correctly... add the at symbol (#) before the string to avoid the escape sequences being processed
string s = #"here is my superscript: \u00B9";
http://msdn.microsoft.com/en-us/library/362314fe(v=vs.80).aspx
I like #NinjaNye's answer, but the other approach is to use a double-backslash to make it literal. Thus string s = "here is my superscript: \\u00B9"
is recommended you encode your string before send to server. You can encode using base64 or URLEncode in client and decode in server side.

C# How to retrieve an original string from the UTF-8 string?

on my page I have set the UTF-8 encoding. Then, I send a string "ły" encoded by encodeURIComponent() function to %C5%82y and on the server side, I get %25C5%2582y. And now I want to have back the original string.
I've tried:
HttpUtility.HtmlDecode(): the result is %25C5%2582y
Uri.UnescapeDataString(): the result is %C5%82y
I'm confused now - how to retrieve back the original string ?
Use HttpUtility.UrlEncode to encode and HttpUtility.UrlDecode to decode.
Uri.UnescapeDataString(HttpUtility.UrlDecode("ły"));
done the trick

Categories