I need a function that takes four unsigned char variables as parameters and combines them into an unsigned int. The first char variable being the first byte of the int, the second char being the second byte and so on. Here is what I have so far, it is not working properly and I can’t figure out why after messing around with it and googling for several hours.
uint32_t combineChar(unsigned char one, unsigned char two, unsigned char three, unsigned char four){
uint32_t com;
com = (uint32_t)one;
com = com << 8 | (uint32_t)two;
com = com << 8 | (uint32_t)three;
com = com << 8 | (uint32_t)four;
return com;
}
Your code is endianess-depend. The first byte (of
uint32_t) is in some systems the leftest, and in some systems the rightest, so you may store the bytes in the opposite way than what to you want.(Actually, if you want just the
uint32_t, it’s fine. Problems begin when you union it withchar[4], or similar stuff)