1

Possible Duplicate:
Converting an int into a 4 byte char array (C)

Using the Dynamic C program (the language is C), I am trying to convert an int into an array of bytes 4 places long. So far I have looked online and I have found a few examples. However, none seem to work for me. I have had an ongoing problem where the correct byte numbers are printed, but they are repeated twice for some reason. I have provided the code below:

void main(){
 int a=1379;
 int i=0;
 unsigned char value [4];

value[3] = (byte) (a & (0xFF));
value[2] = (byte) ((a >> 8) & 0xFF);
value[1] = (byte) ((a >> 16) & 0xFF);
value[0] = (byte) ((a >> 24) & 0xFF);
//convert int values to bytes by placing them in a char buffer

for(i=0;i<4;i++){
 printf("%d",value[i]);
 printf(", ");
 }
printf("\n");
}

For example, with this value the program prints "5, 99, 5, 99," when it should print "0, 0, 5, 99". Thank you for your help.

16
  • There's something you're not telling us. Commented Jul 11, 2012 at 19:11
  • 1
    I'm thinking it's a language (for microcontrollers) that has 16-bit ints. Commented Jul 11, 2012 at 19:13
  • You should initialise your array, unsigned char value[4] = {0};. Commented Jul 11, 2012 at 19:14
  • 1
    @pstrjds -- Just because the title's the same does not make it a dupe. Commented Jul 11, 2012 at 19:17
  • 5
    1) main should return int 2) remove the (byte) casts (what is byte?) they can only do harm. 3) shifting signed ints will sign-extend them, use preferrably unsigned entities to shift. 4) use %u printf format to print unsigned types. 5) you are assuming 32 bits ints, and CHAR_BIT=8. 6) sizeof is your friend. Commented Jul 11, 2012 at 19:17

3 Answers 3

4

It's almost certainly the case that "Dynamic C" is an implementation with an int of 16 bits, which is perfectly "legal" for C. If the int is 16 bits, any shift over 16 bits is modulo-16, so the second two shifts duplicate the first two.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 since this is either the answer OR it is a duplicate question :)
For further info, based on this page http://www.rabbitsemiconductor.com.cn/products/dc/index.shtml mentioning dynamic c 32, I would guess that dynamic c has 16 bit int
@pstrjds It's not a duplicate question at all. The other question doesn't mention the behavior this one does, behavior that indicates a 16-bit machine.
@JimBalter - I added the smily face to point out that I was making a poor joke. I realized after reading further it was not a duplicate and was responding to Hot Licks that I had realized that and upvoted his correct answer.
I believe its actually UB to shift by more than the length of the variable, may be worth saying this. The compiler probably just happens to do something sensible.
|
1

You need to copy the bits from your int variable into the char-array. You can simply do that with memcpy like this:

#include <stdio.h>
#include <string.h>

void split(int val, unsigned char *arr) {
    memcpy(arr, &val, sizeof(int));
}

int main() {
    unsigned char bytes[4];
    split(1379, bytes);
    printf("%d, %d, %d, %d\n", bytes[0], bytes[1], bytes[2], bytes[3]);
}

Comments

0
#include <stdio.h>
#include <string.h>

int main(void){
 int a=1379;
 unsigned bot,top ;
 unsigned char value [sizeof a];

 memcpy (value, &a, sizeof value);

 /* You can omit this loop if you are on a big-endian machine */
#if LITTLE_ENDIAN
 for (bot =0, top = sizeof value; --top > bot; bot++) {
    unsigned char tmp;
    tmp = value[bot];
    value [bot] = value[top];
    value[top] = tmp;
    }
#endif

for(bot=0;bot < sizeof value;bot++){
 printf("%u", (unsigned int) value[bot]);
 printf(", ");
 }
printf("\n");
return 0;
}

1 Comment

The code makes no assumptions about the sizes of ints or characters; that's why there are no manifest constants left in it. (and gcc unrolls the "silly" loop, anyway)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.