שתף באמצעות


How do I specify a binary number?

Question

Saturday, November 16, 2013 6:24 PM

Hello,

I'm using Visual Basic 2010 Express

  If I want to use a decimal number, it is written without any prefix, eg

           value = 12345

 If I want to use Hexadecimal, the prefix is &H, eg

          value = &HD6

My question is, What is the prefix for a binary number?, eg

       value = ????10110010

Trail and error, eg using &B, has proved fruitless.

Related to this, I have spent a lot of time trying to find the answer to above by searching the Microsoft Help pages, but with no success, which is very discouraging for such an elementary question.  Clearly I'm not using the Help facility properly.  I know you have to use the right search terms, but I've tried all sorts of combinations of "format", "number", "binary", etc, to no avail.  Can you provide guidance on using Help?

Many thanks.

All replies (5)

Saturday, November 16, 2013 6:36 PM ✅Answered

You could try the following:

Convert.ToInt32("10110010",2)

http://msdn.microsoft.com/en-us/library/1k20k614(v=vs.110).aspx

LS

Lloyd Sheen


Saturday, November 16, 2013 7:05 PM ✅Answered

There is no such option available for binary numbers. You can try the solution  provided by Lloyd Sheen. Else you can try "ParseBinary" solution provided in following link

http://stackoverflow.com/questions/1417662/how-do-i-type-literal-binary-in-vb-net

Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


Monday, November 18, 2013 8:04 AM ✅Answered

Hi Ken,

have a look at Type Characters (Visual Basic)

And as parsing a literal string using Convert.ToInt32 has some overhead, you should prefer hexadecimal constants - for "10110010" that would be &hB2.

Tip: The Windows Calculator has a programmer mode that can help to convert the value.

Regards Elmar


Saturday, November 16, 2013 11:09 PM

Hi Guys,

Thanks for quick reply.

Convert.ToInt32 works fine.

As a newbie on VB, is there any quick way I can check on the language capabilities/limitations, to avoid spending lots of time searching for an answer that does not exist?  Or is that just part of the learning curve?

Ken_A1


Monday, November 18, 2013 6:53 AM

Hi Guys,

>> As a newbie on VB, is there any quick way I can check on the language capabilities/limitations, to avoid spending lots of time searching for an answer that does not exist?  Or is that just part of the learning curve?

In my view, it is a part of the learning curve. When we start to learn a new development language, we need to practice, practice, and practice. The more you practice, the more you get!

Have a nice time!

Regards,

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.