作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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/
我是一名优秀的程序员,十分优秀!