Qbasicnews.com

QbasicNews.Com => Challenges => Topic started by: whitetiger0990 on May 04, 2003, 03:52:17 PM



Title: Binary... Say what?
Post by: whitetiger0990 on May 04, 2003, 03:52:17 PM
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

Have fun!

Also... I have one that works... It's not perfect though.
BTW: No LIBs

EDIT: If you need help or don't know what binary is... PM me and ill explain it best as i can


Title: Binary... Say what?
Post by: ak00ma on May 04, 2003, 04:16:55 PM
Here's my one:

Code:

DECLARE FUNCTION Dec2Bin$ (Dec%)
DECLARE FUNCTION Word2Bin$ (Word$)

PRINT Word2Bin$("Hello")

FUNCTION Dec2Bin$ (Dec%)
 Bin$ = ""
 DO
  Bin$ = LTRIM$(STR$(Dec% MOD 2)) + Bin$
  Dec% = INT(Dec% / 2)
  IF Dec% = 0 THEN EXIT DO
 LOOP
 IF LEN(Bin$) < 8 THEN Bin$ = STRING$(8 - LEN(Bin$), "0") + Bin$
 Dec2Bin$ = Bin$
END FUNCTION

FUNCTION Word2Bin$ (Word$)
 Bin$ = ""
 FOR n% = 1 TO LEN(Word$)
  Bin$ = Bin$ + Dec2Bin$(ASC(MID$(Word$, n%, 1)) - 64)
 NEXT n%
 Word2Bin$ = Bin$
END FUNCTION


It's a 3-Minutes-Work


Title: Binary... Say what?
Post by: Ninkazu on May 04, 2003, 04:34:08 PM
I think I may have my old program somewhere..... nope, I guess I must have deleted it. It would change .txt files into hex or binary. Quite cool. sorry I couldn't find it.


Title: Binary... Say what?
Post by: whitetiger0990 on May 04, 2003, 04:36:49 PM
ak00ma... Nice but not what i asked for...


Title: Binary... Say what?
Post by: ak00ma on May 04, 2003, 04:48:36 PM
This one maybe:

Code:

DECLARE FUNCTION Dec2Bin$ (Dec%)
DECLARE Sub Word2Bin (Word$)

Input Word$
Word2Bin(Word$)

FUNCTION Dec2Bin$ (Dec%)
Bin$ = ""
DO
  Bin$ = LTRIM$(STR$(Dec% MOD 2)) + Bin$
  Dec% = INT(Dec% / 2)
  IF Dec% = 0 THEN EXIT DO
LOOP
IF LEN(Bin$) < 8 THEN Bin$ = STRING$(8 - LEN(Bin$), "0") + Bin$
Dec2Bin$ = Bin$
END FUNCTION

SUB Word2Bin (Word$)
 Bin$ = ""
 FOR n% = 1 TO LEN(Word$)
  PRINT MID$(Word$, n%, 1) + "=" + Dec2Bin$(ASC(MID$(Word$, n%, 1)) - 64)
 NEXT n%
END SUB


Title: Binary... Say what?
Post by: whitetiger0990 on May 04, 2003, 05:15:47 PM
Even closer... but I want to inout a word... and also...

I want to be able to unout Joe
and it prints out

J = 00001010
O = 00001111
E = 00000101

Your missing something


Title: Binary... Say what?
Post by: toonski84 on May 04, 2003, 06:05:37 PM
Code:
input word$
print "J = 00001010"
print "O = 00001111"
print "E = 00000101"


 :rotfl:

i think the goal of this program have been done time and again.

Code:
input word$
for y% = 1 to len(word$)
  value% = asc(mid$(word$, y%, 1))
  binary$ = ""
  for x% = 0 to 7
    if value% and 1 then binary$ = "1" + binary$ else binary$ = "0" + binary$
    value% = value% \ 2
  next x%
  print ucase$(MID$(word$, y%, 1)); " = "; binary$
next y%


Title: Binary... Say what?
Post by: whitetiger0990 on May 04, 2003, 06:10:28 PM
toonski84: Not quite... Didn't print out the letter.

ak00ma: Yours didn't print out the CAPITAL letter


Title: Binary... Say what?
Post by: toonski84 on May 04, 2003, 06:12:35 PM
printed it out for me...

edit: oh, you want the capital letter?  (anal-retentive bastard...)  :lol:
edit: there... you have your capital letter!  now be happy! *throws a screaming fit!*


Title: Binary... Say what?
Post by: whitetiger0990 on May 04, 2003, 06:14:11 PM
What... you didn't pay attention


Title: Binary... Say what?
Post by: Mango on May 07, 2003, 03:53:09 AM
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/download.asp?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


Title: Binary... Say what?
Post by: whitetiger0990 on May 07, 2003, 08:29:58 AM
toonski84 got it.
Mango: close. i said. input a word.  and you binary has spaces in it


Title: Binary... Say what?
Post by: Mango on May 07, 2003, 11:06:03 AM
Quote from: "whitetiger0990"
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


Title: Binary... Say what?
Post by: whitetiger0990 on May 07, 2003, 06:08:27 PM
toonski84 allready won. And yours doesn't print caps.


Title: Binary... Say what?
Post by: toonski84 on May 07, 2003, 07:08:37 PM
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 :)  great to see new faces (and talent)!


Title: Binary... Say what?
Post by: seph 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")

        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


Title: Re: Binary... Say what?---whitetiger
Post by: Mango 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