Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert an integer into binary

Status
Not open for further replies.

akb

Technical User
Mar 10, 2001
5
0
0
IN
Please could you tell me how to convert a positive or negative integer into binary because i've tried using BIN$
like you can use HEX$ but the BIN$ doesn't work.

Also if you could tell me how to display the GRAY code of integers 0 to 255.

Thank You!
 
My gray matter vaguely remembers hearing about gray codes in digital design class. I definitely have learned about Karnaugh maps for state minimization, so I guess I heard it in that context. Here's some info I found online:
Apparently Gray codes can be used to reduce the analog magnitude of error when bits get corrupted. Also, According to that page, Gray codes are not unique, which means there are multiple solutions to the problem. Here is one I came up with (I haven't checked it for accuracy).
CLS
max = 256
DIM temp$(0 TO max - 1)

REM initialization
x = 4
DIM gray$(0 TO x - 1)
gray$(0) = "00"
gray$(1) = "01"
gray$(2) = "11"
gray$(3) = "10"


DO
'copy to temp array because
'there is no REDIM PRESERVE in QB4.5.
FOR i = 0 TO x - 1
temp$(i) = gray$(i)
NEXT i

oldx = x
x = x * 2
REDIM gray$(0 TO x - 1)
FOR i = 0 TO oldx - 1
gray$(i) = "0" + temp$(i)
gray$(i + oldx) = "1" + temp$(oldx - 1 - i)
NEXT i
PRINT "--gray code for "; x; "numbers ------"
FOR i = 0 TO x - 1
PRINT i; gray$(i)
NEXT i
PRINT "----------------------------------"
LOOP UNTIL x = max



There is more than one method of converting a negative integer to binary. One method is known as one's complement, in which you change all the one bits into zero bits and vice versa. But the way most computers do it is called two's complement, which has the benefit that you don't get two different representations of zero. Two's complement is done by taking the bit pattern (binary representation) of the absolute value of the number, complementing it (by using the one's complement method), and adding 1. An interesting consequence of adding the one is that you get more negative numbers than positive numbers for a given number of bits (if you consider zero to be neither positive nor negative). You can see this in QBasic arrays and integers, where you can use the range -2^15 to 2^15 - 1. Here's some code that I just wrote that works for negative numbers:

num = -1

maxbits = 16
DIM a(maxbits)
CLS : binary$ = ""
FOR i = maxbits - 1 TO 0 STEP -1
REM extract bit at offset i from most significant bit
a(i) = (num AND (2 ^ i)) \ (2 ^ i)
binary$ = binary$ + LTRIM$(STR$(a(i)))
NEXT i
PRINT binary$
 
ok i dont know much about converting integers to binary
but i do have a chart:
Binary Hex Binary
0 - 0000 8 - 1000
1 - 0001 9 - 1001
2 - 0010 A - 1010
3 - 0011 B - 1011
4 - 0100 C - 1100
5 - 0101 D - 1101
6 - 0110 E - 1110
7 - 0111 F - 1111
Hope this chart can help you. [sig][/sig]
 
' Another way to convert is to convert the number to
' hexadecimal, since there is a 1-to-1 correspondance
' between hexadecimal digits and groups of 4 binary
' digits. This example illustrates this:

numbertoconvert% = -13
DIM ht$(15)
zero% = ASC("0")
FOR i% = 0 TO 15
ht$(i%) = CHR$(zero% + (i% AND 8) \ 8) + CHR$(zero% + (i% AND 4) \ 4) + CHR$(zero% + (i% AND 2) \ 2) + CHR$(zero% + (i% AND 1))
NEXT i%
hc$ = "123456789ABCDEF"
q$ = RIGHT$("000" + HEX$(numbertoconvert%), 4)
FOR i% = 1 TO 4
binary$ = binary$ + ht$(INSTR(hc$, MID$(q$, i%, 1)))
NEXT i%
PRINT binary$
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top