1

2 header field that is 19 bits long. I'm trying to pack the number 921 decimal into hex into it. Least Significant Byte first.

Packing into 24 bits is easy:

921.0 = 0x0399

binary:

9 9 0 3 0 0

1001 1001 0000 0011 0000 0000

and done. Result: 0x990300

How do I do pack this into 19 bits?

stackprotector
  • 10,498
  • 4
  • 35
  • 64
dlite922
  • 1,924
  • 3
  • 24
  • 60

1 Answers1

2

Lets pretend you have a collection of books. 19 books to be exact. Each book is one inch thick, and you want to put them on a shelf. So you go Ikea and ask for a 19 inch self. The Ikea guy says "We don't have any 19 inch shelves. All we have is this 8 inch shelf. Its called a bÿte". "Ok great you say. I will take 2.375 bÿtes". He says, "Im sorry sir, I cant sell you fractional bÿtes. But if you get 3 bÿtes, It will hold all your books, and you will have 5 inches left over for something else".

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • :-) OK, love the analogy, so if the books are encyclopedia, how should I arrange the 9 book series that I have into the 19 inches? I won't need all 19. I'm trying to represent the hex 0x399 into 19 bits with LSBF. – dlite922 Jan 07 '16 at 17:06
  • 5 bits across 3 bytes go unused, and will be ignored by the application parsing the data. What 5 bits is irelivant, as long as the producer and consumer agree. – szatmary Jan 07 '16 at 17:14
  • @szatmary Is there a best practice for where to insert the padding? Protocols like UDP pad in the end and provide a length to determine the actual data. On the other hand, inserting the padding at the beginning might be neutral to numbers (when respecting the signedness), so you would not even have to care about the padding. – stackprotector Dec 09 '21 at 09:36