简体   繁体   中英

Convert escaped unicode string to bytearray

My input string consists of a mixture of unicode escape characters with regular characters mixed in. Example:

String input ="\u0000\u0003\u0000\u0013timestamp\u0011clientId\u0015timeToLive\u0017destination\u000fheaders\tbody\u0013messageId\u0001\u0006"

How can I convert this into a bytearray or Stream?

Expected output is Byte[]

//                         t     i     m     e     s     t     a     m     p
{0x00, 0x03, 0x00, 0x13, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x11, ...}

This seems to work:

Encoding.UTF8.GetBytes(input);

You can try it using:

Text = BitConverter.ToString(Encoding.UTF8.GetBytes(input));

It seems you can simply cast each character to its equivalent byte value.

You don't say how to handle unicode characters with a value > 255, but assuming you don't have any of those:

input.Select(c => (byte)c).ToArray();

Note that for your specific example, Encoding.UTF8.GetBytes(input) will produce the exact same byte array.

However, you're not saying you want the string UTF8 encoded, and since you're not showing unicode code points above 255, it's hard to tell exactly what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM