c#中将整数转化为字符串
Prerequisite:How to declare and use byte[] in C#?
先决条件:如何在C#中声明和使用byte []?
C#中的字符串到字节数组的转换 (String to Byte Array Conversion in C#)
In C#, it is possible that astring can be converted to a byte arrayby using Encoding.ASCII.GetBytes() method, it accepts a string as a parameter and returns a byte array.
在C#中,可以使用Encoding.ASCII.GetBytes()方法将字符串转换为字节数组,它接受字符串作为参数并返回字节数组。
Note:In C#, the string contains two bytes per character; the method converts it into 1 byte. Still, sometimes it is possible to lose the data.
注意:在C#中,字符串每个字符包含两个字节;每个字符包含两个字节。 该方法将其转换为1个字节。 但是,有时仍可能会丢失数据。
Syntax:
句法:
Method Encoding.ASCII.GetBytes() contains various overloaded methods, here we are using the following method type...
方法Encoding.ASCII.GetBytes()包含各种重载方法,这里我们使用以下方法类型...
byte[] Encoding.ASCII.GetBytes(String_Object);
Example:
例:
This example contains a constant string and converting it to byte[]
本示例包含一个常量字符串,并将其转换为byte []
using System;using System.Text;namespace Test{class Program{static void Main(string[] args){string str = "Hello World! I am [emailprotected]";//reading all characters as byte and storing them to byte[]byte[] barr = Encoding.ASCII.GetBytes(str);//printing characters with byte valuesfor(int loop =0; loop<barr.Length-1; loop++){Console.WriteLine("Byte of char \'" + str[loop] + "\' : " + barr[loop]);}//hit ENTER to exitConsole.ReadLine();}}}
Output
输出量
Byte of char 'H' : 72Byte of char 'e' : 101Byte of char 'l' : 108Byte of char 'l' : 108Byte of char 'o' : 111Byte of char ' ' : 32Byte of char 'W' : 87Byte of char 'o' : 111Byte of char 'r' : 114Byte of char 'l' : 108Byte of char 'd' : 100Byte of char '!' : 33Byte of char ' ' : 32Byte of char 'I' : 73Byte of char ' ' : 32Byte of char 'a' : 97Byte of char 'm' : 109Byte of char ' ' : 32Byte of char 'I' : 73Byte of char 'n' : 110Byte of char 'c' : 99Byte of char 'l' : 108Byte of char 'u' : 117Byte of char 'd' : 100Byte of char 'e' : 101Byte of char 'H' : 72Byte of char 'e' : 101Byte of char 'l' : 108Byte of char 'p' : 112Byte of char '@' : 64Byte of char '1' : 49Byte of char '2' : 50Byte of char '3' : 51
翻译自: /dot-net/convert-string-to-byte-array-in-c-sharp.aspx
c#中将整数转化为字符串