## How to convert??

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Tommy Jhon
Newbie
Posts: 2
Joined: Tue Feb 21, 2017 11:53 pm

### How to convert??

Hi
How to convert decimal number to binary in QBASIC? Whether it is an effective method or not?

burger2227
Veteran
Posts: 2407
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

### Re: How to convert??

QB64 can use &B for binary numbers to convert _BIT 's back:

Code: Select all

``````
PRINT BIN\$(255)      '1 byte(8 bits) maximum
PRINT BIN\$(32767)    'integer(2 byte, 15 bits) maximum
PRINT BIN\$(-32768)   'integer(2 byte, 16 bits) minimum
PRINT BIN\$(-1)       'all 16 bits on

FUNCTION BIN\$ (n%)
max% = 8 * LEN(n%) ': MSB% = 1   'uncomment for 16 (32 or 64) bit returns
FOR i = max% - 1 TO 0 STEP -1    'read as big-endian MSB to LSB
IF (n% AND 2 ^ i) THEN MSB% = 1: B\$ = B\$ + "1" ELSE IF MSB% THEN B\$ = B\$ + "0"
NEXT
IF B\$ = "" THEN BIN\$ = "0" ELSE BIN\$ = B\$    'check for empty string
END FUNCTION  ``````
http://www.qb64.net/wiki/index.php/%26B

http://www.qb64.net/wiki/index.php/Binary