2014-12-21 140 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 
相关问题