本文介绍了C ++到C# - 将无符号Char []转换为无符号长[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的开发者,



我正在努力将一些C ++代码转换为C#。不幸的是老实说你,我不是很熟悉C ++,所以也许我错过了一些简单的东西。



无论如何,我有这个64元素的无符号字符使用以下元素调用 buffer



[0] ='C'

[1] =' P'

[2] ='0'

[3] ='4'

...

[12] ='€'



剩下的元素都是\0(不是'0')。



然后将这个unsigned char数组转换为unsigned long数组(在类中),如下所示:



Dear fellow devs,

I'm working on a task of converting some C++ code into C# . Unfortunately to be honest with you, I'm not very fluent with C++ so maybe I'm missing something simple.

Anyway, I have this unsigned char of 64 elements called buffer with the following elements:

[0] = 'C'
[1] = 'P'
[2] = '0'
[3] = '4'
...
[12] = '€'

The rest of the elements are all \0 (not '0').

Then this unsigned char array is cast to unsigned long array (within a class) as follows:

typedef struct _SHA_MSG {
    unsigned long W[80];
} sha1_key;

unsigned char buffer[64]={0};

buffer[0] = 'C';
buffer[1] = 'P';
buffer[2] = '0';
buffer[3] = 4 + '0';
buffer[12] = 0x80;

sha1_key* k = (sha1_key*)buffer;





但是在这一点上,它会发生巨大变化!例如 kW 包含:



[0] = 875581507

[3] = 128

[16] = 3435973836

[17] = 2641335238

[18] = 3865956

[19] = 18366288

[20] = 0

[21] = 0

[22] = 2130567168

[23] = 3435973836



等,大多数元素的值为0或3435973836.



刚刚发生了什么?有人可以指示任何产生这些结果的C#等效操作(理想情况下不依赖于IntPtr)吗?



非常感谢!



编辑:



这部分我的C#代码是:





However at this point, it changes drastically! For example k.W contains :

[0] = 875581507
[3] = 128
[16] = 3435973836
[17] = 2641335238
[18] = 3865956
[19] = 18366288
[20] = 0
[21] = 0
[22] = 2130567168
[23] = 3435973836

etc, with most elements having value 0 or 3435973836.

What has just happened? Can someone kindly instruct any C# equivalent of this operation that yields these results (ideally without relying on IntPtr) ?

Thanks a lot!



My C# code of this part is:

struct sha1_key
{
    uint[] w;

    public uint[] W
    {
        get { return w; }
        set { w = value; }
    }
}

sha1_key key = new sha1_key();
key.W[0] = 'C';
key.W[1] = 'P';
key.W[2] = '0';
key.W[3] = (char)(year + '0');

key.W[12] = (char)0x80;





正如您所看到的,到目前为止,我保留了大部分代码,就像在C ++中一样。稍后,当我设法让它工作时,我会改进它:)



As you can see, I have so far retained much of the code as it is in C++. I will improve upon it later, when I manage to get it working :)

推荐答案


byte[] bytes = { (byte) 'C', (byte) 'P', (byte) '0', (byte) '0' + 4 };

 UInt32 i = BitConverter.ToUInt32(bytes, 0);







[]


这篇关于C ++到C# - 将无符号Char []转换为无符号长[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 07:42