Re: Hex to Binary Conversion



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
.



Relevant Pages

  • Re: BCD List to HEX List
    ... first of all python is not going to be used for my purpose (sigh) ... I have device A which holds a binary coded decimal array ... between your `dec` and `hex` values. ... Do you need such a conversion? ...
    (comp.lang.python)
  • Re: Convext HEx value to byte array
    ... > need to convert the HEX value to the byte array. ... > Any ideas or suggestion how to do conversion? ...
    (microsoft.public.vb.general.discussion)
  • Re: string conversion
    ... I didn't fully understand what is it that you are having trouble with, but if it is conversion from hex U8 number array to hex string maybe this solution is what you are looking for: ...
    (comp.lang.labview)
  • Convext HEx value to byte array
    ... need to convert the HEX value to the byte array. ... Any ideas or suggestion how to do conversion? ...
    (microsoft.public.vb.general.discussion)
  • Re: All X0D lost during reading line sequential file using microfocus se
    ... On Jul 30, 2:37 pm, "Pete Dashwood" ... Maybe I should change the DB codepage? ... then I think you need to be VERY careful of your "conversion" ... stream from the Hex, ...
    (comp.lang.cobol)