gpt4 book ai didi

C# 使用河豚 NET : How to convert from Uint32[] to byte[] 时出现问题

转载 作者:行者123 更新时间:2023-11-30 19:34:58 27 4
gpt4 key购买 nike

在 C# 中,我正在使用 Blowfish.NET 2.1.3 的 BlowfishECB.cs 文件(can be found here)

在C++中,不详,但大同小异。

在C++中,Initialize(blowfish)过程如下:

void cBlowFish::Initialize(BYTE key[], int keybytes)

在C#中,Initialize(blowfish)过程是一样的

public void Initialize(byte[] key, int ofs, int len) 

问题是:

这是在 C++ 中初始化 key 的方式

DWORD keyArray[2] = {0}; //declaration
...some code
blowfish.Initialize((LPBYTE)keyArray, 8);

如您所见, key 是一个包含两个 DWORDS 的数组,总共 8 个字节。

在 C# 中我这样声明它,但是我得到一个错误

BlowfishECB blowfish = new BlowfishECB();
UInt32[] keyarray = new UInt32[2];
..some code
blowfish.Initialize(keyarray, 0, 8);

错误是:

参数“1”:无法从“uint[]”转换为“byte[]”

我做错了什么?

提前致谢!

最佳答案

您可以使用 BitConverter从 UInt32 获取字节。


为此,您需要循环转换每个元素。我会做类似的事情:

private byte[] ConvertFromUInt32Array(UInt32[] array)
{
List<byte> results = new List<byte>();
foreach(UInt32 value in array)
{
byte[] converted = BitConverter.GetBytes(value);
results.AddRange(converted);
}
return results.ToArray();
}

返回:

private UInt32[] ConvertFromByteArray(byte[] array)
{
List<UInt32> results = new List<UInt32>();
for(int i=0;i<array.Length;i += 4)
{
byte[] temp = new byte[4];
for (int j=0;j<4;++j)
temp[j] = array[i+j];
results.Add(BitConverter.ToUInt32(temp);
}
return results.ToArray();
}

关于C# 使用河豚 NET : How to convert from Uint32[] to byte[] 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/692810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com