转换逃脱字符串ByteArray或流; C# [英] Convert escaped string to bytearray or stream; c#

查看:563
本文介绍了转换逃脱字符串ByteArray或流; C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入字符串包括在混合与常规的Unicode字符转义字符的混合物,例如:

  \\\\\ \\\\\\\timestamp\\\clientId\\\timeToLive\\\destination\\\headers\tbody\\\messageId\\\\\\ 

我怎么可以转换这个转换为bytearray或流



编辑:UTF + 8编码。为了澄清输入字符串:

 字符01:U + 0000 
字符02:U + 0003
字符03:U + 0000
字符04:U + 0013
字符05:T已
字符06:我
字符07:M
字符08:电子
字符09:■
字符10:T已
字符11:一个
字符12:M
字符13:p
字符14:U + 0011
...
...


解决方案

好了,你已经得到了一个任意字符串(它包含非打印字符其实是无关紧要的),并且希望将其转换成使用UTF-8字节数组。这很简单:)

 字节[]字节= Encoding.UTF8.GetBytes(文本); 

或者写一个流,你通常包装在一个 StreamWriter的

  //需要注意的是,由于使用的语句,这将在关闭流使用块
(VAR作家=新的StreamWriter(流))
{
writer.Write(文本)结束
//;
}



(UTF-8是默认编码的StreamWriter ,但可以明确的过程中指定。)



我假设你真的有一个很好的理由有文在这虽然形成。我不能说我曾经发现一个使用了U + 0003(文本结束)。如果像I4V曾建议,这一数据在二进制流本来,你应该避免处理它作为摆在首位的文本。从您的文本数据分离出来的二进制数据 - 当你将它们混合,它的将会的导致问题。 (例如,如果你的字符串中的第四个字符是U + 00FF,那么,当编码成UTF-8,这可能不会是你想要的最后两个字节)。


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

\u0000\u0003\u0000\u0013timestamp\u0011clientId\u0015timeToLive\u0017destination\u000fheaders\tbody\u0013messageId\u0001\u0006

How can I convert this into a bytearray or Stream?

EDIT: UTF+8 encoding. To clarify the input string:

Char 01: U+0000
Char 02: U+0003
Char 03: U+0000
Char 04: U+0013
Char 05: t
Char 06: i
Char 07: m
Char 08: e
Char 09: s
Char 10: t
Char 11: a
Char 12: m
Char 13: p
Char 14: U+0011
...
...    

解决方案

Okay, so you've got an arbitrary string (the fact that it contains non-printable characters is irrelevant) and you want to convert it into a byte array using UTF-8. That's easy :)

byte[] bytes = Encoding.UTF8.GetBytes(text);

Or to write to a stream, you'd normally wrap it in a StreamWriter:

// Note that due to the using statement, this will close the stream at the end
// of the block
using (var writer = new StreamWriter(stream))
{
    writer.Write(text);
}

(UTF-8 is the default encoding for StreamWriter, but you can specify it explicitly of course.)

I'm assuming you really have a good reason to have "text" in this form though. I can't say I've ever found a use for U+0003 (END OF TEXT). If, as I4V has suggested, this data was originally in a binary stream, you should avoid handling it as text in the first place. Separate out your binary data from your text data - when you mix them, it will cause issues. (For example, if the fourth character in your string were U+00FF, it would end up as two bytes when encoded to UTF-8, which probably wouldn't be what you wanted.)

这篇关于转换逃脱字符串ByteArray或流; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆