五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > string和byte[]的轉(zhuǎn)換 (C#)

string和byte[]的轉(zhuǎn)換 (C#)

文章來源:365jz.com     點(diǎn)擊數(shù):1956    更新時(shí)間:2009-09-18 10:32   參與評(píng)論
 string類型轉(zhuǎn)成byte[]

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

反過來,byte[]轉(zhuǎn)成string

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


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

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

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

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

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":

        public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "
        {
            
string hexString = string.Empty;
            
if ( bytes != null )
            
{
                StringBuilder strB 
= new StringBuilder ();

                
for ( int i = 0; i < bytes.Length; i++ )
                
{
                    strB.Append ( bytes[i].ToString ( 
"X2" ) );
                }

                hexString 
= strB.ToString ();
            }

            
return hexString;
        }


反過來,16進(jìn)制格式的string 轉(zhuǎn)成byte[],例如, "ae00cf"轉(zhuǎn)換成0xae00cf,長度縮減一半;"3031" 轉(zhuǎn)成new byte[]{ 0x30, 0x31}:

        public static byte[] GetBytes(string hexString, out int discarded)
        
{
            discarded 
= 0;
            
string newString = "";
            
char c;
            
// remove all none A-F, 0-9, characters
            for (int i=0; i<hexString.Length; i++)
            
{
                c 
= hexString[i];
                
if (IsHexDigit(c))
                    newString 
+= c;
                
else
                    discarded
++;
            }

            
// if odd number of characters, discard last character
            if (newString.Length % 2 != 0)
            
{
                discarded
++;
                newString 
= newString.Substring(0, newString.Length-1);
            }


            
int byteLength = newString.Length / 2;
            
byte[] bytes = new byte[byteLength];
            
string hex;
            
int j = 0;
            
for (int i=0; i<bytes.Length; i++)
            
{
                hex 
= new String(new Char[] {newString[j], newString[j+1]});
                bytes[i] 
= HexToByte(hex);
                j 
= j+2;
            }

            
return bytes;
        }

 

返回 返回

如對(duì)本文有疑問,請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答?。?點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (1956人查看,0條評(píng)論)
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)