在C# 中將byte轉換為int和int轉換為byte


     在各種系統交互的時候,需要調用第三方動態庫;會將有一些參數類型聲明成byte,才能調用,尤其是調用dephi的動態庫。

有時候回用到byte和各數據類型的轉換。

下面列出這些轉換的方法。

 第一種方法:

          

   byte[] k = { 225,7,0,0 };

            int g = System.BitConverter.ToInt32(k,0);

  

     如果轉換成其他類型請查看System.BitConverter的其他方法。

第二中方法:

  

int h = bytesToInt(k, 0);

  

轉換成其他參數類型自己定義位移。

 /** 
            * byte數組中取int數值,本方法適用於(低位在前,高位在后)的順序,和和intToBytes()配套使用
            *  
            * @param src 
            *            byte數組 
            * @param offset 
            *            從數組的第offset位開始 
            * @return int數值 
            */
        public static int bytesToInt(byte[] src, int offset)
        {
            int value;
            value = (int)((src[offset] & 0xFF)
                    | ((src[offset + 1] & 0xFF) << 8)
                    | ((src[offset + 2] & 0xFF) << 16)
                    | ((src[offset + 3] & 0xFF) << 24));
            return value;
        }

        /** 
        * byte數組中取int數值,本方法適用於(低位在后,高位在前)的順序。和intToBytes2()配套使用
        */
        public static int bytesToInt2(byte[] src, int offset)
        {
            int value;
            value = (int)(((src[offset] & 0xFF) << 24)
                    | ((src[offset + 1] & 0xFF) << 16)
                    | ((src[offset + 2] & 0xFF) << 8)
                    | (src[offset + 3] & 0xFF));
            return value;
        }

 

 /** 
     * 將int數值轉換為占四個字節的byte數組,本方法適用於(低位在前,高位在后)的順序。 和bytesToInt()配套使用
     * @param value 
     *            要轉換的int值
     * @return byte數組
     */  
	public static byte[] intToBytes( int value ) 
	{ 
		byte[] src = new byte[4];
		src[3] =  (byte) ((value>>24) & 0xFF);
		src[2] =  (byte) ((value>>16) & 0xFF);
		src[1] =  (byte) ((value>>8) & 0xFF);  
		src[0] =  (byte) (value & 0xFF);				
		return src; 
	}
	 /** 
     * 將int數值轉換為占四個字節的byte數組,本方法適用於(高位在前,低位在后)的順序。  和bytesToInt2()配套使用
     */  
	public static byte[] intToBytes2(int value) 
	{ 
		byte[] src = new byte[4];
		src[0] = (byte) ((value>>24) & 0xFF);
		src[1] = (byte) ((value>>16)& 0xFF);
		src[2] = (byte) ((value>>8)&0xFF);  
		src[3] = (byte) (value & 0xFF);		
		return src;
	}

  

 其他轉換方法:

string類型轉成byte[]:

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


byte[]轉成string:

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


string類型轉成ASCII byte[]:

("01" 轉成 byte[] = new byte[]{ 0x30,0x31})

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


ASCIIbyte[]轉成string:

(byte[] = new byte[]{ 0x30, 0x31} 轉成"01")

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


byte[]轉16進制格式string:

new byte[]{ 0x30, 0x31}轉成"3031":

publicstaticstring 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 轉byte[]:

publicstaticbyte[] GetBytes(string hexString, outint discarded)

{            

discarded = 0;

string newString = "";

char c;// remove all none A-F, 0-9, charactersfor (int i=0; i<hexString.Length; i++)

{              

  c = hexString[i];if (IsHexDigit(c))                    

newString += c;

else                    

discarded++;            

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

newString = newString.Substring(0, newString.Length-1);            }

int byteLength = newString.Length / 2;byte[] bytes = newbyte[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;       

 }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM