从固定字节 [] 数组中读取字节

问题描述

我需要使用一个包含结构体的 DLL:

unsafe public struct RESPONSE2
    {
        // some other stuff...
        public THERSP TheRsp;
        // some other stuff...
    };

unsafe public struct THERSP
    {
        // some other stuff...
        public TRANSACTIONINFO Transaction;
    };

unsafe public struct TRANSACTIONINFO
    {
        // some other stuff...
        public fixed byte CardName[17];
        // some other stuff...
    }

现在,当我得到信息时,我需要从 TRANSACTIONINFO 读取信息。 DLL 具有获取响应的功能,所以我得到的响应如下

RESPONSE2 rsp = new RESPONSE2();
int x = getRsp(ref rsp);

使用 DLL

[DllImport("thedll.dll",CallingConvention = CallingConvention.StdCall)]
public static extern int getRsp(ref RESPONSE2 Response);

在这工作正常,但我无法从 TRANSACTIONINFO.CardName 中读出字节

我试过狐狸的例子: 我期望的解码值是 VISA。

1.

var name = Marshal.PtrToStringAnsi(new IntPtr(rsp.TheRsp.Transaction.CardName));

这给了我“V”的结果,其他字节都丢失了。

2.

byte[] test_card = new byte[17];
int i = 0;
while (i < 17)
{
   test_card[i] = rsp.TheRsp.Transaction.CardName[i];
   i++;
}

这正确地给了我第一个字节,但所有其他字节都是 0x00。

3.

unsafe
            {
                byte* ptr = rsp.TheRsp.Transaction.CardName;
                {
                    byte[] bytes = new byte[17];
                    int index = 0;
                    byte* counter = ptr;
                    while(*counter != 0x00)
                    {
                        bytes[index] = *counter;
                        index++;
                        counter++;
                    }
                    string name = Encoding.ASCII.GetString(bytes,17);
                }
            }

这也给了我结果“V”。

那么问题是,我如何读取所有 CardName 字节?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)