Advertisement
Shalfey

Make array of bytes from integer

Apr 19th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. const int BITS_PER_BYTE = 8;
  2. int result = 0;
  3. // I would initialize the variable.
  4. // logic block  ...
  5. int res = result;
  6. int numBytesInResult = 0;
  7. while(res != 0)
  8. {
  9.     numBytesInResult++;
  10.     res = res >> BITS_PER_BYTE;
  11. }
  12. char *buf = new char[numBytesInResult + 1];
  13. buf[numBytesInResult] = '\0';
  14. for(int i = 0; i < numBytesInResult; i++)
  15. {
  16.     char c = (char)  result & 0xF;      // took the last byte of "result" and saved it in "с"
  17.     buf[numBytesInResult - i - 1] = c;  // write to buf from the youngest byte to oldest
  18.     result = result >> BITS_PER_BYTE;
  19. }
  20. // WriteFile
  21. delete [] buf;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement