Re: Hex to Binary Conversion
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Wed, 15 Oct 2008 17:09:15 -0700
On Wed, 15 Oct 2008 10:01:03 -0700, dondigitech <dondigitech@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
[...]
Basically, I have an array of hex bytes. In 3 byte chunks i need to grab the
bits 0(lsb)-12 which will be ORed with 0xE0 (which is why i'm doing the hex
to binary string conversion), then I will have to take the 2s complement of
it to get my desired offset value. Bits 13-23(msb) will just be converted to
a decimal number which will be a multiplier for a function that I will have
to perform. So right now, this is my approach (i know its not even close to
being the most efficient):
1. convert hex bytes to binary
Here's a good place to be careful with your terminology. "Hex" implies a written representation, i.e. text. Are your bytes really being provided as text? Or do you have actual bytes? The conversion itself depends on exactly what you mean, as well as in what order the bytes are provided.
That said, whatever the input, the conversion is not hard. Let's assume you are actually being given bytes, not characters (text), and that they are provided most-significant-byte first. Then the conversion looks like this:
byte[] rgb = ...; // input, MSB first
int i = 0; // output
// Assumes 3-byte array always
for (int ib = 0; ib < 3; ib++)
{
i = (i << 8) | rgb[i];
}
// now i contains the 24-bit number that was in the array
2. grab bit fields needed (11 for multiplier and 13 for offset)
int offset = i & 0x1fff,
multiplier = (i >> 13) & 0x7ff;
3. OR offset (13 bit) with 0xE0 (ultimately yield a 16 bit string) & take 2s
complement of it
Surely you mean 0xe000? And when you say "2's complement", are looking for the 2's complement of the 16-bit value, or the full 32-bit value of an int? I'm assuming the 16-bit value.
offset = -(offset | 0xffffe000);
At this point, using the code above, you would now have in your variables "offset" and "multiplier" the correct numeric values as I understand them from your description. Steps 4 and 5 seem superfluous (see below).
Note that I'm assuming the description is correct. It seems odd to me to deliver an offset as a 2's complement 13-bit value, but presumably you just happen to be dealing with some unusual source of data and this is in fact correct.
4. convert to decimal to get finalOffset number
This part doesn't make sense. The number isn't "decimal" unless you convert it to written form, i.e. text. Until then, it's just a number.
5. convert (11 bit) multiplier to decimal
See above.
Pete
.
- References:
- Hex to Binary Conversion
- From: dondigitech
- Re: Hex to Binary Conversion
- From: Peter Duniho
- Re: Hex to Binary Conversion
- From: dondigitech
- Hex to Binary Conversion
- Prev by Date: hardware control
- Next by Date: Re: ftp from a c sharp application
- Previous by thread: Re: Hex to Binary Conversion
- Next by thread: how to convert a class/struct to a byte array?
- Index(es):
Relevant Pages
|