我正在从 SQL Server 数据库中检索已签名的 int,并且需要将其转换为“正常”外观的虚线字符串以显示给用户。
谷歌搜索,我发现了这段代码:
SELECT
dbo.IPADDRESS.IPADDRESS,
CAST(ROUND( (cast(dbo.IPADDRESS.IPADDRESS as bigint) / 16777216 ), 0, 1) AS varchar(4)) + '.' +
CAST((ROUND( (cast(dbo.IPADDRESS.IPADDRESS as bigint) / 65536 ), 0, 1) % 256) AS varchar(4)) + '.' +
CAST((ROUND( (cast(dbo.IPADDRESS.IPADDRESS as bigint) / 256 ), 0, 1) % 256) AS varchar(4)) + '.' +
CAST((cast(dbo.IPADDRESS.IPADDRESS as bigint) % 256 ) AS varchar(4)) as IPDottedNotation
FROM
dbo.IPADDRESS
这在某些时候有效,但在其他时候会产生古怪的输出。例如,转换 this-1951276725
会产生 result -116.-78.-30.-181
。
有什么建议么?谢谢。