KB-47098: Converting decimal values into binary ones.

Question:

How can I convert a decimal value into binary?

Solution:

To convert these values, you must divide the first one by 2, then take the rest (always 0 or 1) and concatenate this digit to the binary number, to the left.  Repeat this process as many times as necessary until the original value is smaller than 2.

In vbScript, the operator calculating the rest in a division is called Mod. See a sample script below:

my_value = InputBox(“Type the desired value in decimal format:”)
 
binary_value = “”
while my_value >= 2
               binary_value = (my_value Mod 2) & binary_value
               my_value = int(my_value / 2)
wend
binary_value = my_value & binary_value
 
MsgBox binary_value

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *