-3
  var stringu = @"\u003cbr /\u003e\u003cbr /\u003eHello world";

Background here - I'm using HttpClient to request data, and am getting back a JSON string in UTF-8 (Content-Type: application/json; charset=utf-8 is the the header on the response).

To simulate the content conversion I wrote this code

  var stringu = @"\u003cbr /\u003e\u003cbr /\u003eHello world";
  var ubytes = Encoding.UTF8.GetBytes(stringu);
  var asciibytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, ubytes);
  var ascii = Encoding.UTF8.GetString(asciibytes);
  Console.WriteLine(ascii);
  Console.ReadKey();

This does not work however. I admit - I do not fully understand UTF-16/8 conversions (or even if I need to make it, considering I just output the returned bytes to a html file), so any help here is appreciated.

user466512
  • 127
  • 2
  • 9
  • It's very unclear to me what you want to get out as a result - I'd expect a JSON library to handle all of this for you. – Jon Skeet Jul 23 '13 at 06:00
  • Why use ascii? Why not UTF16? .NET's internal char type? – Cole Tobin Jul 23 '13 at 06:00

  • Hello world - is the output I want on the console
    – user466512 Jul 23 '13 at 06:02
  • @ColeJohnson The string shown is just a snippet of the UTF-8 string that is returned as the response of a http request – user466512 Jul 23 '13 at 06:05
  • Here's a little tip. You _can't_ use
    in a console. It's not HTML. Also, that string could already be written. Your code just converts it to ASCII and then back to UTF-16 by casting it to a string.
    – Cole Tobin Jul 23 '13 at 06:06
  • @ColeJohnson I know that
    will not work on the console. What I have from my response is a UTF-8 string as shown. Ideally, the response's charset should've been UTF-16, but this I cannot control.
    – user466512 Jul 23 '13 at 06:12
  • So your response would be a stream of bytes that are in UTF 8? If that's the case, then your question makes a lot more sense. I would just leave out the string "response". I'd also reccomend _against_ ASCII. – Cole Tobin Jul 23 '13 at 18:28
  • However, if what you were provided _was_ a string object, then it is _already_ in UTF16. – Cole Tobin Jul 23 '13 at 18:29
  • @ColeJohnson Agreed about the naming; this was just quick snippet code. stringu is a quoted string if you notice, if I remove the '@' I would get a UTF-16 encoded string as expected – user466512 Jul 24 '13 at 10:07
  • I don't think you understand. ANY string in .NET is internally UTF16! Even if you convert it to ASCII. Converting it to ASCII just throws away the other information. The only way to get _actual_ UTF-8 is to use a byte array (`Encoding.UTF8.GetBytes(string)`). That will convert your UTF16 string into a UTF8 byte array. – Cole Tobin Jul 24 '13 at 19:01

1 Answers1

0

Try Below code it will help you

var ss = Microsoft.JScript.GlobalObject.unescape("\u003cbr /\u003e\u003cbr /\u003eHello world")
Nitin Chaurasia
  • 253
  • 1
  • 5