Author Topic: Decimal / Binary converter  (Read 8551 times)

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Decimal / Binary converter
« on: July 08, 2021, 10:28:33 pm »
Code: QB64: [Select]
  1. CONST TRUE = 1
  2. CONST FALSE = 0
  3. DIM decimal AS LONG
  4. DIM binaryNum$
  5.  
  6. CALL Menu
  7.  
  8. SUB Menu
  9.     DO
  10.         DO
  11.             COLOR 10, 0
  12.             PRINT: PRINT: PRINT
  13.             PRINT "1 - Decimal to binary"
  14.  
  15.             PRINT "2 - Binary to decimal"
  16.             which$ = INPUT$(1)
  17.         LOOP UNTIL which$ = "1" OR which$ = "2"
  18.         IF which$ = "1" THEN
  19.             CALL DtoB
  20.         ELSE
  21.             CALL BtoD
  22.         END IF
  23.     LOOP
  24.  
  25. SUB BtoD
  26.     PRINT: PRINT: PRINT
  27.     INPUT "Type in the binary number: ", binaryNumber$
  28.     power = 0
  29.     FOR digit = LEN(binaryNumber$) TO 1 STEP -1
  30.         onOff = VAL(MID$(binaryNumber$, digit, 1))
  31.         decimal = decimal + (onOff * (2 ^ power))
  32.         power = power + 1
  33.     NEXT digit
  34.     PRINT
  35.     PRINT
  36.     PRINT "Decimal equivalent is " + S$(decimal)
  37.     PRINT
  38.     PRINT
  39.     PRINT
  40.     PRINT "Press any key for another conversion or [ESC] to end.": CALL P(TRUE)
  41.  
  42. SUB DtoB
  43.     PRINT: PRINT: PRINT
  44.     INPUT "Type in a decimal number: ", decimal
  45.     binaryString$ = ""
  46.     DO
  47.         q = INT(decimal / 2): r = decimal MOD 2
  48.         binaryString$ = S$(r) + binaryString$
  49.         decimal = q
  50.     LOOP UNTIL q = 0
  51.     PRINT binaryString$
  52.     PRINT
  53.     PRINT
  54.     PRINT
  55.     PRINT "Press any key for another conversion or [ESC] to end.": CALL P(TRUE)
  56.  
  57. SUB P (escOrNot)
  58.     pause$ = INPUT$(1)
  59.     IF pause$ = CHR$(27) AND escOrNot = TRUE THEN END
  60.  
  61. FUNCTION S$ (number)
  62.     rtn$ = ""
  63.     rtn$ = STR$(number)
  64.     rtn$ = LTRIM$(rtn$)
  65.     S$ = rtn$
  66.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Decimal / Binary converter
« Reply #1 on: July 09, 2021, 05:49:05 pm »
Hi Jaze
fine tool!
What do you think about an Inform version?
Programming isn't difficult, only it's  consuming time and coffee

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Decimal / Binary converter
« Reply #2 on: July 09, 2021, 06:50:39 pm »
Cool converter. Nicely done!
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #3 on: July 09, 2021, 06:55:33 pm »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Decimal / Binary converter
« Reply #4 on: July 09, 2021, 07:05:59 pm »
bplus: Point well made... Counterpoint. Will BIN$() teach the user 'how' it is converted? Nothing wrong with using the command but knowing how it is done is beneficial if one is using a 'basic' that does not utilize the BIN$() function...
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #5 on: July 09, 2021, 09:38:41 pm »
bplus: Point well made... Counterpoint. Will BIN$() teach the user 'how' it is converted? Nothing wrong with using the command but knowing how it is done is beneficial if one is using a 'basic' that does not utilize the BIN$() function...

Code: QB64: [Select]
  1.     DIM temp~&&, charPos%, highPos%
  2.     '--- init ---
  3.     temp~&& = value&&
  4.     BIN$ = STRING$(64, "0"): charPos% = 64: highPos% = 64  
  5. 'BIN$ is 0's with 1's to be inserted by way of MID$
  6.   DO
  7.  ' starting left and working right insert 1's where AND 1 is True then move place variables down 1 to right.
  8.         IF (temp~&& AND 1) THEN MID$(BIN$, charPos%, 1) = "1": highPos% = charPos%
  9.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2 ' << tracking positions
  10.     LOOP UNTIL temp~&& = 0
  11.  
It's all Basic even those scary looking suffix :)

The adjustment for negative numbers is honestly over my head :( but this allows negative number conversions :).
Code: QB64: [Select]
  1.     IF value&& < 0 THEN
  2.         IF -value&& < &H0080000000~&& THEN highPos% = 33
  3.         IF -value&& < &H0000008000~&& THEN highPos% = 49
  4.         IF -value&& < &H0000000080~&& THEN highPos% = 57
  5.     END IF
Do you have to know how a TV works before you use it?

The final line cuts BIN$ down to size before returning BIN$ by function name.
Code: QB64: [Select]
  1. BIN$ = MID$(BIN$, highPos%)

« Last Edit: July 09, 2021, 10:18:37 pm by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Decimal / Binary converter
« Reply #6 on: July 09, 2021, 10:43:00 pm »
CRT, Plasma, LCD or OLED? Nah, kidding! Just curious as to how some commands work... lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #7 on: July 10, 2021, 08:20:25 am »
You can make some cool patterns by converting fractions (same denominator) to binary and letting 0 make a white square and 1 make a black square ( or for Johnno56 a Blue square ). eg 1/15, 2/15, 3/15... 14/15 stacked down the page.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Decimal / Binary converter
« Reply #8 on: July 10, 2021, 08:39:51 am »
@bplus
please Screenshot!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #9 on: July 10, 2021, 03:53:47 pm »
@bplus
please Screenshot!

  [ You are not allowed to view this attachment ]  

Also makes for cool optical illusion, looks like non parallel lines (specially last set) :)
« Last Edit: July 10, 2021, 03:55:25 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #10 on: July 10, 2021, 04:31:39 pm »
Something tells me this work would all be a lot easier if you guys used the BIT routines; _SETBIT and _READBIT.

Just saying.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #11 on: July 10, 2021, 04:42:26 pm »
Something tells me this work would all be a lot easier if you guys used the BIT routines; _SETBIT and _READBIT.

Just saying.

Yeah? so show us 27/101 in Binary :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #12 on: July 10, 2021, 06:41:06 pm »
This is start of what I am getting:
.0100010001101111100001100101011000101101100111111011

What are you guys getting?

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Decimal / Binary converter
« Reply #13 on: July 10, 2021, 10:10:25 pm »
For 27/101, and if I set the value of the MSbit to 1, I get:

0010 0010 0011 0111 1100 0011 0010 1011 0001 0110 1100 1111 11011

which are then followed by binary zeroes. Which implies to me that I've reached the limit of the accuracy of _Float.

This is where I compute the binary numbers, bit by bit. Also, my super duper program also gives the hex equivalent.

hex 2237C32B16CFD800

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #14 on: July 10, 2021, 10:44:39 pm »
Yeah? so show us 27/101 in Binary :)

you do realize that binary only deals in 1 and 0 right. 27/101 is not a 1 or a 0
Granted after becoming radioactive I only have a half-life!