| View previous topic :: View next topic |
| Author |
Message |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Sat Mar 06, 2010 10:16 am Post subject: writing to HiWord LoWord |
|
|
I have an array of bytes, and a WORD. I want to write the 2 bytes seperately in the WORD. How should I do it. Example
| Code: |
WORD TheWord;
BYTE Bytes[100]; <-- say this is full with values.
TheWord = Bytes[0]; <-- High
TheWord = Bytes[1]; <-- Low
|
If I do that, only the Low Word will be written, the High Word will be empty.
Please help to do it.
EDIT: I am thinking now,, if I multiply the high byte with 256 will give the high word value. Eg. I want to write 0xB6.
0xB6 * 256 = 0xB600 and then just adding up the low word??
Last edited by kot1990 on Sat Mar 06, 2010 10:26 am; edited 1 time in total |
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Mar 06, 2010 10:26 am Post subject: Re: writing to HiWord LoWord |
|
|
| kot1990 wrote: | I have an array of bytes, and a WORD. I want to write the 2 bytes seperately in the WORD. How should I do it. Example
| Code: |
WORD TheWord;
BYTE Bytes[100]; <-- say this is full with values.
TheWord = Bytes[0]; <-- High
TheWord = Bytes[1]; <-- Low
|
If I do that, only the Low Word will be written, the High Word will be empty.
Please help to do it. |
Derrp look at your code... you're rewriting "TheWord".
|
|
| Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Sat Mar 06, 2010 10:30 am Post subject: Re: writing to HiWord LoWord |
|
|
| &Vage wrote: | | Derrp look at your code... you're rewriting "TheWord". |
yes I know,, what I want to do when writing the High Word,, is that I want to distinguish it, so it will write on the high Word , and not the low word, so I won't rewrite the Low Word.
|
|
| Back to top |
|
 |
AtheistCrusader Grandmaster Cheater
Reputation: 6
Joined: 23 Sep 2006 Posts: 681
|
Posted: Sat Mar 06, 2010 10:51 am Post subject: |
|
|
| Code: |
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
BYTE Byte = 64;
BYTE byte = 65;
LONG TheWord;
TheWord = MAKELONG(Byte,byte);
cout << LOWORD(TheWord) << " " << HIWORD(TheWord) << endl;
system("PAUSE");
} |
|
|
| Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Sat Mar 06, 2010 10:57 am Post subject: |
|
|
Thx man I'll keep in mind,, I found another way to do it like
| Code: |
WORD TheWord;
BYTE Bytes[100]; <-- say this is full with values.
TheWord = Bytes[0] * 256; <-- High
TheWord += Bytes[1]; <-- Low
|
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Sat Mar 06, 2010 1:23 pm Post subject: |
|
|
| Code: | WORD TheWord;
BYTE Bytes[100]; <-- say this is full with values.
TheWord = Bytes[0] << 8 | Bytes[1]; |
Just to note: the following would use Bytes[0] as low word and Bytes[1] as high word:
| Code: | | TheWord = *(WORD*)&Bytes[0]; |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Mar 06, 2010 5:38 pm Post subject: Re: writing to HiWord LoWord |
|
|
| kot1990 wrote: | I have an array of bytes, and a WORD. I want to write the 2 bytes seperately in the WORD. How should I do it. Example
| Code: |
WORD TheWord;
BYTE Bytes[100]; <-- say this is full with values.
TheWord = Bytes[0]; <-- High
TheWord = Bytes[1]; <-- Low
|
If I do that, only the Low Word will be written, the High Word will be empty.
Please help to do it.
EDIT: I am thinking now,, if I multiply the high byte with 256 will give the high word value. Eg. I want to write 0xB6.
0xB6 * 256 = 0xB600 and then just adding up the low word?? |
I think you were aiming for something like this?
| Code: | int main()
{
WORD dongs;
WORD w = 0x1234;
BYTE* b = (BYTE*)&w;
b[0] += 1; //b[0] is 0x34 before the addition
b[1] += 1; //take a guess
dongs = (WORD)(b[0] | b[1] << 8); //or...
dongs = *(WORD*)b;
return 0;
} |
you can just use a bitwise or to 'combine', just make sure you shift appropriately... or you can just cast it.
|
|
| Back to top |
|
 |
|