如何在SAS中将HEXADECIMAL转换为DECIMAL?

时间:2017-02-01 12:02:41

标签: sql sas sas-gtl

我有: 每4个位置具有十六进制值的字符串

00F701C101C900EC01E001D2

我需要:

将这些值与4个位置中的4个分开并以这种方式转换为十进制数字:

247,449,457,480,466

我的列最多可以有1200个十六进制位置

你能帮助我吗?

韩国社交协会!!!

2 个答案:

答案 0 :(得分:1)

这有效:

data out;
    hex = "00F701C101C900EC01E001D2";
    do while(hex ne "");
        valHex = substr(hex, 1, 4);
        hex = substr(hex, 5);
        valDec = input(valHex, hex4.);
        output;
    end;
run;

但您需要为真正的解决方案添加更多错误检查等。

答案 1 :(得分:0)

对不起,我要禁食。这是SQL-Server语法,可能不适合你,但你可能会有一个想法......

试试这样:

DECLARE @YourString VARCHAR(100)='00F701C101C900EC01E001D2';
WITH Separated AS
(
    SELECT CAST(LEFT(@YourString,4) AS VARCHAR(MAX)) AS SourceString
          ,CAST(SUBSTRING(@YourString,5,10000) AS VARCHAR(MAX)) AS RestString
    UNION ALL
    SELECT LEFT(RestString,4) 
          ,SUBSTRING(RestString,5,10000)
    FROM Separated
    WHERE LEN(RestString)>=4
)
SELECT *
      ,CAST(sys.fn_cdc_hexstrtobin(SourceString) AS VARBINARY(2))
      ,CAST(CAST(sys.fn_cdc_hexstrtobin(SourceString) AS VARBINARY(2)) AS INT)
FROM Separated

结果

+--------------+----------------------+--------------------+--------------------+
| SourceString | RestString           | (Kein Spaltenname) | (Kein Spaltenname) |
+--------------+----------------------+--------------------+--------------------+
| 00F7         | 01C101C900EC01E001D2 | 0x00F7             | 247                |
+--------------+----------------------+--------------------+--------------------+
| 01C1         | 01C900EC01E001D2     | 0x01C1             | 449                |
+--------------+----------------------+--------------------+--------------------+
| 01C9         | 00EC01E001D2         | 0x01C9             | 457                |
+--------------+----------------------+--------------------+--------------------+
| 00EC         | 01E001D2             | 0x00EC             | 236                |
+--------------+----------------------+--------------------+--------------------+
| 01E0         | 01D2                 | 0x01E0             | 480                |
+--------------+----------------------+--------------------+--------------------+
| 01D2         |                      | 0x01D2             | 466                |
+--------------+----------------------+--------------------+--------------------+