Hex to Binary Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dondigitech@gmail.com

    Hex to Binary Conversion

    I want to convert hex to binary without losing bits. 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.ToStrin g(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.

    Thanks in advance!!
  • Alberto Poblacion

    #2
    Re: Hex to Binary Conversion

    <dondigitech@gm ail.comwrote in message
    news:970dac63-fd57-496f-aad5-99a696ca417b@l3 3g2000pri.googl egroups.com...
    >I want to convert hex to binary without losing bits. 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.ToStrin g(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.
    One way to add the required number of zeroes is to use the String.PadLeft
    method:

    string revLim = Convert.ToStrin g(curveData[4], 2).PadLeft(8,'0 ');


    Comment

    • Peter Duniho

      #3
      Re: Hex to Binary Conversion

      On Tue, 07 Oct 2008 10:36:05 -0700, <dondigitech@gm ail.comwrote:
      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.ToStrin g(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

      Comment

      • =?Utf-8?B?ZG9uZGlnaXRlY2g=?=

        #4
        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@gm ail.comwrote:
        >
        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.ToStrin g(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
        >

        Comment

        • =?Utf-8?B?ZG9uZGlnaXRlY2g=?=

          #5
          Re: Hex to Binary Conversion

          i forgot to include the first step, which was to combine the 3 byte chunks to
          an int

          "dondigitec h" wrote:
          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@gm ail.comwrote:
          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.ToStrin g(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

          Comment

          Working...