I'm working on IJVM project using C language. First, I imported a binary file into my program, which should be parsed as words. Now I made a variable (text) which contains the binary and I need to convert it into an array of words. I got a hint
int result = ((bytes[0] & 0xFF) << 24) |
((bytes[1] & 0xFF) <<16) |
((bytes[2] & 0xFF) << 8) | (bytes[3] & 0xFF);
This worked only for the first 4-bytes, What I need is an array of result
.
I tried something like this, but it never worked .. :(
void type_converter(byte_t* text)
{
for (int i = 0; i < sizeof(text)/4; i++) {
for (int j = 0; i < sizeof(text)/4; i++) {
int result[i] = ((text[j+0] & 0xFF) << 24) |
((text[j+1] & 0xFF) << 16) |
((text[j+2] & 0xFF) << 8) | (text[j+3] & 0xFF);
instruction[i] = result;
j =+ 4;
}
}
About OP's code sample:
The inner loop doesn't make sense (or I didn't understand the intention).
The outer loop, I would change to:
for (int i = 0; i + 3 < size; i += 4)
. Please, note thei + 3
. It makes the loop safe against a partial amount of bytes at end of buffer. Thei += 4
makes the appropriate step in 4 byte jumps.The
size
is something you have to provide. As Martin already mentioned:sizeof text
provides the size of the pointer. There is no chance to determine this by an operator (if you don't have direct access to original array). You should make a parametersize
for this. (This is a very common solution in C programs.)Btw. there is no declaration for
result
orinstruction
. Hence, I would make one of them as another parameter of function.So, your change function could look like this:
And, please, don't forget that
result
has to provide enough storage forsize / 4
uint32_t
words.As I just remembered how I did similar things in the past, this can be a little bit changed to prevent the
i + 3
in loop condition:Live Demo on wandbox
Note:
I just realized that this kind of byte combination is for big-endian numbers (highest byte in first position). For little-endian the indices had to be re-ordered.