title: Convert an int into a 4 byte char array in C
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3784263/converting-an-int-into-a-4-byte-char-array-c">Converting an int into a 4 byte char array (C)</a>  </p>
  </blockquote>
  
  
  
  <p>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:</p>
  
  <pre><code>void main(){
   int a=1379;
   int i=0;
   unsigned char value [4];
  
  value[3] = (byte) (a &amp; (0xFF));
  value[2] = (byte) ((a &gt;&gt; 8) &amp; 0xFF);
  value[1] = (byte) ((a &gt;&gt; 16) &amp; 0xFF);
  value[0] = (byte) ((a &gt;&gt; 24) &amp; 0xFF);
  //convert int values to bytes by placing them in a char buffer
  
  for(i=0;i&lt;4;i++){
   printf("%d",value[i]);
   printf(", ");
   }
  printf("\n");
  }
  </code></pre>
  
  <p>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.</p>