简体   繁体   English

将转义的unicode字符串转换为bytearray

[英]Convert escaped unicode string to bytearray

My input string consists of a mixture of unicode escape characters with regular characters mixed in. Example: 我的输入字符串由Unicode转义字符和常规字符混合而成。示例:

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[] 预期输出为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: 您没有说明如何处理值大于255的unicode字符,但是假设您没有以下任何一个:

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

Note that for your specific example, Encoding.UTF8.GetBytes(input) will produce the exact same byte array. 请注意,对于您的特定示例, Encoding.UTF8.GetBytes(input)将产生完全相同的字节数组。

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. 但是,您并不是说要对字符串UTF8进行编码,并且由于您没有显示255以上的unicode代码点,因此很难准确说明您想要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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