Qbasicnews.com February 19, 2020, 12:48:37 AM  Pages: 1 Author Topic: Binary... Say what?  (Read 8828 times)
seph
Na_th_an     Posts: 1915  « Reply #15 on: May 22, 2003, 07:51:34 PM »

Here ya go:

Code:
DECLARE SUB StrToBin (s AS STRING, b() AS STRING, places AS INTEGER)
DECLARE FUNCTION IntToBin\$ (n AS INTEGER, places AS INTEGER)
CLS
EnterString:
INPUT "Enter a string: ", s\$
IF s\$ = "" THEN CLS : PRINT "Just enter a goddamn string!": GOTO EnterString

DIM b(1 TO LEN(s\$)) AS STRING

CALL StrToBin(s\$, b(), 8)

PRINT "The binary code of your string is as follows: "
FOR i = 1 TO LEN(s\$)
PRINT "- "; b(i)
NEXT i

FUNCTION IntToBin\$ (n AS INTEGER, places AS INTEGER)
s\$ = STRING\$(places, "0")

FOR i = 1 TO LEN(s\$)
MID\$(s\$, i, 1) = "1"
END IF
NEXT i

IntToBin = s\$
END FUNCTION

SUB StrToBin (s AS STRING, b() AS STRING, places AS INTEGER)
s\$ = UCASE\$(s\$)

FOR i = 1 TO LEN(s)
IF ASC(MID\$(s\$, i, 1)) = 32 THEN
b(i) = STRING\$(places, "0")
ELSE
b(i) = IntToBin(ASC(MID\$(s\$, i, 1)) - 64, places)
END IF
NEXT i
END SUB Logged

earn.
Mango
Wandering Guru   Posts: 360  « Reply #16 on: June 01, 2003, 11:49:26 PM »

Quote from: "whitetiger0990"
Make a qbasic program that convert words/ letters into binary.
I should be able to enter Joe and it comes out:

J = 00001010
O = 00001111
E = 00000101

Oh...  and also... ABC = 123
So "J" = 10

Whitetiger--just to beat a dead horse a little, the reason no one met your challenge is that no one would *ever* write a program like this...so you didn't even want the whole ASCII value??!!!  Just the 4 least significant bits with 4 leading zeros!!! I figured it must have been a typo when you said you wanted J=00001010 and not 01001010.

The way to do this is to do what I call bit-clip.  To get rid of a byte's 4 left most (most significant) bits, take the (bytes value) AND 31 [or... {(2^5) - 1} or  (1111)], then convert the result to zeros and ones.
You can use this general technique to get rid of left, right, or middle bits.  AND your number with (11110000) to keep the 4 MSB.  AND it with (00001111) to keep the 4 LSB.  AND it with (00111100) to keep the middle 4 bits.  AND it with (11000011) to keep the 2 most significant and the 2 least significant bits discarding the rest.  This is a very handy concept to know if you are ever trying to get at data smaller than the whole byte.

The following short code meets your "challenge" exactly.  It uses bitclipping, but does it one bit at a time instead of all at once.  I then use the SGN functiuon to turn the value of a particular bit to a one or zero instead of it's actual numeric value.  Try removing the SGN function to better understand.  It will then print a zero or the power of 2 represented by each bit rather than simply printing the 0 or 1.

The ltrim\$ and str\$ are completely not needed...but to keep you from saying "not quite..."

BTW....seph's works fine, although it doesn't print the characters.

My FOR x= 4 to 7 :NEXT is the mechanism to get only the 4 LSB.  Change 4 to 0 to get the whole byte's binary code...oh...and dont' forget to remove the silly PRINT " = 0000 "

Code:
CLS : b = 128  'incase you want the rest of the other nibble
PRINT "Enter a string ": LINE INPUT a\$
FOR z = 1 TO LEN(a\$)
PRINT UCASE\$(MID\$(a\$, z, 1)); " = 0000";
FOR t = 4 TO 7  'change 4 to 0 to get the binary of the complete ASCII code
PRINT LTRIM\$(STR\$(SGN(ASC(MID\$(a\$, z, 1)) AND (b \ (2 ^ t)))));
NEXT t
PRINT
NEXT z Logged
 Pages: 1