Page 1 of 1

How to convert??

Posted: Wed Feb 22, 2017 12:01 am
by Tommy Jhon
Hi
How to convert decimal number to binary in QBASIC? Whether it is an effective method or not?

Re: How to convert??

Posted: Wed Feb 22, 2017 11:42 am
by burger2227
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

Re: How to convert??

Posted: Wed Feb 22, 2017 10:55 pm
by Tommy Jhon
thank you so much for the great knowledge share with me.... :D