C ++到C# - 将无符号Char []转换为无符号长[] [英] C++ to C# - Casting Unsigned Char[] to Unsigned Long[]

查看:230
本文介绍了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 :)

推荐答案

在C ++中, unsigned char type是单个字节。如果将指向 unsigned char 的数组的指针强制转换为指向 unsigned long 数组的指针,则表示您正在查看内存好像被组织成32位字而不是单字节。



kW 包含875581507,这是十六进制的34305043。从最低有效字节开始,对应于原始 unsigned char 数组中的 [0] 个字节,有43('C'),50('P'),30('0')和34('4')。
In C++, the unsigned char type is a single byte. If you cast the pointer to an array of unsigned char to a pointer to an array of unsigned long, you are viewing the memory as if it were organized into 32-bit words instead of single bytes.

k.W contains 875581507, which is 34305043 in hexadecimal. Starting from the least significant byte, which corresponds to the [0]th byte in the original unsigned char array, you have 43 ('C'), 50 ('P'), 30 ('0'), and 34 ('4').


对于ascii字符



For ascii characters

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

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







http://msdn.microsoft.com/en-us/library/system.bitconverter.touint32.aspx [ ^ ]


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆