国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

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

2008-04-09 10:40 by Mainz, 13713 visits, 網(wǎ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 );

 

有時候還有這樣一些需求:

byte[] 轉(zhuǎn)成原16進制格式的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進制格式的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;
        }
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android MD5加密
SM2簽名的預(yù)處理過程
用Java來實現(xiàn)的Sniffer
C# 16進制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換
java字節(jié)、位移以及進制轉(zhuǎn)換
Android usb
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服