Change Hex to Binary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ron

    #1

    Change Hex to Binary

    Hi,
    I have a 16 bit hex value contained in a string.
    example="00A4"
    I need to convert the hex value to binary.
    I then need to return the whole binary value as a string.
    example="000000 0010100100"

    Then need to look at each bit value to see if "1" or "0"
    and return a bool indicating the resultant.

    I have done this in VB6 but it was a long drawn out
    process of if statements.

    There must be a easy way do this in C#?

    Could someone please explain what is the best way to
    handle this, a code example would be most appreciated.

    Thanks Ron
  • Jon Skeet

    #2
    Re: Change Hex to Binary

    ron <goodr@no_spam_ 4me_mddm.panaso nic.com> wrote:[color=blue]
    > I have a 16 bit hex value contained in a string.
    > example="00A4"
    > I need to convert the hex value to binary.
    > I then need to return the whole binary value as a string.
    > example="000000 0010100100"
    >
    > Then need to look at each bit value to see if "1" or "0"
    > and return a bool indicating the resultant.
    >
    > I have done this in VB6 but it was a long drawn out
    > process of if statements.
    >
    > There must be a easy way do this in C#?
    >
    > Could someone please explain what is the best way to
    > handle this, a code example would be most appreciated.[/color]

    example = Convert.ToStrin g(Convert.ToInt 32(example, 16), 2);

    Note that it's using a 32 bit int instead of 16 bits to avoid problems
    with values > 32768.

    That doesn't end up with any left padding either, but that's easy
    enough to do as well.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • William Stacey

      #3
      Re: Change Hex to Binary

      string binaryText = Convert.ToStrin g(Convert.ToInt 32(hex, 16),
      2).PadLeft(8,0) ;

      [color=blue]
      > That doesn't end up with any left padding either, but that's easy
      > enough to do as well.[/color]


      Comment

      • Jon Skeet

        #4
        Re: Change Hex to Binary

        William Stacey <staceyw@mvps.o rg> wrote:[color=blue]
        > string binaryText = Convert.ToStrin g(Convert.ToInt 32(hex, 16),
        > 2).PadLeft(8,0) ;[/color]

        Cheers - hadn't seen PadLeft before. I think the arguments in question
        should be (16, '0') though :)

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • William Stacey

          #5
          Re: Change Hex to Binary

          Cheers :-)


          Comment

          Working...