我想转换C#代码java代码

时间:2018-06-13 07:18:27

标签: java c#

我试图将一些C#代码转换为Java,它会将命令发送到vx805终端,我遇到了一个调用此方法的行:

public void EMVCardReadRequest() {
    currentCommandName = CommandType.C30;

    //header C30
    byte[] Data = new byte[10];
    byte[] commandByte = new byte[100];

    ASCIIEncoding.Default.GetBytes(CommandType.C30.ToString(), 0, 3, commandByte, 0);
    int totalLength = 3, commandLength = 0;

    // Skip 2 length for total Length bytes
    totalLength += 2;

    //TimeOut
    commandLength = PackCommandByte(120, Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;

    //Terminal record
    Data = new byte[10];
    commandLength = PackCommandByte(1, Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;

    //Date: C1<len><YYMMDD> 
    Data = new byte[10];
    string date = DateTime.Now.ToString("yyMMdd");
    commandLength = PackCommandByte(date, Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;

    //time: C1<len><HHMMSS> 
    Data = new byte[10];
    string time = DateTime.Now.ToString("HHmmss");
    commandLength = PackCommandByte(time, Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;


    //C1<len><txn type> 
    Data = new byte[10];
    commandLength = PackCommandByte(0, Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;


    //C1<len><Language Code>
    Data = new byte[10];
    commandLength = PackCommandByte('1', Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;

    //C1<len><curr code>
    Data = new byte[10];
    commandLength = PackCommandByte(string.Empty, Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;

    //// C1<len><amt> 
    Data = new byte[10];
    commandLength = PackCommandByte("400", Data);
    Data.CopyTo(commandByte, totalLength);
    totalLength += commandLength;

    //add total length            
    byte[] bytes = BitConverter.GetBytes(totalLength - 5);
    if (BitConverter.IsLittleEndian)
        Array.Reverse(bytes);

    for (int i = 2; i < 4; i++) {
        commandByte[i + 1] = bytes[i];
    }


    SendPINPadBytes(commandByte, totalLength);
}

和另一种方法。

private int SendPINPadBytes(byte[] msg, int Len) {
    byte LRC;
    int retry, bLen = 0, i = 0;
    byte[] bmsg = new byte[4096];
    bmsg[bLen] = (byte) ConstraintValue.STX;
    bLen += 1;
    msg.CopyTo(bmsg, 1);
    bLen += Len;
    bmsg[bLen] = (byte) ConstraintValue.ETX;
    bLen += 1;
    retry = 3;
    LRC = CalculateLRCByte(bmsg, bLen);
    bmsg[bLen] = LRC;
    bLen += 1;
    //string ToSend =  + msg + ETX + (char)LRC;

    while (retry > 0) {
        comPort.DiscardInBuffer();
        comPort.Write(bmsg, 0, bLen);
        char ch = ReadOneChar(1000);
        if (ch == ConstraintValue.ACK)
            break;
        if (ch == ConstraintValue.EOT)
            return -2;
        retry--;
    }
    if (retry > 0)
        return 0;
    else
        return -1;
}
//Calculate LRC for byte array
private byte CalculateLRCByte(byte[] msg, int len) {
    byte LRC = 0;
    int i = 0;
    foreach(byte c in msg) {
        i++;
        if (c == ConstraintValue.STX)
            continue;
        LRC = (byte)(LRC ^ c);

        if (i >= len)
            break;
    }
    return LRC;
}

和equilvant java代码。

public void emvCardReadRequest() throws Exception, IOException {
    currentCommandName = CommandType.C30;

    //header C30
    byte[] Data = new byte[10];
    byte[] commandByte = new byte[100];

    commandByte = CommandType.C30.toString().getBytes("StandardCharsets.US_ASCII"); //US-ASCII
    int totalLength = 3, commandLength = 0;

    // Skip 2 length for total Length bytes
    totalLength += 2;

    //TimeOut
    commandLength = packCommandByte(120, Data);
    // Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;

    //Terminal record
    Data = new byte[10];
    commandLength = packCommandByte(1, Data);
    // Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;

    //Date: C1<len><YYMMDD> 
    Data = new byte[10];
    // String date = DateTime.Now.toString("yyMMdd");
    String date = DateTime.now().toString("yyMMdd");
    commandLength = packCommandByte(date, Data);
    //Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;

    //time: C1<len><HHMMSS> 
    Data = new byte[10];
    //String time = DateTime.Now.ToString("HHmmss");
    String time = DateTime.now().toString("HHmmss");
    commandLength = packCommandByte(time, Data);
    //Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;


    //C1<len><txn type> 
    Data = new byte[10];
    commandLength = packCommandByte(0, Data);
    //Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;


    //C1<len><Language Code>
    Data = new byte[10];
    commandLength = packCommandByte('1', Data);
    //Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;

    //C1<len><curr code>
    Data = new byte[10];
    // commandLength = PackCommandByte(String.Empty, Data);
    commandLength = packCommandByte(0, Data);
    //Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;

    //// C1<len><amt> 
    Data = new byte[10];
    commandLength = packCommandByte(1.00, Data);
    //Data.CopyTo(commandByte, totalLength);
    Data = Arrays.copyOf(commandByte, totalLength);
    totalLength += commandLength;

    //add total length            
    // byte[] bytes = BitConverter.GetBytes(totalLength - 5);
    // if(BitConverter.IsLittleEndian)
    //     Array.Reverse(bytes);
    // byte[] bytes = currentCommandName.getBytes(totalLength - 5);
    byte[] bytes = new byte[totalLength - 5];
    if (!ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) {
        ArrayUtils.reverse(bytes);
    }
    for (int i = 0; i < 2; i++) {
        commandByte[i + 1] = bytes[i];
    }
    System.out.print(String.valueOf(commandByte));
    sendPinPadBytes(commandByte, totalLength);

}

我正在一个项目中工作,其中我要求将命令发送到VX805设备用于EMV卡,当我发送命令时它将显示插入卡并在插入显示处理和交易批准的卡后。

我试图想方设法用Java做同样的事情而没有运气。我怎样才能模仿Java中的相同行为?

感谢任何帮助..

0 个答案:

没有答案