Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
[no name] 20-Jun-12 9:22am    
UINT32 FrmRev = Rawmem[1] << 32 | Rawmem[2] << 24 | Rawmem[3] << 16 | Rawmem[4] <<8 | Rawmem[5];

This line does NOT "map" 5 bytes into 4 whatever that means. The question makes no sense.
Vitaly Tomilov 20-Jun-12 13:16pm    
The question needs to be reported, it doesn't makes sense in its current form.

// 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 :)
 
Share this answer
 
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:

C++
#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!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900