Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC++
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 1Blush | :O 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 20-Jun-12 2:57am
Comments
pwasser at 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 at 20-Jun-12 13:16pm
   
The question needs to be reported, it doesn't makes sense in its current form.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

// 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 Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 373
2 Sergey Alexandrovich Kryukov 239
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 5,022
2 Maciej Los 3,269
3 Peter Leow 3,164
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100