Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary... Say what?
#11
This is my first post here...Hi everyone.
I recently wrote a sub (text2bin) that does what you ask. Here's a snipet of code that will operate as a stand alone program...My original code transfered a string to a bit-sequence. However, I've placed the print statements so that they meet your requirements. I used this code in an encryption program I wrote. See:
http://www.tcdn.teiher.gr/upload/downloa...fileid=678

Anyway...this does what you want.


CLS

pwa$ = "JOE"
DIM binaryarray(1 TO (8 * LEN(pwa$))) AS INTEGER


p = 1
FOR x = 1 TO LEN(pwa$)
p$ = MID$(pwa$, x, 1)
byte = ASC(p$)
PRINT p$; "=";
FOR y = 7 TO 0 STEP -1
power = 2 ^ y
binaryarray(p) = byte \ power
IF binaryarray(p) = 1 THEN byte = byte - power
PRINT binaryarray(p);
p = p + 1
NEXT y
PRINT
NEXT x
Reply
#12
toonski84 got it.
Mango: close. i said. input a word. and you binary has spaces in it
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#13
Quote:toonski84 got it.
Mango: close. i said. input a word. and you binary has spaces in it


Whatever...and your sentence has lowercase letters, where capitals are appropriate. I don't understand your post...several people have clealy demonstrated the ability to effectively convert a string to binary...it's time to move on. Anyway...it's not much of a challenge...I was just trying to help.

Here's code that gets rid of the spaces. substituet the array for a string. However, for most applications, the array, like I showed first, is more useful.

DEFINT A-Z
CLS

INPUT "Input string to convert to binary with no spaces. ", pwa$
PRINT
p = 1
FOR x = 1 TO LEN(pwa$)
p$ = MID$(pwa$, x, 1)
byte = ASC(p$)
PRINT p$; "=";
FOR y = 7 TO 0 STEP -1
power = 2 ^ y
binary$ = binary$ + LTRIM$(STR$(byte \ power))
IF (byte \ power) THEN byte = byte - power
p = p + 1
NEXT y
PRINT binary$
binary$ = ""
NEXT x

END
Reply
#14
toonski84 allready won. And yours doesn't print caps.
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#15
uh... uh...

1) whitetiger: he made a binary converter, as did everyone else. that's the main goal of the task.

2) mango: i wouldnt yo mama's mama somebody's yo mama. that just leads to a yo mama's mama's mama, in the words of my old coach.

now please, why cant we all just, get along? :wink:

oh yeah, welcome to the forum, mango Smile great to see new faces (and talent)!
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#16
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")

        adv = 128
        FOR i = 1 TO LEN(s$)
                IF n >= adv THEN
                        MID$(s$, i, 1) = "1"
                        n = n - adv
                END IF
                adv = adv \ 2
        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
earn.
Reply
#17
Quote: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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)