如何在VC ++中将更大的字节从原始内存映射到4字节变量 [英] How to map larger bytes from raw memory into 4 bytes variable in VC++

查看:111
本文介绍了如何在VC ++中将更大的字节从原始内存映射到4字节变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将值(巨大)转换为4个字节的值时遇到问题.
//分配内存
UINT8 * Rawmem =(UINT8 *)new [4096];

例如,
下面是typedef unsigned int UINT32(4个字节)和typedef unsigned char UINT8(1个字节)

来自标准文档:
假设一些变量
1.系统ID有1:0个字节,总共4096个字节中分配了1个字节
2. FirmwareRev从总共4096个字节中分配了24:1个字节,

//成功分配
我在VC ++中使用以下构造将原始字节映射到变量.
UINT32 SysID = Rawmem [0];

UINT32 FrmRev = Rawmem [1]<< 32 | Rawmem [2]<< 24 | Rawmem [3]<< 16 | Rawmem [4]<< 8 | Rawmem [5];

但是,在上面我将5个字节分配到4个字节的内存中,如果我需要将多达23个字节的内存映射到UINT32(4bytes)怎么办?如何才能将相同的较大字节映射到UINT32?

我正在使用VS IDE 2008,赢得7点,
其他信息:Intel proc,小尾数格式

在此先感谢您,

问候,
Vishalk_91

I have a problem in converting the values(huge) to 4 bytes values.,

//Alloc memory
UINT8 * Rawmem = (UINT8*) new[4096];

for eg.,
Below, the following, typedef unsigned int UINT32(4 bytes) and typedef unsigned char UINT8(1 byte)

from standard documentation:
assume some variable
1. System ID has 1:0 bytes 1 bytes to be allocated from total of 4096 bytes
2. FirmwareRev has 24:1 bytes are allocated from total 4096 bytes,

// On successfull allocation
I use the following construct in VC++ to map the raw bytes to a variable.,

UINT32 SysID = Rawmem[0] ;

UINT32 FrmRev = Rawmem[1] << 32 | Rawmem[2] << 24 | Rawmem[3] << 16 | Rawmem[4] <<8 | Rawmem[5];

but, in the above I am allocating 5 bytes into 4 bytes memory, what if I need to map as large as 23 bytes memory into UINT32(4bytes)? how can one accomplish the same mapping larger bytes into UINT32?

I am using VS IDE 2008,win 7 o/s.,
Misc., info: Intel proc., little endian format

Thanks in Advance.,

With Regards,
Vishalk_91

推荐答案

//如何将更大的字节从原始内存映射到4个字节的变量?
只需在BYTE*
上设置DWORD* 然后使用尖的DWORD:)
// How to map larger bytes from raw memory into 4 bytes variable ?

Just set a DWORD* at a BYTE*
and then use the pointed DWORD :)


好吧,对于那些没有任何意义的入门者.

几秒钟后,您的代码就没有按照您的要求或认为的那样做!
考虑下面的代码&它的输出:

Well, for starters that just doesn''t make an iota of sense.

For seconds, your code''s not doing what your claim or think it is!
Consider the following code & it''s output:

#include <windows.h>
#include <stdio.h>

BYTE Rawmem[5] = {0x11, 0x22, 0x33, 0x44, 0x55};
UINT32 unsignedLong32;

int main()
{
    unsignedLong32 = Rawmem[0] << 32 | Rawmem[1] << 24 | Rawmem[2] << 16 | Rawmem[3] <<8 | Rawmem[4];
    printf("0x%X\n", unsignedLong32);
}


输出:


Output:

0x22334455




您实际上想做什么?您要解决的问题是什么?
毫无疑问,这是一种方法-但这不是!!




What are you actually trying to do? What''s the problem you''re trying to solve?
There''s undoubtedly a way to do it - but this certainly isn''t it!


这篇关于如何在VC ++中将更大的字节从原始内存映射到4字节变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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