That is a bit more complicated (and less efficient) than it needs to be though. One option is to snprintf each of the four bytes into a string, then use strtol, to convert it into from a string to an integer. In that case, once you convert the byte array to an unsigned integer, you can simply do printf("%08x", foo) to print it in hex, or printf("%d", foo) to print it in decimal. So it sounds like you want to work in 4-byte chunks (words). If you want to treat the entire byte array as one giant number, you'll need a special library like The GNU MP Bignum Library. Note, without a special big number library, those methods are limited to the largest native types your system supports (probably 64-bit unsigned integers). Make sure you use unsigned integers as the shift operators don't always play nicely with signed integers (by integers, I don't necessarily mean int, you may want unsigned long, or uint32_t, uint64_t or whatever). You can look into the htonl and ntohl functions to help re-order bytes, or make your own (some masking, shifting and bitwise OR should do it) if those wont work. Depending on the endianness of the data in your arra, and the endianness of your system (big, little or other in each case), this may require a bit of finagling. 0 * 16 + 1 * 1 = 0 + 1 = 1 <- The first number you printģ * 16 + 10 * 1 = 48 + 10 = 58 <- The second number you printYou need to consider all 3 or 4 bytes at once, to print the correct decimal number.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |