How to convert between Byte [] and String, bytestring

Source: Internet
Author: User

How to convert between Byte [] and String, bytestring

From the C # And. NET programmer interview book.


How to convert Byte [] and String?
BITs (B): bits only 0, 1, and 1 represent pulses, while 0 represents none. It is the most basic unit for storing computer physical memory.
Byte (B): 8 bits, represented by an integer ranging from 0
Encoding: The character must be encoded before it can be processed by the computer. Early computers used 7 AscII encoding. In order to process Chinese characters, they designed Chinese Simplified GB2312 and big5

Conversion between strings and byte arrays is actually a conversion between information in the real world and information in the digital world. It is bound to involve some encoding methods. Different encoding methods will lead to different conversion results. C # System. Text. Encoding is often used to manage common encodings. The following code is directly used:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace ByteToString 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 string str = "! "; 14 15 // use UTF Encoding... 16 Byte [] utf8 = StrToByte (str, Encoding. UTF8); 17 // It is estimated that C # There was no simplified Chinese language at the time of design. why did this happen in China later? 18 Byte [] gb2312 = StrToByte (str, Encoding. getEncoding ("GB2312"); 19 20 Console. writeLine ("this is UTF8 (), length: {0}", utf8.Length); 21 foreach (var item in utf8) 22 {23 Console. write (item); 24} 25 26 Console. writeLine ("\ n this is gb2312 (), length: {0}", gb2312.Length); 27 foreach (var item in gb2312) 28 {29 Console. write (item); 30} 31 32 33 // convert the UTF-8 encoded byte array to str34 string utf8Str = ByteToStr (utf8, Encoding. UTF8); 35 string gb2312Str = ByteToStr (gb2312, Encoding. getEncoding ("GB2312"); 36 37 Console. writeLine ("\ n \ nutf8: {0}", utf8Str); 38 Console. writeLine ("gb2312: {0}", gb2312Str); 39 Console. readKey (); 40 41} 42 43 44 // C # System is usually used. text. encoding 45 46 // string to array 47 static Byte [] StrToByte (string str, encoding) 48 {49 return Encoding. getBytes (str); 50} 51 52 // array conversion String 53 static String ByteToStr (Byte [] bt, Encoding encoding) 54 {55 return encoding. getString (bt); 56} 57 58} 59}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.