Advertisement
dumle29

Split multibyte variable to an array of bytes

Dec 19th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. // Splits a multi-byte variable, into single bytes stored in a buffer.
  2. void split_to_bytes(size_t value_To_Split, uint8_t * pBuf, uint8_t length)
  3. {
  4.   uint64_t mask = 0xFF; // Mask used for getting the byte
  5.  
  6.   for(int i=0; i<length; i++) // Loop through the larger variable
  7.   {
  8.     pBuf += i;
  9.     *pBuf = value_To_Split & mask; // Apply the mask to the value, and store it in the buffer
  10.     value_To_Split = value_To_Split >> 8; // Rightshift the value one byte to the right, and loop through again.
  11.   }
  12. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement