c#中将整数转化为字符串_在C#中将字符串转换为字节数组
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 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()方法將字符串轉換為字節數組 ,它接受字符串作為參數并返回字節數組。
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 [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#中將整數轉化為字符串
總結
以上是生活随笔為你收集整理的c#中将整数转化为字符串_在C#中将字符串转换为字节数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python饼形图_Python | 饼
- 下一篇: 如何使用JavaScript访问对象的键