Decimal to binary and sixteen functions
A. Call the test:
Update mysheet1 set binary = DBO. inttobit (decimal)
Update mysheet2 set hexadecimal = DBO. inttohex (decimal)
B. function content
-- Decimal to binary function
Create Function DBO. inttobit (@ number INT)
Returns varchar (100)
As
Begin
Declare @ I int
Declare @ J float
Declare @ M int
Declare @ out1 varchar (1)
Declare @ out2 varchar (20)
Set @ I = @ number
Set @ out2 =''
While @ I> = 1
Begin
Set @ J = @ I/2
Set @ M = @ I % 2
Set @ I = floor (@ J)
Set @ out1 = cast (@ M as char (1 ))
Set @ out2 = @ out1 + @ out2
End
Return @ out2
End
-- Convert decimal to hexadecimal Functions
Create Function DBO. inttohex (@ I INT)
Returns varchar (15)
Begin
-- Declare @ I int
-- Set @ I = 11259375
Declare @ r varchar (10)
Set @ r =''
While @ I/16> 0
Begin
Set @ r =
(Case
When (@ I % 16) <= 9 then convert (varchar (1), @ I % 16)
When (@ I % 16) = 10 then 'A'
When (@ I % 16) = 11 then 'B'
When (@ I % 16) = 12 then 'C'
When (@ I % 16) = 13 then 'D'
When (@ I % 16) = 14 then 'E'
When (@ I % 16) = 15 then 'F'
End)
+ @ R
-- Select @ r, @ I
Set @ I = @ I/16
End
-- Select @ r, @ I
If @ I> 0
Set @ r = (Case
When (@ I % 16) <= 9 then convert (varchar (1), @ I % 16)
When (@ I % 16) = 10 then 'A'
When (@ I % 16) = 11 then 'B'
When (@ I % 16) = 12 then 'C'
When (@ I % 16) = 13 then 'D'
When (@ I % 16) = 14 then 'E'
When (@ I % 16) = 15 then 'F'
End) + @ r
-- Select @ r
Return @ r
End