Qbasicnews.com
August 10, 2020, 10:11:23 PM
 Pages: [1] 2
 Author Topic: Binary... Say what?  (Read 9180 times)
whitetiger0990
__/--\__

Posts: 2964

 « 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
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
ak00ma
Ancient Guru

Posts: 669

 « Reply #1 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
 Logged

B 4 EVER
Ninkazu
Been there, done that

Posts: 1169

 « Reply #2 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.
 Logged

am an asshole. Get used to it.
whitetiger0990
__/--\__

Posts: 2964

 « Reply #3 on: May 04, 2003, 04:36:49 PM »

ak00ma... Nice but not what i asked for...
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
ak00ma
Ancient Guru

Posts: 669

 « Reply #4 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
 Logged

B 4 EVER
whitetiger0990
__/--\__

Posts: 2964

 « Reply #5 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

 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
toonski84
__/--\__

Posts: 2567

 « Reply #6 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%
 Logged

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
whitetiger0990
__/--\__

Posts: 2964

 « Reply #7 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
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
toonski84
__/--\__

Posts: 2567

 « Reply #8 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!*
 Logged

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
whitetiger0990
__/--\__

Posts: 2964

 « Reply #9 on: May 04, 2003, 06:14:11 PM »

What... you didn't pay attention
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Mango
Wandering Guru

Posts: 360

 « Reply #10 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:

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
 Logged
whitetiger0990
__/--\__

Posts: 2964

 « Reply #11 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
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Mango
Wandering Guru

Posts: 360

 « Reply #12 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
 Logged
whitetiger0990
__/--\__

Posts: 2964

 « Reply #13 on: May 07, 2003, 06:08:27 PM »

toonski84 allready won. And yours doesn't print caps.
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
toonski84
__/--\__

Posts: 2567

 « Reply #14 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)!
 Logged

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
 Pages: [1] 2