转换十进制< - >二进制与T-SQL

时间:2017-05-02 11:19:12

标签: sql-server tsql

如何使用T-SQL将二进制文件(存储为varchar的基数2)转换为十进制数(基数10存储为int或bigint),反之亦然?

示例结果:

  • 7(dec)< - > 111(bin)
  • 136(dec)< - > 10001000(bin)
  • 2123942362(dec)< - > 1111110100110001100100111011010(bin)

3 个答案:

答案 0 :(得分:5)

这个答案可以处理bigint

转换为位(包含1和0的varchar)

DECLARE @input BIGINT = 9223372036854775807

;WITH N(N)AS 
(
  SELECT top 63
    POWER(cast(2 as bigint),
      ROW_NUMBER()over(ORDER BY (SELECT 1))-1)
  FROM
    (VALUES(1),(1),(1),(1))M(a),
    (VALUES(1),(1),(1),(1))L(a),
    (VALUES(1),(1),(1),(1))K(a)
)
SELECT
  COALESCE
  (
    REVERSE
    (
      ( 
        SELECT CAST(@input/N%2 as CHAR(1))
        FROM N 
        WHERE N <= @input
        for xml path(''), type 
      ).value('.', 'varchar(max)')
    )
    , '0'
  )

结果:

111111111111111111111111111111111111111111111111111111111111111

将包含位值的varchar转换为bigint

DECLARE @input varchar(max) = 
  '111111111111111111111111111111111111111111111111111111111111111'

;WITH N(V) AS 
(
  SELECT
    ROW_NUMBER()over(ORDER BY (SELECT 1))
  FROM
    (VALUES(1),(1),(1),(1))M(a),
    (VALUES(1),(1),(1),(1))L(a),
    (VALUES(1),(1),(1),(1))K(a)
)
SELECT SUM(SUBSTRING(REVERSE(@input),V,1)*POWER(CAST(2 as BIGINT), V-1))
FROM   N
WHERE  V <= LEN(@input)

结果:

9223372036854775807

答案 1 :(得分:0)

您可以从master.dbo.spt_values获取整数,并将它们用作每个数字的权重。

use master;

declare @binary     as  varchar(max)    =   '1111110100110001100100111011010';
declare @decimal    as  int             =   2123942362;

--binary --> decimal
select  
    @binary                                                     as  binaryValue
,   sum(    substring(@binary,len(@binary)-position.number,1) 
        *   power(2,position.number) )                          as  decimalValue
FROM    dbo.spt_values  AS  position
WHERE   position."type" =   'P'     --integers
    and position.number <=  len(@binary)-1
;

--decimal --> binary
select  
    @decimal                                                    as  decimalValue
,   (
        SELECT  CAST(sign(@decimal & power(2,position.number)) as varchar(max))
        FROM    dbo.spt_values  AS  position
        WHERE   position."type" =   'P'     --integers
            and position.number <=  isnull(LOG(nullif(@decimal,0),2),0)
        order by    position.number desc
        FOR XML PATH(''),TYPE
    ).value('(./text())[1]','VARCHAR(8000)')                    as  binaryValue

答案 2 :(得分:-1)

您可以创建如下两个函数,然后创建一个包装函数,该函数将有一个输入来说明您要转换的格式。

将二进制转换为十进制

CREATE FUNCTION [dbo].[BinaryToDecimal]
(
    @Input varchar(255)
)
RETURNS bigint
AS
BEGIN

    DECLARE @Cnt tinyint = 1
    DECLARE @Len tinyint = LEN(@Input)
    DECLARE @Output bigint = CAST(SUBSTRING(@Input, @Len, 1) AS bigint)

    WHILE(@Cnt < @Len) BEGIN
        SET @Output = @Output + POWER(CAST(SUBSTRING(@Input, @Len - @Cnt, 1) * 2 AS bigint), @Cnt)

        SET @Cnt = @Cnt + 1
    END

    RETURN @Output  

END

将十进制转换为二进制

CREATE FUNCTION [dbo].[DecimalToBinary]
(
    @Input bigint
)
RETURNS varchar(255)
AS
BEGIN

    DECLARE @Output varchar(255) = ''

    WHILE @Input > 0 BEGIN

        SET @Output = @Output + CAST((@Input % 2) AS varchar)
        SET @Input = @Input / 2

    END

    RETURN REVERSE(@Output)

END

希望这有帮助