# binary representation of a decimal number

• ### Question

• is there any function in vb which gives the binary representation of a decimal number?

Sougata Ghosh

• Edited by Thursday, November 29, 2018 5:47 PM changed keyword
Thursday, November 29, 2018 5:09 PM

• See the following code as there is no builtin method.

http://www.vb-helper.com/howto_decimal_to_binary.html

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Thursday, November 29, 2018 6:01 PM
Thursday, November 29, 2018 5:50 PM

### All replies

• See the following code as there is no builtin method.

http://www.vb-helper.com/howto_decimal_to_binary.html

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Thursday, November 29, 2018 6:01 PM
Thursday, November 29, 2018 5:50 PM
• is there any function in vb which gives the binary representation of a decimal number?

Sougata Ghosh

A decimal number like

```        Dim d As Decimal = 123.987D '????
```

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

- from former MSDN User JohnWein

SerialPort Info

Multics - An OS ahead of its time.

Thursday, November 29, 2018 6:00 PM
• is there any function in vb which gives the binary representation of a decimal number?

If by "a decimal number" you mean an integral number:

```Dim n As Int32 = 64
Dim s As String = Convert.ToString(n, 2)
```

To show all bits you can pad the string with the appropriate number of zeros:

```Dim s As String = Convert.ToString(n, 2).PadLeft(32, "0")
```

If a Decimal type, then it would have to be narrowed to an integral type
which will lose the precision provided by the decimal fraction:

```Dim d As Decimal = 123.45 ' will be truncated to 123
Dim sd As String = Convert.ToString(Convert.ToInt32(d), 2).PadLeft(32, "0")
```

- Wayne

Friday, November 30, 2018 5:07 AM
• no...I meant a number represented in the decimal number system...that is base 10.

Sougata Ghosh

Friday, November 30, 2018 6:15 PM
• no...I meant a number represented in the decimal number system...that is base 10.

Sougata Ghosh

So I am guessing natural numbers or integers if negatives allowed.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

- from former MSDN User JohnWein

SerialPort Info

Multics - An OS ahead of its time.

Friday, November 30, 2018 6:41 PM
• yes. that's right...I meant integers

Sougata Ghosh

Friday, November 30, 2018 6:48 PM
• yes. that's right...I meant integers

Sougata Ghosh

Be aware that the negative numbers are two's complement.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

- from former MSDN User JohnWein

SerialPort Info

Multics - An OS ahead of its time.

Friday, November 30, 2018 8:05 PM