数字值到字节[6]数组读卡器

时间:2015-06-17 10:05:52

标签: c# smartcard smartcard-reader cardreader

byte[6]中将数字转换为C#的最佳方式是什么?

我正在使用MagTek Card reader并尝试在设备屏幕上显示所需的金额,它应该是6-byte数组。需要使用和授权金额EMV Tag 9F02format n12

功能:

 int requestSmartCard(int cardType, int comfirmationTime, int pinEnteringTime, int beepTones, int option, byte [] amount, int transactionType, byte[] cashback, byte [] reserved);

金额参数的描述是: - 金额使用和授权的金额,EMV标签9F02,格式n12。它应该是一个6字节的数组。

编辑:

这是他们在C#中的示例的示例代码:

          byte []amount = new byte[6];
          amount[3] = 1;
          byte []cashBack = new byte[6];

          PrintMsg(String.format("start a emv transaction"));
          byte reserved[]  = new byte[26];

          byte cardType = 2;
          byte confirmWaitTime = 20;
          byte pinWaitTime = 20;
          byte tone = 1;
          byte option = 0;
          byte transType = 4;
          retCode = m_MTSCRA.requestSmartCard(cardType, confirmWaitTime, pinWaitTime, tone, option, amount, transType, cashBack, reserved);

然后在设备的屏幕上显示100.00 $。

编辑: 我将问题形式float更改为byte [6],将number更改为byte [6]。

2 个答案:

答案 0 :(得分:2)

该方法应该是这样的:

public static byte[] NumberToByteArray(float f, int decimals)
{
    // A string in the format 0000000000.00 for example
    string format = new string('0', 12 - decimals) + "." + new string('0', decimals);

    // We format the number f, removing the decimal separator
    string str = f.ToString(format, CultureInfo.InvariantCulture).Replace(".", string.Empty);

    if (str.Length != 12)
    {
        throw new ArgumentException("f");
    }

    var bytes = new byte[6];

    for (int i = 0; i < 6; i++)
    {
        // For each group of two digits, the first one is shifted by
        // 4 binary places
        int digit1 = str[i * 2] - '0';
        bytes[i] = (byte)(digit1 << 4);

        // And the second digit is "added" with the logical | (or)
        int digit2 = str[(i * 2) + 1] - '0';
        bytes[i] |= (byte)digit2;
    }

    return bytes;
}

注意(1)它不是一种优化方法。我可以乘以10^decimals代替,但我不想做无用的乘法。

注意(2)你不应该真正使用floatfloat精度为6或7位(幸运时),而该格式可存储12位数。使用double甚至更好decimal。您可以将float替换为decimal,它将正常运行。

注意(3)您需要使用小数位数。 2欧元或美元,但对于其他货币,可能会有所不同。

注意(4)这是从数字转换为BCD的问题。它可以在不经过字符串的情况下制作。太懒了(我不想在float进行乘法运算)

答案 1 :(得分:0)

某些处理器未在EMV内核标签9F02中发送预定金额时,可能会从银行返回“ Do No Honor”错误消息,这是一个普遍错误。

通过发送标签9F02交易中的最终/费用金额获得批准。

这两种情况的结果似乎表明您的处理器正在根据最终金额验证标签9F02。

这违反了签证规定。

https://www.visa.com/chip/merchants/grow-your-business/payment-technologies/credit-card-chip/docs/quick-chip-emv-specification.pdf

enter image description here

xanatos的上述解决方案可以解决问题。我也测试过。