I have tried to find information and how. But it did not contain any information that would help.
我试着去找一些信息和方法。但它没有包含任何有用的信息。
With T-SQL, I want to convert negative decimal to binary and convert it back.
使用T-SQL,我想将负小数转换为二进制并将它转换回来。
Sample value: -9223372036854775543
样本值:-9223372036854775543
I try in convert with Calculater this value to Bin result is ... 1000000000000000000000000000000000000000000000000000000100001001
我尝试用计算器把这个值转换成Bin结果是……1000000000000000000000000000000000000000000000000000000100001001
and Convert back to Dec. It's OK.
然后转换回12月就可以了。
How i can Convert like this with T-SQL(SQL2008) Script/Function ? Long time to find information for how to. Anyone who knows about this, please help.
如何用T-SQL(SQL2008)脚本/函数将其转换成这样?很长时间来寻找信息。任何知道这件事的人,请帮忙。
1 个解决方案
#1
0
There is no build in functionality. for INT and BIGINT you can use CONVERT(VARCHAR(100),CAST(3 AS VARBINARY(100)),2)
to get the hex representation as a string. then you can do a simple search replace as every hex digit represents exactly 4 binary digits. However, with values outside of the BIGINT range there is no standard as to how they are represented internally. You might get the right result or not and that behavior might even change between versions.
功能上没有构建。对于INT和BIGINT,您可以使用CONVERT(VARCHAR(100),CAST(3作为VARBINARY(100)),2)将hex表示作为字符串。然后你可以做一个简单的搜索替换,因为每个十六进制数字正好代表4个二进制数字。然而,对于超出BIGINT范围的值,内部如何表示它们没有标准。您可能会得到正确的结果,也可能会得到不同版本之间的行为。
There is also no standard as to how negative numbers are represented. Most implementations of integers use the two's-complement representation. In that representation the top most bit indicates the sign of the number. How many bits you have is a metter of convention and fully dependent on your environment.
对于负数的表示方式也没有标准。大多数整数的实现都使用二补表示。在这种表示中,最上面的位表示数字的符号。你有多少位元取决于你的环境。
In mathematics -3 woud be -11 in binary and not 11111101.
在数学中-3在二进制中应该是-11,而不是11111101。
To solve your problem you can either use a CLR function or you go through your number the old fashioned way:
要解决你的问题,你可以使用CLR函数,也可以使用老式的方法:
Is it odd? -> output a 1
Is it even? -> output a 0
integer divide by 2
repeat until the value is 0
This will give you the digits in opposite order, so you have to flip the result. To get the two's-complement representation of a negative number n calculate 1-n, convert the result to binary using the above algorithm but with reversed digits (0 instead of 1 and vice versa). After flipping the digits into the right order prepend with enough 1s to fill your "box".
这将给你的数字以相反的顺序,所以你必须翻转结果。要得到一个负数n的二补表示,计算1-n,使用上面的算法将结果转换为二进制,但使用相反的数字(0而不是1,反之亦然)。用足够的1填满你的“盒子”,把数字翻成正确的顺序。
#1
0
There is no build in functionality. for INT and BIGINT you can use CONVERT(VARCHAR(100),CAST(3 AS VARBINARY(100)),2)
to get the hex representation as a string. then you can do a simple search replace as every hex digit represents exactly 4 binary digits. However, with values outside of the BIGINT range there is no standard as to how they are represented internally. You might get the right result or not and that behavior might even change between versions.
功能上没有构建。对于INT和BIGINT,您可以使用CONVERT(VARCHAR(100),CAST(3作为VARBINARY(100)),2)将hex表示作为字符串。然后你可以做一个简单的搜索替换,因为每个十六进制数字正好代表4个二进制数字。然而,对于超出BIGINT范围的值,内部如何表示它们没有标准。您可能会得到正确的结果,也可能会得到不同版本之间的行为。
There is also no standard as to how negative numbers are represented. Most implementations of integers use the two's-complement representation. In that representation the top most bit indicates the sign of the number. How many bits you have is a metter of convention and fully dependent on your environment.
对于负数的表示方式也没有标准。大多数整数的实现都使用二补表示。在这种表示中,最上面的位表示数字的符号。你有多少位元取决于你的环境。
In mathematics -3 woud be -11 in binary and not 11111101.
在数学中-3在二进制中应该是-11,而不是11111101。
To solve your problem you can either use a CLR function or you go through your number the old fashioned way:
要解决你的问题,你可以使用CLR函数,也可以使用老式的方法:
Is it odd? -> output a 1
Is it even? -> output a 0
integer divide by 2
repeat until the value is 0
This will give you the digits in opposite order, so you have to flip the result. To get the two's-complement representation of a negative number n calculate 1-n, convert the result to binary using the above algorithm but with reversed digits (0 instead of 1 and vice versa). After flipping the digits into the right order prepend with enough 1s to fill your "box".
这将给你的数字以相反的顺序,所以你必须翻转结果。要得到一个负数n的二补表示,计算1-n,使用上面的算法将结果转换为二进制,但使用相反的数字(0而不是1,反之亦然)。用足够的1填满你的“盒子”,把数字翻成正确的顺序。