unicode - Convert escaped string to bytearray or stream; c# -


my input string consists of mixture of unicode escape characters regular characters mixed in. example:

\u0000\u0003\u0000\u0013timestamp\u0011clientid\u0015timetolive\u0017destination\u000fheaders\tbody\u0013messageid\u0001\u0006 

how can convert bytearray or stream?

edit: utf+8 encoding. clarify input string:

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

okay, you've got arbitrary string (the fact contains non-printable characters irrelevant) , want convert byte array using utf-8. that's easy :)

byte[] bytes = encoding.utf8.getbytes(text); 

or write stream, you'd wrap in streamwriter:

// note due using statement, close stream @ end // of block using (var writer = new streamwriter(stream)) {     writer.write(text); } 

(utf-8 default encoding streamwriter, can specify explicitly of course.)

i'm assuming have reason have "text" in form though. can't i've ever found use u+0003 (end of text). if, i4v has suggested, data in binary stream, should avoid handling text in first place. separate out binary data text data - when mix them, will cause issues. (for example, if fourth character in string u+00ff, end 2 bytes when encoded utf-8, wouldn't wanted.)


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -