# Thread: int to binary conversion

1. Registered User
Join Date
Oct 2002
Posts
5

## Unanswered: int to binary conversion

I'm trying to print an int as binary but it keeps insisting on printing the hex form of the number...even when I convert it to BINARY. I want to see 1's and 0's not 0x3FAD45. please help.

thanks

2. Registered User
Join Date
Jan 2003
Location
Posts
80
The binary datatype is not base 2, it's base 16. It's there to handle things like icons or other very small graphics.

I don't know of a base 2 datatype. You'd probably have to do a conversion in your select statement.

3. Registered User
Join Date
Oct 2002
Posts
5

## Re: int to binary conversion

ok, so I had to make code that builds the base 2 number into a varchar and then prints it. A less then ideal hack.

set @num = 43069
set @i = 15
set @numstr = ''
while (@i>=0)
begin
if power(2,@i) & @toy = 0
set @toystr = @toystr + '0'
else
set @toystr = @toystr + '1'
set @i = @i - 1
end
print @num
print @numstr

4. Registered User
Join Date
Jan 2003
Location
Posts
80
Does that code work?

If not, try this:

DECLARE @num INT
DECLARE @i INT
DECLARE @numstr VARCHAR(100)

set @num = 43069
set @numstr = ''
print @num
while (@num > 0)
begin
if (@num % 2) = 1
set @numstr = '1' + @numstr
else
set @numstr = '0' + @numstr
set @num = @num / 2
end
print @numstr
Last edited by bcrockett; 02-17-03 at 19:22.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•