2014-12-21 53 views
0

我在c#中編程並嘗試將控制檯輸入轉換爲十六進制。 輸入是1-256(前125) 之間的數字轉換後的數字應該是這樣的:將ASCII控制檯輸入轉換爲十六進制

fpr 125: 0x31, 0x32, 0x35 

我已經嘗試過使用來解決我的問題時間:

byte[] array = Encoding.ASCII.GetBytes(Senke) 

但它總是顯示我byte[]

我需要這種轉換用於創建APDU通過使用智能卡我的應用程序的最終APDU看起來像這樣寫我的智能卡信息:

{ 0xFF, 0xD6, 0x00, 0x02, 0x10, 0x31, 0x32, 0x35} 

我希望有人能幫助我。

+0

我編輯了我的答案,看看它是否有幫助。 – Eric

回答

0

爲整數轉換爲十六進制,使用:(更多信息可發現here

int devValue = 211; 
string hexValue = decValue.ToString("X"); 

爲了進一步詳細描述,下面將產生所需輸出:

string input = "125"; // your input, could be replaced with Console.ReadLine() 

foreach (char c in input) { 
    int decValue = (int)c; // Convert ASCII character to an integer 
    string hexValue = decValue.ToString("X"); // Convert the integer to hex value 

    Console.WriteLine(hexValue); 
} 

代碼會產生以下輸出:

31 
32 
35 
+0

到目前爲止,謝謝你,現在我得到的輸出沒有0x,我需要它爲這個功能,我需要爲每個數字一個字節作爲變量,例如a = 0x31,b = 0x32,c = 0,35使用它在此APDU字節[] WriteAPDU = {0xFF,0xD6,0x00,0x02,0x10,0x31,0x32,0x35} – Matt

0

這裏是一個例子:

int d = 65; // Capital 'A' 

string h= d.ToString("X"); // to hex 
int d2 = int.Parse(h, System.Globalization.NumberStyles.HexNumber); //to ASCII 
相關問題