Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Mapping Binary data to character representation

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-10-03 : 07:42:28
John writes "Hi;

Okay folks, this ones taking me a bit(?) longer than I think it should. I need a way to convert a Binary map representation that is now stored as integer, to either decimal or character representation. So;

integer value on db is: 84
Character Map needs to be 01010100

I'm sure someone has a quick approach that will humble me today.

Thanks so much!"

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-03 : 07:44:42
[code]CREATE FUNCTION dbo.fnINT2BIN
(
@Value BIGINT
)
RETURNS VARCHAR(64)
AS

BEGIN
DECLARE @BIN VARCHAR(64),
@Index SMALLINT,
@SUM BIGINT

SELECT @BIN = '',
@Index = CEILING(LOG(@Value) / LOG(2.0)),
@SUM = POWER(2.0, @Index)

WHILE @Index >= 0
SELECT @BIN = @BIN + CASE WHEN @Value >= @SUM THEN '1' ELSE '0' END,
@Value = @Value - CASE WHEN @Value >= @SUM THEN @SUM ELSE 0 END,
@SUM = @SUM / 2,
@Index = @Index - 1

RETURN @BIN
END[/code]Good luck on the exam!
I hope 2 minutes 14 seconds was fast enough for you.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-03 : 08:49:24
>>I hope 2 minutes 14 seconds was fast enough for you.

Becuase you are very Fast

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -