Question:
Looking for a math genius....?
2006-09-27 06:58:47 UTC
I have an input byte stream consisting of numbers in the set {0 - 15} exclusively of about 1000 bytes.

What I would like to do is to be able to somehow store that mathematically in ONE unsigned integer and then, having in hand the the last byte value that was "encoded" into that unsigned integer, mathematically reverse it back to the original byte stream.

I am hoping there is a way to mathematically encode the values into an integer and then decoding them back.

Do you know how?

Manny
Three answers:
2006-09-27 07:02:27 UTC
An encoding table is the way to go. It would be quick, and you could easily implement alternative encoding tables to change it.
Jeff Alexander
2006-09-27 17:11:53 UTC
If I understand the question, you want to encode 4000 or 8000 bits (depending upon how the byte stream encodes the numbers) in a single 32 bit value plus a 4 bit value. If there are no constraints on the byte stream then the encoding you request doesn't exist.



The problem is that there are more potential values in the byte stream then there are encodings in the integer plus number.
2006-09-27 14:16:03 UTC
I'm not 100% sure if this is the answer you're looking for, but here goes...



Binary number (8 bits) 0000 0000

The first 4 bits can have a value from 0 - 15

The second 4 bits can have a value from 0-15 also

So you can store two values (so long as thoes values are between 0-15) in one binary number.



Below is the decimal value of each half byte (nibble)



decimal value 1 2 4 8 (1+2+4+8=15)

binary equiv. 1 1 1 1 (15)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...