Methods for converting between byte[] and string in C # _c# Tutorials

Source: Internet
Author: User

This article describes how to convert between byte[] and string.

Bit (b): The bit is only 0 1, 1 represents a pulse, and 0 represents no pulse. It is the most basic unit of computer physical memory storage.

Byte (B): 8 bits, 0-255 integer representation

Encoding: The character must be encoded before it can be processed by the computer. Early computers used 7 for ASCII encoding, and Chinese Simplified GB2312 and Big5 were designed to handle Chinese characters.

The conversion between a string and a byte array is, in fact, a conversion between the real world information and the digital world information, which is bound to involve some kind of encoding, and different encoding methods will result in different conversion results. System.Text.Encoding is often used in C # to manage commonly used encodings. The following direct code:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks; Namespace Bytetostring {class Program {static void Main (string[] args) {string str = "Sungozhen handsome!"
";
Use UTF encoding ...
byte[] UTF8 = strtobyte (str, ENCODING.UTF8);
It is estimated that C # was not designed at the time of Chinese Simplified, this is later in China?
byte[] gb2312 = Strtobyte (str,encoding.getencoding ("GB2312")); Console.WriteLine ("This is UTF8 (Sungozhen handsome), length is: {0}", UTF8.
Length);
foreach (var item in UTF8) {Console.Write (item);} Console.WriteLine ("N ' gb2312 (Sungozhen handsome), length is: {0}", gb2312.
Length); 
foreach (var item in gb2312) {Console.Write (item)}/////////UTF8 encoded byte array to str string utf8str = Bytetostr (Utf8,encoding.utf8);
String gb2312str = Bytetostr (gb2312,encoding.getencoding ("GB2312"));
Console.WriteLine ("\n\nutf8: {0}", UTF8STR);
Console.WriteLine ("gb2312: {0}", GB2312STR);
Console.readkey (); //c# typically uses System.Text.Encoding encoding//string-array static byte[] Strtobyte (String str, Encoding Encoding) {return Encoding.
GetBytes (str); }//Array conversion string static string Bytetostr byTe[] bt,encoding Encoding) {return Encoding.
GetString (BT); }
}
}

The above is a small series to introduce the C # byte[] and string conversion between the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.