Re: Hex to Binary Conversion



Thank you for your replies. Using the padleft() method worked out for me but
now I'm trying to figure out if it would ultimately be useful in the overall
scheme of things. Any advice in a direction to take would be much
appreciated.

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
2. grab bit fields needed (11 for multiplier and 13 for offset)
3. OR offset (13 bit) with 0xE0 (ultimately yield a 16 bit string) & take 2s
complement of it
4. convert to decimal to get finalOffset number
5. convert (11 bit) multiplier to decimal


"Peter Duniho" wrote:

On Tue, 07 Oct 2008 10:36:05 -0700, <dondigitech@xxxxxxxxx> wrote:

I want to convert hex to binary without losing bits.

No you don't. You want to convert an integer to a string formatted as
binary without losing digits.

You might find that pedantic, but it's an important distinction.

I want to
preserve the 8-bits because I ultimately need a 24-bit string to grab
information from. I am just using this line of code for the
conversion:

string revLim = Convert.ToString(curveData[4], 2);
// curveData[4] = 0x14
// I want 00010100
// I am getting 10100

So, this creates an issue when I want to combine the three bytes
because it totally screws up the bit fields that I need to grab.

Probably the easiest approach would be to use the String.PadLeft() method
to fill out the result string with '0' characters.

That said, you're not showing us the rest of the problem, but if after
creating the string you turn around and try to extract specific
bit-fields, it seems to me that you're going to a lot of extra work for
nothing. You probably just should be doing all of the processing of the
value as an integer, using bit-masks (possibly declared as an enum).

Pete

.



Relevant Pages

  • Re: Hex to Binary Conversion
    ... In 3 byte chunks i need to grab the ... a decimal number which will be a multiplier for a function that I will have ... You want to convert an integer to a string formatted as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help with check time function
    ... My actual script code for this function is below. ... there's no conversion to 24hr clock. ... values will end up with the right multiplier, ... There is no need to use parseInt when multiplying a string ...
    (comp.lang.javascript)
  • Proposal: String::Format::General
    ... It provides format string parsing and output assembly, you provide the code that implements the individual conversion characters. ... Format syntax is kind of a cross between sprintf and strftime, but how close it is to each of these depends on the semantics implemented by the user. ... Note that the following is pre-alpha documentation; the interface to the output conversion code has changed since yesterday, ... conversion character, and contain a number of optional fields which may ...
    (comp.lang.perl.modules)
  • Re: what if (f)printf returns EINTR ?
    ... vsnprintf - formatted output conversion ... int fprintf; ... write to the character string str. ...
    (comp.unix.programmer)
  • Re: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... > So library code would be better if it could handle huge sets. ... widestring, in the future maybe also string). ... Note that ansi->wide conversion is codepage sensitive. ... compiler links in correct conversion code or table). ...
    (comp.lang.pascal.misc)

Loading