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!
Post Reply
Tommy Jhon
Newbie
Posts: 2
Joined: Tue Feb 21, 2017 11:53 pm

How to convert??

Post by Tommy Jhon »

Hi
How to convert decimal number to binary in QBASIC? Whether it is an effective method or not?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: How to convert??

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Tommy Jhon
Newbie
Posts: 2
Joined: Tue Feb 21, 2017 11:53 pm

Re: How to convert??

Post by Tommy Jhon »

thank you so much for the great knowledge share with me.... :D
Post Reply