自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# string byte數(shù)組轉(zhuǎn)換解析

開(kāi)發(fā) 后端
C# string byte數(shù)組轉(zhuǎn)換是我們實(shí)際工作中所遇到的需求,那么如何實(shí)現(xiàn)C# string byte數(shù)組轉(zhuǎn)換呢?具體的內(nèi)容是什么呢?本文就向你介紹詳細(xì)的內(nèi)容。

C# string byte數(shù)組轉(zhuǎn)換實(shí)現(xiàn)的過(guò)程是什么呢?C# string byte數(shù)組間的轉(zhuǎn)換需要注意什么呢?C# string byte數(shù)組間轉(zhuǎn)換所涉及的方法是什么呢?讓我們來(lái)看看具體的內(nèi)容:

C# string byte數(shù)組轉(zhuǎn)換之string類(lèi)型轉(zhuǎn)成byte[]:

byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

反過(guò)來(lái),byte[]轉(zhuǎn)成string:

string str = System.Text.Encoding.Default.GetString ( byteArray );

其它編碼方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如:

string類(lèi)型轉(zhuǎn)成ASCII byte[]:("01" 轉(zhuǎn)成 byte[] = new byte[]{ 0x30, 0x31})

  1. byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str ); 

ASCII byte[] 轉(zhuǎn)成string:(byte[] = new byte[]{ 0x30, 0x31} 轉(zhuǎn)成 "01")

  1. string str = System.Text.Encoding.ASCII.GetString ( byteArray ); 

有時(shí)候還有這樣一些需求:

byte[] 轉(zhuǎn)成原16進(jìn)制格式的string,例如0xae00cf, 轉(zhuǎn)換成 "ae00cf";new byte[]{ 0x30, 0x31}轉(zhuǎn)成"3031":

  1. public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "  
  2. {  
  3. string hexString = string.Empty;  
  4. if ( bytes != null )  
  5. {  
  6. StringBuilder strB = new StringBuilder ();  
  7.  
  8. for ( int i = 0; i < bytes.Length; i++ )  
  9. {  
  10. strB.Append ( bytes[i].ToString ( "X2" ) );  
  11. }  
  12. hexString = strB.ToString ();  
  13. }  
  14. return hexString;  
  15. }  

C# string byte數(shù)組轉(zhuǎn)換之16進(jìn)制格式的string 轉(zhuǎn)成byte[]

例如, "ae00cf"轉(zhuǎn)換成0xae00cf,長(zhǎng)度縮減一半;"3031" 轉(zhuǎn)成new byte[]{ 0x30, 0x31}:

  1. public static byte[] GetBytes(string hexString, out int discarded)  
  2. {  
  3. discarded = 0;  
  4. string newString = "";  
  5. char c;  
  6. // remove all none A-F, 0-9, characters  
  7. for (int i=0; i
  8. {  
  9. c = hexString[i];  
  10. if (IsHexDigit(c))  
  11. newString += c;  
  12. else 
  13. discarded++;  
  14. }  
  15. // if odd number of characters, discard last character  
  16. if (newString.Length % 2 != 0)  
  17. {  
  18. discarded++;  
  19. newString = newString.Substring(0, newString.Length-1);  
  20. }  
  21.  
  22. int byteLength = newString.Length / 2;  
  23. byte[] bytes = new byte[byteLength];  
  24. string hex;  
  25. int j = 0;  
  26. for (int i=0; i
  27. {  
  28. hex = new String(new Char[] {newString[j], newString[j+1]});  
  29. bytes[i] = HexToByte(hex);  
  30. j = j+2;  
  31. }  
  32. return bytes;  
  33. }  

C# string byte數(shù)組轉(zhuǎn)換的問(wèn)題就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# string byte數(shù)組轉(zhuǎn)換有所幫助。

【編輯推薦】

  1. C#創(chuàng)建文件的實(shí)戰(zhàn)應(yīng)用示例解析
  2. 全面解析C#創(chuàng)建XML文件的具體操作
  3. 搞定C#創(chuàng)建PDF文件的五大步驟
  4. C#創(chuàng)建一個(gè)文件的具體實(shí)現(xiàn)淺析
  5. C#打開(kāi)一個(gè)文件的操作詳解
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-08-31 14:56:32

C# Byte數(shù)組轉(zhuǎn)換

2009-08-28 14:25:57

C# byte數(shù)組

2009-08-28 10:44:46

C#字符數(shù)組轉(zhuǎn)換

2009-09-01 18:32:32

C#動(dòng)態(tài)數(shù)組

2009-09-02 16:41:56

C#聲明數(shù)組

2009-09-02 16:30:20

C#定義數(shù)組

2009-09-01 16:35:55

C#操作String數(shù)

2009-09-02 16:20:22

C#動(dòng)態(tài)創(chuàng)建數(shù)組

2009-09-02 16:14:21

C#動(dòng)態(tài)創(chuàng)建數(shù)組

2009-08-12 11:24:25

C# String對(duì)象

2009-08-28 11:09:35

C#數(shù)組初始化

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2009-09-01 17:06:20

C#命名管道

2009-08-27 13:50:08

C# StringBu

2009-09-17 16:53:15

C#數(shù)組

2009-08-26 13:07:07

C#交錯(cuò)數(shù)組

2009-08-07 11:26:53

C#數(shù)組結(jié)構(gòu)

2009-09-09 14:40:15

C# XML解析

2009-09-02 10:58:02

C#動(dòng)態(tài)數(shù)組

2009-08-24 14:20:13

C# 強(qiáng)制類(lèi)型轉(zhuǎn)換
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)