我有: 每4个位置一个十六进制值的字符串
00F701C101C900EC01E001D2
我需要:
将这些值从 4 中的 4 个位置分开,并以这种方式转换为十进制数:
247、449、457、480、466
我的列最多可以有 1200 个十六进制位置
你能帮助我吗?
咳咳!!!
这有效:
data out;
hex = "00F701C101C900EC01E001D2";
do while(hex ne "");
valHex = substr(hex, 1, 4);
hex = substr(hex, 5);
valDec = input(valHex, hex4.);
output;
end;
run;
但是您需要为您的实际解决方案添加更多错误检查等。
对不起,我要禁食。这是 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 |
+--------------+----------------------+--------------------+--------------------+