{"id":37943,"date":"2004-09-07T06:58:00","date_gmt":"2004-09-07T06:58:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/09\/07\/converting-a-byte-to-a-system-string\/"},"modified":"2004-09-07T06:58:00","modified_gmt":"2004-09-07T06:58:00","slug":"converting-a-byte-to-a-system-string","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040907-00\/?p=37943","title":{"rendered":"Converting a byte[] to a System.String"},"content":{"rendered":"<p>For some reason, this question gets asked a lot. How do I convert a byte[] to a System.String? (Yes, this is a CLR question. Sorry.)<\/p>\n<p> You can use <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpref\/html\/frlrfSystemTextEncodingClassGetStringTopic1.asp\"> String System.Text.UnicodeEncoding.GetString()<\/a> which takes a byte[] array and produces a string. <\/p>\n<p> Note that this is not the same as just blindly copying the bytes from the byte[] array into a hunk of memory and calling it a string. The GetString() method must validate the bytes and forbid invalid surrogates, for example. <\/p>\n<p> You might be tempted to create a string and just mash the bytes into it, but that violates string immutability <a href=\"http:\/\/weblogs.asp.net\/brada\/archive\/2004\/06\/03\/148144.aspx\"> and can lead to subtle problems<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>For some reason, this question gets asked a lot. How do I convert a byte[] to a System.String? (Yes, this is a CLR question. Sorry.) You can use String System.Text.UnicodeEncoding.GetString() which takes a byte[] array and produces a string. Note that this is not the same as just blindly copying the bytes from the byte[] [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-37943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>For some reason, this question gets asked a lot. How do I convert a byte[] to a System.String? (Yes, this is a CLR question. Sorry.) You can use String System.Text.UnicodeEncoding.GetString() which takes a byte[] array and produces a string. Note that this is not the same as just blindly copying the bytes from the byte[] [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=37943"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=37943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=37943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=37943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}