0

我开始了获取 Mifare 1k Classic Cards 的 UID 的项目——这真的很好用。现在我必须把我的 UID 放到一个 mySQL 数据库中。我将我的 ByteArray 转换为字符串 - 这也有效。

private string getcardUID()                                             
{
    string cardUID = "";
    byte[] receivedUID = new byte[256];
    Card.SCARD_IO_REQUEST request = new Card.SCARD_IO_REQUEST();
    request.dwProtocol = Card.SCARD_PROTOCOL_T1;
    request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Card.SCARD_IO_REQUEST));
    byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 };     
    int outBytes = receivedUID.Length;
    int status = Card.SCardTransmit(hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);

    if (status != Card.SCARD_S_SUCCESS)
    {
        cardUID = "Error";
    }
    else
    {
       cardUID = BitConverter.ToString(receivedUID.Take(4).ToArray()).Replace("-", string.Empty).ToUpper();
    }

    return cardUID;

正如我们所知,我无法将最终看起来像 0B05C2FD 的字符串放入我的数据库中。尝试了不同的数据类型,如 INT BIGINT 和 VARCHAR:

MySqlConnection connection = new MySqlConnection(MyConnectionString);
        MySqlCommand cmd;
        connection.Open();
        string mycardUID = getcardUID();
        int mycardUIDint = Convert.ToInt32(mycardUID, 16);
        try
        {
            cmd = connection.CreateCommand();
            cmd.CommandText = "INSERT INTO terminal(id,uid,buttoncode,timestamp,transfered,ts)VALUES(@id,@uid,@buttoncode,@timestamp,@transfered,@ts)";
            cmd.Parameters.AddWithValue("@id", "");
            cmd.Parameters.AddWithValue("@uid", mycardUID);
            cmd.Parameters.AddWithValue("@timestamp", DateTime.Now);
            cmd.Parameters.AddWithValue("@buttoncode", "1");
            cmd.Parameters.AddWithValue("@transfered", "");
            cmd.Parameters.AddWithValue("@ts", "");
            cmd.ExecuteNonQuery();
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

有解决办法吗?我试图转换为 Int 但这不可能使用 Convert.ToInt32(value, 16); 输入错误<.<

编辑:更改的代码添加了 Convert.ToInt32

编辑:我发现了一些东西(c#bitconverter.ToString 转换为十六进制字符串),但我不确定我是否能够将它应用到我的程序中,我真的被提问的人弄糊涂了,我对答案更加困惑:(

4

0 回答 0