converting decimal to binary

Hi all,

I am trying to design a gui for a project and i need to take values entered by the user into a textbox, then convert them from decimal to binary, and send them to the serial port...how would i go about this

Any help or code or anything would be great

thanx a million



Answer this question

converting decimal to binary

  • Randy Kreitzman

    If by decimal you just mean a normal decimal number, as opposed to a decimal type, and it is an integer, then to convert to binary is extremely easy. All you have to do is repeatedly divide by 2 until the answer is 0 and look at the remainders.

    e.g. to convert 50 in decimal to binary

    Divide by 2
    Answer = 25, Remainder = 0, so least significant digit = 0

    Divide answer by 2
    Answer = 12, Remainder = 1 , so second digit = 1

    divide answer by 2
    Answer = 6, Remainder = 0, so third digit = 0

    divide answer by 2
    Answer = 3, Remainder = 0, so fourth digit = 0

    divide answer by 2
    Answer = 1, Remainder = 1, so fifth digit = 1

    divide answer by 2
    Answer = 0, Remainder = 1, so sixth digit = 1

    So, reading the remainders from bottom to top, 50 in binary is 110010


    That should be very easy to code using the Mod operator and Int function.


    Dave


  • Steve W.

    The only way to convert a decimal to binary is to have the binary as a string, each character represent a bit. Decimal extremely tricky to convert to binary, here are some good tutorial: http://www.google.com/search hl=en&lr=&q=ieee+float+binary

    If your serial port need to receive bit, then just sending a floating number should do the job.



  • converting decimal to binary