c#中将整数转化为字符串_在C#中将字符串转换为字节数组
c#中將整數(shù)轉(zhuǎn)化為字符串
Prerequisite: How to declare and use byte[] in C#?
先決條件: 如何在C#中聲明和使用byte []?
C#中的字符串到字節(jié)數(shù)組的轉(zhuǎn)換 (String to Byte Array Conversion in C#)
In C#, it is possible that a string can be converted to a byte array by using Encoding.ASCII.GetBytes() method, it accepts a string as a parameter and returns a byte array.
在C#中, 可以使用Encoding.ASCII.GetBytes()方法將字符串轉(zhuǎn)換為字節(jié)數(shù)組 ,它接受字符串作為參數(shù)并返回字節(jié)數(shù)組。
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#中,字符串每個(gè)字符包含兩個(gè)字節(jié);每個(gè)字符包含兩個(gè)字節(jié)。 該方法將其轉(zhuǎn)換為1個(gè)字節(jié)。 但是,有時(shí)仍可能會(huì)丟失數(shù)據(jù)。
Syntax:
句法:
Method Encoding.ASCII.GetBytes() contains various overloaded methods, here we are using the following method type...
方法Encoding.ASCII.GetBytes()包含各種重載方法,這里我們使用以下方法類(lèi)型...
byte[] Encoding.ASCII.GetBytes(String_Object);Example:
例:
This example contains a constant string and converting it to byte[]
本示例包含一個(gè)常量字符串,并將其轉(zhuǎn)換為byte []
using System; using System.Text;namespace Test {class Program{static void Main(string[] args){string str = "Hello World! I am [email?protected]";//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' : 72 Byte of char 'e' : 101 Byte of char 'l' : 108 Byte of char 'l' : 108 Byte of char 'o' : 111 Byte of char ' ' : 32 Byte of char 'W' : 87 Byte of char 'o' : 111 Byte of char 'r' : 114 Byte of char 'l' : 108 Byte of char 'd' : 100 Byte of char '!' : 33 Byte of char ' ' : 32 Byte of char 'I' : 73 Byte of char ' ' : 32 Byte of char 'a' : 97 Byte of char 'm' : 109 Byte of char ' ' : 32 Byte of char 'I' : 73 Byte of char 'n' : 110 Byte of char 'c' : 99 Byte of char 'l' : 108 Byte of char 'u' : 117 Byte of char 'd' : 100 Byte of char 'e' : 101 Byte of char 'H' : 72 Byte of char 'e' : 101 Byte of char 'l' : 108 Byte of char 'p' : 112 Byte of char '@' : 64 Byte of char '1' : 49 Byte of char '2' : 50 Byte of char '3' : 51翻譯自: https://www.includehelp.com/dot-net/convert-string-to-byte-array-in-c-sharp.aspx
c#中將整數(shù)轉(zhuǎn)化為字符串
總結(jié)
以上是生活随笔為你收集整理的c#中将整数转化为字符串_在C#中将字符串转换为字节数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python饼形图_Python | 饼
- 下一篇: c#中的long类型示例_C#中带示例的