bitwise operations.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    bitwise operations.

    Hey everyone. Quick question. Say I have a hex string = "6c". I want to be able to turn that into a bit representation of 0000 0000. would i have to convert it to a integer first then do some sort of left shift on it?? Thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by yeshello54
    Hey everyone. Quick question. Say I have a hex string = "6c". I want to be able to turn that into a bit representation of 0000 0000. would i have to convert it to a integer first then do some sort of left shift on it?? Thanks.
    You first want to convert that string value to a number; the string must be interpreted as a hexadecimal number. The *scanf family of functions can do it; read here about them.

    The next step is to create a string that represents a binary representation of the number. The *printf family of functions can't help you (they only do octal, decimal or hexadecimal representations of a number). A bit of bit shifting can do the job though.

    kind regards,

    Jos

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      In the context if what JosAH has said
      The following algotithm will perform the transformation also.
      1. make the 4 LH bits equal to the LH hex numeral or letter.
      2. make the next 4 bits equal the next hex numeral or letter
      3. repeat 2 above until the hex integer is exhausted.

      e.g. convert 0x 5 d 2 3 f a
      bin equivalent is 0101 1101 0010 0011 1111 1010

      hth's

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I find it hard to believe that you are recommending using a *scanf function Jos, I personally have always believed that they are a curse on the C language.

        Myself I would use strtol

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Banfa
          I find it hard to believe that you are recommending using a *scanf function Jos, I personally have always believed that they are a curse on the C language.

          Myself I would use strtol
          The *scanf functions do their job if used correctly; I agree, when used as interactive lexical analyzers they feel like acute diarrhea with no toilet in the vicinity.

          kind regards,

          Jos

          Comment

          Working...