進制轉換

原文: 進制轉換

1. 背景

最近在整理關於字符集的筆記(整理後可能會作成一個系列的博客,敬請留意:)),其中涉及不一樣進制之間轉換的運算。因此乾脆就把幾種經常使用的進制換算作成函數,方便直接調用。如下函數均以T-SQL的語法實現。
 

2. 列表

  • 十進制轉二進制
  • 十進制轉八進制
  • 十進制轉十六進制
  • 十進制轉三十六進制
  • 二進制轉十進制
  • 八進制轉十進制
  • 十六進制轉十進制
  • 三十六進制轉十進制
其中非十進制之間的轉換,好比二進轉十六進制,或十六進制轉二進制等,均可以經過先成十進後再轉。因此只要搞定這幾個轉換,經常使用的進制互換都實現了。
 
 

 十進制轉二進制

CREATE FUNCTION [dbo].[ufn_bigint2bin] ( @value BIGINT )
RETURNS VARCHAR(64)
/*十進制轉換爲二進制*/
AS
    BEGIN
        DECLARE @seq CHAR(2);
        DECLARE @result VARCHAR(64);
        DECLARE @digit CHAR(1);
        DECLARE @power INT;

        SET @power = 2;   
        SET @seq = '01';
        SET @result = SUBSTRING(@seq, ( @value % @power ) + 1, 1);

        WHILE @value > 0
            BEGIN
                SET @digit = SUBSTRING(@seq, ( ( @value / @power ) % @power ) + 1, 1);
                SET @value = @value / @power;
                IF @value <> 0
                    SET @result = @digit + @result;
            END; 

        RETURN @result;
    END;
GO

Code-1: 十進制轉二進制html

 

 

十進制轉八進制

CREATE FUNCTION [dbo].[ufn_bigint2oct] ( @value BIGINT )
RETURNS VARCHAR(64)
/*十進制轉換爲八進制*/
AS
    BEGIN
        DECLARE @seq CHAR(8);
        DECLARE @result VARCHAR(64);
        DECLARE @digit CHAR(1);
        DECLARE @power INT;

        SET @power = 8;    
        SET @seq = '01234567';
        SET @result = SUBSTRING(@seq, ( @value % @power ) + 1, 1);

        WHILE @value > 0
            BEGIN
                SET @digit = SUBSTRING(@seq, ( ( @value / @power ) % @power ) + 1, 1);
                SET @value = @value / @power;
                IF @value <> 0
                    SET @result = @digit + @result;
            END; 

        RETURN @result;
    END;
GO

Code-2: 十進制轉八進制git

 

 

十進制轉十六進制

1. 內置函數

/*注意值不能超過@i<=2147483647(7FFFFFFF)*/
DECLARE @i INT = 255;
SELECT CONVERT(VARBINARY, @i);
SELECT CAST(@i AS VARBINARY);
SELECT  master.dbo.fn_varbintohexstr(@i);
GO

 Code-3: 內置函數十進制轉十六進制ide

 

/*若是@i>2147483647,最大不能超過9223372036854775807(7FFFFFFFFFFFFFFF)*/
DECLARE @i BIGINT= 4294967295;
SELECT CONVERT(VARBINARY, @i);
SELECT CAST(@i AS VARBINARY);
SELECT  master.dbo.fn_varbintohexstr(@i);
GO

 Code-4: 內置函數十進制轉十六進制函數

 
/*若是直接輸入具體數值,@i<=2147483647*/
SELECT CONVERT(VARBINARY, 2147483647);
SELECT CAST(2147483647 AS VARBINARY);
SELECT  master.dbo.fn_varbintohexstr(2147483647);
GO

 Code-5: 內置函數十進制轉十六進制spa

 

2. 自定義函數

CREATE FUNCTION [dbo].[ufn_bigint2hex] ( @value BIGINT )
RETURNS VARCHAR(64)
/*十進制轉換爲十六進制*/
AS
    BEGIN
        DECLARE @seq CHAR(16);
        DECLARE @result VARCHAR(64);
        DECLARE @digit CHAR(1);
        DECLARE @power INT;
        
        SET @power = 16;
        SET @seq = '0123456789ABCDEF';
        SET @result = SUBSTRING(@seq, ( @value % @power ) + 1, 1);

        WHILE @value > 0
            BEGIN
                SET @digit = SUBSTRING(@seq, ( ( @value / @power ) % @power ) + 1, 1);
                SET @value = @value / @power;
                IF @value <> 0
                    SET @result = @digit + @result;
            END; 

        RETURN @result;
    END;
GO

Code-6: 十進制轉十六進制code

 

 

十進制轉三十六進制

CREATE FUNCTION [dbo].[ufn_bigint236hex] ( @value BIGINT )
RETURNS VARCHAR(64)
/*十進制轉換爲三十六進制*/
AS
    BEGIN
        DECLARE @seq CHAR(36);
        DECLARE @result VARCHAR(64);
        DECLARE @digit CHAR(1);
        DECLARE @power INT;

        SET @power = 36;    
        SET @seq = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        SET @result = SUBSTRING(@seq, ( @value % @power ) + 1, 1);

        WHILE @value > 0
            BEGIN
                SET @digit = SUBSTRING(@seq, ( ( @value / @power ) % @power ) + 1, 1);
                SET @value = @value / @power;
                IF @value <> 0
                    SET @result = @digit + @result;
            END; 

        RETURN @result;
    END;
GO

Code-7: 十進制轉三十六進制orm

 

 

二進轉十進制

CREATE FUNCTION [dbo].[ufn_bin2bigint] ( @bin_string VARCHAR(63) )
RETURNS BIGINT
/* 二進制轉十進制 */
AS
    BEGIN 
        DECLARE @result BIGINT;

        DECLARE @i INT
           ,@len INT;

        DECLARE @power BIGINT;
        SET @power = 2;

        SELECT  @i = 0
               ,@result = 0
               ,@bin_string = RTRIM(LTRIM(UPPER(@bin_string)));

        SET @len = LEN(@bin_string); 

        IF ( @len = 63 )
            BEGIN 
                IF ( UNICODE(SUBSTRING(@bin_string, 1, 1)) > 49 )
                    BEGIN 
                        RETURN NULL;
                    END; 
            END; 

        WHILE ( @i < @len )
            BEGIN 
                IF (
                     ( SUBSTRING(@bin_string, @len - @i, 1) NOT BETWEEN '0' AND '1' )
                   )
                    BEGIN 
                        SET @result = NULL;
                        BREAK;
                    END; 
 
                SET @result = @result + ( CHARINDEX(SUBSTRING(@bin_string, @len - @i, 1), '01') - 1 )
                              * CAST(POWER(@power, @i) AS BIGINT);
                SET @i = @i + 1; 
            END; 
 
        RETURN @result; 
    END;
GO

Code-8: 二進制轉十進制htm

 

 

八進制轉十進制

CREATE FUNCTION [dbo].[ufn_oct2bigint] ( @oct_string VARCHAR(21) )
RETURNS BIGINT
/*八進制轉十進制*/
AS
    BEGIN 
        DECLARE @result BIGINT;

        DECLARE @i INT
           ,@len INT;

        DECLARE @power BIGINT;
        SET @power = 8;

        SELECT  @i = 0
               ,@result = 0
               ,@oct_string = RTRIM(LTRIM(UPPER(@oct_string)));

        SET @len = LEN(@oct_string); 

        IF ( @len = 21 )
            BEGIN 
                IF ( UNICODE(SUBSTRING(@oct_string, 1, 1)) > 55 )
                    BEGIN 
                        RETURN NULL;
                    END; 
            END; 

        WHILE ( @i < @len )
            BEGIN 
                IF (
                     ( SUBSTRING(@oct_string, @len - @i, 1) NOT BETWEEN '0' AND '7' )                     
                   )
                    BEGIN 
                        SET @result = NULL;
                        BREAK;
                    END; 
 
                SET @result = @result + ( CHARINDEX(SUBSTRING(@oct_string, @len - @i, 1), '01234567') - 1 )
                              * CAST(POWER(@power, @i) AS BIGINT);
                SET @i = @i + 1; 
            END; 
 
        RETURN @result; 
    END;
GO

Code-9: 八進制轉十進制blog

 

 

十六進制轉十進制

1. 內置函數

/*16進制是偶數位的,而且不包含前綴0x*/
DECLARE @s VARCHAR(16) = 'FF';
SELECT  CONVERT(BIGINT, CONVERT(VARBINARY, CAST(N'0x' + @s AS VARCHAR), 1));

 Code-10: 內置函數十六進制轉十進制get

 

2. 自定義函數

CREATE FUNCTION [dbo].[ufn_hex2bigint] ( @hex_string VARCHAR(16) )
RETURNS BIGINT
/*十六進制轉十進制*/
AS
    BEGIN 
        DECLARE @result BIGINT;

        DECLARE @i INT
           ,@len INT;

        DECLARE @power BIGINT;
        SET @power = 16;

        SELECT  @i = 0
               ,@result = 0
               ,@hex_string = RTRIM(LTRIM(UPPER(@hex_string)));

        SET @len = LEN(@hex_string); 

        IF ( @len = 16 )
            BEGIN 
                IF ( UNICODE(SUBSTRING(@hex_string, 1, 1)) > 55 )
                    BEGIN 
                        RETURN NULL;
                    END; 
            END; 

        WHILE ( @i < @len )
            BEGIN 
                IF (
                     ( SUBSTRING(@hex_string, @len - @i, 1) NOT BETWEEN '0' AND '9' )
                     AND ( SUBSTRING(@hex_string, @len - @i, 1) NOT BETWEEN 'A' AND 'F' )
                   )
                    BEGIN 
                        SET @result = NULL;
                        BREAK;
                    END; 
 
                SET @result = @result + ( CHARINDEX(SUBSTRING(@hex_string, @len - @i, 1), '0123456789ABCDEF') - 1 )
                              * CAST(POWER(@power, @i) AS BIGINT);
                SET @i = @i + 1; 
            END; 
 
        RETURN @result; 
    END;
GO

Code-11: 十六進制轉十進制

 

 

三十六進制轉十進制

CREATE FUNCTION [dbo].[ufn_36hex2bigint] ( @thirtysix_string VARCHAR(13) )
RETURNS BIGINT
/*三十六進制轉十進制*/
AS
    BEGIN 
        DECLARE @result BIGINT;

        DECLARE @i INT
           ,@len INT;

        DECLARE @power BIGINT;
        SET @power = 36;

        SELECT  @i = 0
               ,@result = 0
               ,@thirtysix_string = RTRIM(LTRIM(UPPER(@thirtysix_string)));

        SET @len = LEN(@thirtysix_string); 

        IF ( @len = 13 )
            BEGIN 
                IF ( UNICODE(SUBSTRING(@thirtysix_string, 1, 1)) > 49 )
                    BEGIN 
                        RETURN NULL;
                    END; 
            END; 

        WHILE ( @i < @len )
            BEGIN 
                IF (
                     ( SUBSTRING(@thirtysix_string, @len - @i, 1) NOT BETWEEN '0' AND '9' )   
                     AND ( SUBSTRING(@thirtysix_string, @len - @i, 1) NOT BETWEEN 'A' AND 'Z' )                
                   )
                    BEGIN 
                        SET @result = NULL;
                        BREAK;
                    END; 
 
                SET @result = @result + ( CHARINDEX(SUBSTRING(@thirtysix_string, @len - @i, 1), '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') - 1 )
                              * CAST(POWER(@power, @i) AS BIGINT);
                SET @i = @i + 1; 
            END; 
 
        RETURN @result; 
    END;
GO

Code-12: 三十六進制轉十進制

 

 

3. 使用示範

--十進轉其餘進制
SELECT  dbo.ufn_bigint2bin(255) AS bigint2bin
       ,dbo.ufn_bigint2oct(255) AS bigint2oct
       ,dbo.ufn_bigint2hex(255) AS bigint2hex
       ,dbo.ufn_bigint236hex(255) AS bigint236hex;

--其餘進制轉十進制
--注意傳入參數是字符型的
SELECT  dbo.ufn_bin2bigint('11111111') AS bin2bigint
       ,dbo.ufn_oct2bigint('377') AS oct2bigint
       ,dbo.ufn_hex2bigint('FF') AS hex2bigint
       ,dbo.ufn_36hex2bigint('73') AS thirty_six_2bigint;

--二進制轉十六進制
SELECT  dbo.ufn_bigint2hex(dbo.ufn_bin2bigint('11111111')) AS bin2hex;
GO

Code-13: 使用示範

 

Figure-1: 使用示範

相關文章
相關標籤/搜索