Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the amount of symbols in a string
#1
I am making a program to convert binary to decimal. I need a way to know how many 1's and 0's are in a string that the user has inputed, so that I can use LEFT$, RIGHT$ and MID$ to take apart the number and convert it to decimal.

Can anyone help?

Thanks.
-yah
Reply
#2
What happened to that last guys post?!?! I was just coming back to get it and try it and suddenly its not there... I think it was LIN(stringx) or somthing...
-yah
Reply
#3
Sorry, that was me. I deleted it because I thought I misread your post. Are you after something that will tell you separately how many 1s and 0s there are in a string? Or just something to tell you the length of the whole string? In that case, LEN(string$) will work.
Reply
#4
Yeah, All I want to is know the length of the string. Ill go try LEN now.


Thanks!
-yah
Reply
#5
Just finished the program. Thanks to you, it works great!

If you take a look, tell me if you find any bugs.

Here it tis:

Code:
'Translates binary numbers up to 10 digits in length into decimal numbers.
CLS
INPUT bin$
IF LEFT$(bin$, 1) = "0" THEN
PRINT "Please do not put zeros in front of your number"
END IF
IF LEN(bin$) > 10 THEN
PRINT "You can only input numbers up to 10 digits in length"
END IF
IF LEN(bin$) = 1 THEN GOTO 1
IF LEN(bin$) = 2 THEN GOTO 2
IF LEN(bin$) = 3 THEN GOTO 3
IF LEN(bin$) = 4 THEN GOTO 4
IF LEN(bin$) = 5 THEN GOTO 5
IF LEN(bin$) = 6 THEN GOTO 6
IF LEN(bin$) = 7 THEN GOTO 7
IF LEN(bin$) = 8 THEN GOTO 8
IF LEN(bin$) = 9 THEN GOTO 9
IF LEN(bin$) = 10 THEN GOTO 10

1 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
PRINT I1%
END

2 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 1, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
PRINT I1% + I2%
END

3 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
PRINT I1% + I2% + I3%
END

4 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
PRINT I1% + I2% + I3% + I4%
END

5 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 4, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I5% = 16
        ELSE I5% = 0
END IF
PRINT I1% + I2% + I3% + I4% + I5%
END

6 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 5, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 4, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I5% = 16
        ELSE I5% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I6% = 32
        ELSE I6% = 0
END IF
PRINT I1% + I2% + I3% + I4% + I5% + I6%
END

7 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 6, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 5, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF MID$(bin$, 4, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I5% = 16
        ELSE I5% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I6% = 32
        ELSE I6% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I7% = 64
        ELSE I7% = 0
END IF
PRINT I1% + I2% + I3% + I4% + I5% + I6% + I7%
END

8 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 7, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 6, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF MID$(bin$, 5, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
IF MID$(bin$, 4, 1) = "1" THEN
        I5% = 16
        ELSE I5% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I6% = 32
        ELSE I6% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I7% = 64
        ELSE I7% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I8% = 128
        ELSE I8% = 0
END IF
PRINT I1% + I2% + I3% + I4% + I5% + I6% + I7% + I8%
END

9 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 8, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 7, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF MID$(bin$, 6, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
IF MID$(bin$, 5, 1) = "1" THEN
        I5% = 16
        ELSE I5% = 0
END IF
IF MID$(bin$, 4, 1) = "1" THEN
        I6% = 32
        ELSE I6% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I7% = 64
        ELSE I7% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I8% = 128
        ELSE I8% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I9% = 256
        ELSE I9% = 0
END IF
PRINT I1% + I2% + I3% + I4% + I5% + I6% + I7% + I8% + I9%
END

10 IF RIGHT$(bin$, 1) = "1" THEN
        I1% = 1
        ELSE I1% = 0
END IF
IF MID$(bin$, 9, 1) = "1" THEN
        I2% = 2
        ELSE I2% = 0
END IF
IF MID$(bin$, 8, 1) = "1" THEN
        I3% = 4
        ELSE I3% = 0
END IF
IF MID$(bin$, 7, 1) = "1" THEN
        I4% = 8
        ELSE I4% = 0
END IF
IF MID$(bin$, 6, 1) = "1" THEN
        I5% = 16
        ELSE I5% = 0
END IF
IF MID$(bin$, 5, 1) = "1" THEN
        I6% = 32
        ELSE I6% = 0
END IF
IF MID$(bin$, 4, 1) = "1" THEN
        I7% = 64
        ELSE I7% = 0
END IF
IF MID$(bin$, 3, 1) = "1" THEN
        I8% = 128
        ELSE I8% = 0
END IF
IF MID$(bin$, 2, 1) = "1" THEN
        I9% = 256
        ELSE I9% = 0
END IF
IF LEFT$(bin$, 1) = "1" THEN
        I10% = 512
        ELSE I10% = 0
END IF
PRINT I1% + I2% + I3% + I4% + I5% + I6% + I7% + I8% + I9% + I10%
END
'Whew!
-yah
Reply
#6
Why don't you think about this logically? From what it looks like this is probably an assignment... because only a teacher would force you to do something by giving you something that can be done in about 3 lines.

You already know how a binary works.
Code:
128 64 32 16 8 4 2 1
0   0  0  0  1 0 1 1

You take the digits above the ones and add them together. soo.. 11. You should know that all those numbers are exponents of 2. the most rightmost is 2^0, then 2^1, then 2^2, ect. Notice how it increases by one each time. This can be accomplished with a FOR loop.
Code:
for i = 1 to len(a$)
if a$ was "1011" then it i will go 1,2,3,4. Good. Now we have the exponents.. but we only want the ones with the 1's, no 0's.
Code:
val(mid(a$,len(a$)-i,1))
This returns the value of the character of a which is i positions away from the most right. So.. when i is 2 it'd return 1. when i is 3. it's return 0.

Now we have the 0 and the 1! You know what to do with those..? Yes, that's right.. Multiply it to the exponent
Code:
2^i*val(mid(a$,len(a$)-i,1))
woo, now when the for loop goes through "1011" it goes 1, 2, 0, 8. Know what to do to that? Add them together.
Code:
b=b+2^i*val(mid(a$,len(a$)-i,1))

Tadaa. I'm not going to give you the full code... that'd for you. This looks like an assignment and I'm not going to give it to you straight out. but I hope you learned something from this.
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#7
And if your teacher lets you use FreeBASIC, you can do this:

Code:
decimal = val("&b" + binary$)

Big Grin
Reply
#8
Actualy, this was only an assignment in a way. We had to do this using VisualBasic, and I wanted to try it with qb in sorta the same way we did it in vb. Thanks for the tips though! Ill finish that one you were showing me.
-yah
Reply
#9
Quote:'Whew!

"Whew" is right! What a lot of code. And it had restrictions such as
------------------
IF LEFT$(bin$, 1) = "0" THEN
PRINT "Please do not put zeros in front of your number"
END IF
------------------
But note that you did not enforce those restrictions. You just print an error message and continue.

Below is your program reduced in size, yet allows any size input and leading zeros.

I use "LINE INPUT" rather than "INPUT". That way you can trap user errors like including commas in the input.

Try to avoid GOTO and repetitive code.

Mac

Code:
DIM c AS STRING * 1
DIM v AS DOUBLE
CLS : LOCATE 3, 10
PRINT "Translates binary numbers of any size into decimal numbers."
LOCATE 5, 1
DO: GOSUB Do1: LOOP WHILE bin$ <> ""
SYSTEM

Do1:
PRINT : v = 0
LINE INPUT "Binary number: "; bin$: IF bin$ = "" THEN RETURN
FOR i = 1 TO LEN(bin$)
  c = MID$(bin$, i, 1)
  IF INSTR("01", c) = 0 THEN PRINT "Use only 1s and 0s": RETURN
  v = (v + v) + VAL(c)
NEXT i
PRINT v
RETURN
Reply
#10
I redid it my own way, but I keep on getting an error message. Iv'e been through it a few times and cant find any more errors.

Code:
CLS
INPUT BIN$
start% = 1
PWR2% = 1
RECURSION% = LEN(BIN$)

RECURSION% = RECURSION% - 1
NUM% = LEN(BIN$)
IF MID$(BIN$, NUM%, 1) = "1" THEN
  TOTAL% = PWR2%
  END IF
IF MID$(BIN$, NUM%, 1) = "0" THEN
  TOTAL% = 0
END IF
NUM% = NUM% - 1

DO WHILE RECURSION% > 0

  start% = 0
  RECURSION% = RECURSION% - 1
  

  IF MID$(BIN$, NUM%, 1) = "1" AND start% = 0 THEN
    TOTAL% = PWR2% * 2 + TOTAL
    NUM% = NUM% - 1
  END IF

IF MID$(BIN$, NUM%, 1) = "0" AND start% = 0 THEN
    TOTAL% = TOTAL%
    PWR2% = PWR2% * 2
    NUM% = NUM% - 1
  END IF

LOOP

CLS
PRINT TOTAL%
-yah
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)