1

So I have an address where an offset for a vftable is located. it is stored in the assembly as bytes. ex: 03 c3 bd 0c

I want to get the bytes, and convert them little endian style to an address.

byte[0] = ((unsigned char *)addr)[3];
byte[1] = ((unsigned char *)addr)[2];
byte[2] = ((unsigned char *)addr)[1];
byte[3] = ((unsigned char *)addr)[0];

so the output of the example would be 0x0cbdc303.

how can I correctly code this?

user207421
  • 305,947
  • 44
  • 307
  • 483
CPunkh
  • 99
  • 7
  • I was about to flag this as duplicate, at least for the principle. Surprisingly, there was a non-working answer chosen [here](https://stackoverflow.com/q/20755897/1856738) although a [good answer was given](https://stackoverflow.com/a/20756148/1856738), while [no answer at all was chosen here](https://stackoverflow.com/q/7879953/1856738). Now I see two guys disputing what the OP really wanted... looks like this a hot topic... please choose the correct answer (now that you learned about shifting operations)... – class stacker Jun 09 '17 at 12:31

2 Answers2

0
byte[0] = ((unsigned char *)addr)[3];
byte[1] = ((unsigned char *)addr)[2];
byte[2] = ((unsigned char *)addr)[1];
byte[3] = ((unsigned char *)addr)[0];

should be followed by

uint32_t address = (byte[0] << 24)|(byte[1] << 16)|(byte[2] << 8)|(byte[3]);

Of course you can get rid of byte[] and substitute the original values into this.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • ...and on a little-endian machine (which it needs to be to get the correct number with your code), this is a slow version of my answer (which was downvoted, probably by you) – deviantfan Jun 09 '17 at 03:12
  • @deviantfan He needs to swap bytes . He says so. Read the question. – user207421 Jun 09 '17 at 11:37
-1

So you have a little-endian device and want to convert 4 little-endian byte to a single 4-byte-number. This can easily be done this way:

uint32_t myNumber = *((uint32_t *)addr);
deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • Doesn't swap the bytes as per the OP's code. – user207421 Jun 09 '17 at 03:03
  • @EJP Imho OP didn't ask about swapping, but about getting a numerical value from bytes. OP just guessed swapping is necessary. – deviantfan Jun 09 '17 at 03:06
  • Read the question The OP has 0x03c3bd0c and he wants 0x0cbd3c03. Hard to see to accompish that without swapping. – user207421 Jun 09 '17 at 11:32
  • @EJP 0x03c3bd0c doesn't exist in OPs post. 03 c3 bd 0c, which usually means single bytes (and OP even calls them "bytes"), is there. And this *is* 0x0cbd3c03 in little endian. ... And the word "swap", like suggested by you below, doesn't exist either. – deviantfan Jun 09 '17 at 14:46
  • The OP has accepted my answer. QED – user207421 Jun 09 '17 at 23:03
  • @EJP As if that means something ... accepting goes 95% to the user with more rep. Well, whatever ... the deletion vote for an answer you just don't like, *before* you knew anything from OP, shows pretty well you just can't stand to not be right. Be happy with your 15 virtual points, I'm not here for points. – deviantfan Jun 10 '17 at 07:04