I'm looking for a way to convert a string into a sequence of Unicode character literals in C#.
For example:
Input:
Hi!
Output:
\u0048\u0069\u0021
If you mean you want that output as string, you can iterate through all the characters get their Unicode hex values:
const string value = "Hi!";
var chars = value
.Select(c => (int) c)
.Select(c => $@"\u{c:x4}");
var result = string.Concat(chars);
See this fiddle for a working demo.
Here's the same approach, implemented with StringBuilder.
StringBuilder sb = new StringBuilder();
foreach (char c in s)
sb.AppendFormat("\\u{0:X4}",(uint)c);
return sb.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With