question

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

    #1

    question

    how do one convert decimal to binary?
  • Martin Ambuhl

    #2
    Re: question

    Darklight wrote:[color=blue]
    > how do one convert decimal to binary?[/color]

    int main(void)
    {
    int x = 37;
    x = x; /* x is now 37 (base 10) */
    x = x; /* x is now 25 (base 16) */
    x = x; /* x is now 45 (base 8) */
    x = x; /* x is now 100101 (base 2) */
    x = x; /* x is now 1b (base 26) */
    return 0;
    }

    Easy, isn't it?

    Comment

    • Markus Moll

      #3
      Re: question

      Hi

      Darklight wrote:
      [color=blue]
      > how do one convert decimal to binary?[/color]

      One rewrites a sum of (distinct) powers of 10 as a sum of (distinct) powers
      of 2, i.e.:

      \sum_{i=0}^n a_i 10^i = \sum_{i=0}^m b_i 2^i
      where 0 <= a_i < 10, 0 <= b_i < 2 for all i

      However, this has nothing to do with the C programming language.

      I'm sorry, I really don't know what you mean by "convert". Do you want
      convert a decimal string representation of a number to its corresponding
      binary representation? Please explain what exactly you want to do...

      BTW: "question" is one of the worst subject lines you could think of.

      Markus

      Comment

      • Richard Heathfield

        #4
        Re: question

        Darklight said:
        [color=blue]
        > how do one convert decimal to binary?[/color]

        This is really a comp.programmin g question. Still, since you're here...

        The words "decimal" and "binary" describe, not numbers, but
        *representation s* of numbers.

        Let us assume we have a string, D, containing a decimal representation of a
        number. For simplicity's sake, we will assume the number is non-negative.

        The easiest way to do this is to convert D into a number N, which we can do
        using, say, strtoul().

        To store the binary representation in a string, we'll need some storage B -
        conservatively, 4 * L + 1 bytes, where L is the length of D. We will need
        to check that this storage is available before proceeding. For simplicity's
        sake, we'll initialise every byte in B to 0, except that we'll set B[0] to
        '0' rather than 0.

        Pseudocode:

        pos = 0
        While N > 0
        B[pos++] = N mod 2 + '0'
        N /= 2
        Wend
        REM We now have the binary representation, but it's back to front
        REM so we need to reverse it
        If pos > 0
        x = 0
        y = pos - 1
        While x > y
        t = B[x]
        B[x] = B[y]
        B[y] = t
        Wend
        Endif

        Job done. Your mission, should you choose to accept it, is to find how to
        convert the above into a C function.


        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • SM Ryan

          #5
          Re: question

          Darklight <nglen702@netsc ape.net> wrote:
          # how do one convert decimal to binary?

          scanf and related functions (fscanf, sscanf, etc).
          Or functions like strtol and strtod.

          --
          SM Ryan http://www.rawbw.com/~wyrmwif/
          If your job was as meaningless as theirs, wouldn't you go crazy too?

          Comment

          • Kenny McCormack

            #6
            Re: question

            In article <4379b3dc$0$219 55$9b4e6d93@new sread2.arcor-online.net>,
            Markus Moll <moll@rbg.infor matik.tu-darmstadt.de> wrote:
            ....[color=blue]
            >BTW: "question" is one of the worst subject lines you could think of.
            >
            >Markus[/color]

            But is it better or worse than:

            "homework"

            ?

            Comment

            • Keith Thompson

              #7
              Re: question

              [ '#' quoting character corrected ]
              SM Ryan <wyrmwif@tang o-sierra-oscar-foxtrot-tango.fake.org> writes:[color=blue]
              > Darklight <nglen702@netsc ape.net> wrote:[color=green]
              > > how do one convert decimal to binary?[/color]
              >
              > scanf and related functions (fscanf, sscanf, etc).
              > Or functions like strtol and strtod.[/color]

              The *scanf functions don't support binary. They do support octal and
              hexadecimal, which can easily be converted to and from binary.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Malcolm

                #8
                Re: question


                "Darklight" <nglen702@netsc ape.net> wrote[color=blue]
                >
                > how do one convert decimal to binary?
                >[/color]
                "binary" can mean two things, either the machine representation (bits set or
                unset) or a string of ASCII 1s and 0s, which the user can read as binary.

                It is possible but unnecessarily wieldy to convert directly from an ASCII
                decimal to and ASCII binary format. Normally we convert everything to and
                from machine representation.

                The functions sscanf(), atoi() or strtol() will all convert a number from
                decimal to machine representation for you.
                To convert from machine repreentation to ASCII binary is easy. Just AND with
                0x01 to get the first (lowest) bit, 0x02 to get the second bit, 0x40 to get
                the third bit.

                If your are asking, "how does a function like strtol() work", the answer is
                that you take the first digit, and use a look-up table to get the value in
                machine representation (0-9). Then you check for a second digit. If the
                second digit exists, mutliply by ten, convert the second digit, and add.
                repeat until you run out of digits.


                Comment

                • Emmanuel Delahaye

                  #9
                  Re: question

                  Kenny McCormack a écrit :[color=blue]
                  > In article <4379b3dc$0$219 55$9b4e6d93@new sread2.arcor-online.net>,
                  > Markus Moll <moll@rbg.infor matik.tu-darmstadt.de> wrote:
                  > ...
                  >[color=green]
                  >>BTW: "question" is one of the worst subject lines you could think of.
                  >>
                  >>Markus[/color]
                  >
                  >
                  > But is it better or worse than:
                  >
                  > "homework"
                  >
                  > ?[/color]

                  Yes

                  --
                  A+

                  Emmanuel Delahaye

                  Comment

                  Working...