August 15, 2007 at 2:03 am
Hi,
I would like to find out an easiest way to convert an integer (maximum 8 bits in binary) into a hexadecimal presentation using SQL Server scripts. For example,
7 (int) --> E0 (hexadecimal presentation)
15 (int) --> F0 (hexadecimal presentation)
1 (int) --> 80 (hexadecimal presentation)
66 (int)--> 42 (hexadecimal presentation)
etc.
Business rule details:
Example 1: 7 (int) = (8 bit binary presentation) --> E0 (hexadecimal presentation) because
4 left most bits of 1110: 1*(2^3) + 1*(2^2) + 1*(2^1) + 0*(2^1) = 14 (int) = E (hexadecimal)
4 right most bits of 0000: 0*(2^3) + 0*(2^2) + 0*(2^1) + 0*(2^1) = 0 (int) = 0 (hexadecimal)
Finally, 7 (int) --> E0 (hexadecimal presentation).
Example 2: 66 (int) = (8 bit binary presentation) --> 42 (hexadecimal presentation) because
4 left most bits of 0100: 0*(2^3) + 1*(2^2) + 0*(2^1) + 0*(2^1) = 4 (int) = 4 (hexadecimal)
4 right most bits of 0010: 0*(2^3) + 0*(2^2) + 1*(2^1) + 0*(2^1) = 2 (int) = 2 (hexadecimal)
Finally, 66 (int) --> 42 (hexadecimal presentation)
Note: 2^3 means 2 to the power of 3 or 8
Thanks in advance.
johnsql
August 15, 2007 at 8:22 am
john i must be reading your post all wrong... for me, if i was going to present int's as hex, I'd do the following:
select convert(varbinary,7) ,'note said --> E0 (hexadecimal presentation)'
select convert(varbinary,15) ,'note said--> F0 (hexadecimal presentation)'
select convert(varbinary,1) ,'note said--> 80 (hexadecimal presentation)'
select convert(varbinary,66) ,'note said--> 42 (hexadecimal presentation)'
that gives me results like this, which is nowhere near the same data you were expecting:
0x00000007 | note said--> E0 (hexadecimal presentation) |
0x0000000F | note said--> F0 (hexadecimal presentation) |
0x00000001 | note said--> 80 (hexadecimal presentation) |
0x00000042 | note said--> 42 (hexadecimal presentation) |
Lowell
August 15, 2007 at 8:27 am
Would you be able to use the proc that is used for transferring logins called sp_hexadecimal?
http://support.microsoft.com/kb/246133
August 15, 2007 at 1:16 pm
Lowell,
Thanks for your post. I agree with you about about the normal representation of binaries of 0 and 1. But if you double-read the business requirements, you will see the format users epect to see.
For example, if I have 7 (base 10). I can show it in binary format as 1110 0000 and (normal way in hexadecimal as 70 (7=1*(2^0) + 1*(2^1) + 1*(2^2) + 0*(2^3) for 4 left most bits 1110; and 0 for 4 left most bits 0000). But the requirement is not in normal way as you think. In contradition to that is the requirement:
1110 (base 2) = 1*(2^3) + 1*(2^2) + 1*(2^1) + 0*(2^0) = 8 + 4 + 2 (base 10) = 14 (base 10) = E (base 16). The way to convert is reverse with the normal way you think.
So, what I expect 7 (base 10) should be formated like E0 (base 16), not 7 or 70
August 15, 2007 at 2:17 pm
Not sure how you get the leftmost 4 bits in 7 to be 1110. Unless you are feeding the data from a system with opposite endian than what Wintel world is using.
August 15, 2007 at 2:35 pm
John, you're using quite freaky representation, so I doubt there is any ready made tool for this exercise.
You need to get binary string (0's and 1's), reverse it and get binary representation from reversed string.
Too lazy to do it for you , but you can do it yourself with a little help from functions dbo.fn_varbintohexstr and dbo.fn_varbintohexsubstring in master database.
_____________
Code for TallyGenerator
August 15, 2007 at 4:57 pm
August 16, 2007 at 6:41 am
Thank all of you for your posts. "8 original bits are split into 2 group of four, then the left most 4 original are put in the left most. The other right most 4 bits are put in the rightmost and the hexadecmial repsentation is such..." are project requirements and I do not know how to explain.
johnsql
August 16, 2007 at 7:12 am
So you are basically not storing it in bits.... 1110 0000 would be 128 + 64 + 32 + 0 = 224, not 7...
I understand that you are trying to split it into 2 4-bit words basically, but that leaves the conversion from base 10 to base 2 still wrong.
August 16, 2007 at 7:55 am
It appears as if you are trying to do some odd form of Big Endian storage, but either I'm confused about that, or your example is wrong. If you're taking the low order 4 bits, and swapping them with the high order 4 bits, but leaving each bit in the 4 bit sets in order, then 7 would be 01110000, not 11100000. If this is indeed a mistake on your part, then at least the logic is nailed down, and we can begin to help.
August 16, 2007 at 8:49 am
He seems to want to reverse the bits in a tinyint and then show the number in Hex.
Maybe:
-- From http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#ReverseByteWith64BitsDiv
DECLARE @big1 bigint
,@big2 bigint
SELECT @big1 = 8623620610
,@big2 = 1136090292240
-- result may need converting to a string
SELECT CAST((N * @big1 & @big2 ) % 1023 AS binary(1))
FROM (
SELECT CAST(7 AS tinyint) UNION ALL
SELECT 15 UNION ALL
SELECT 1 UNION ALL
SELECT 66
) N (N)
August 16, 2007 at 10:18 am
Now that I look at it closer, I'm pretty sure you're right. He is wanting to completely reverse the binary digits and then process it, not do the Big Endian as I had guessed.
As an aside, I have to say that your link is one of the cooler finds I've ever seen here. Shortcut on the desktop!
August 16, 2007 at 10:52 am
Use the 64-bit reverse thingy above and then the function I posted in the link earlier.
N 56°04'39.16"
E 12°55'05.25"
August 17, 2007 at 3:17 am
It strikes me that we may have been seduced by our inner low level programmer. Maybe all that is required is a 256 row lookup table which could be generated with Peter’s function.
August 17, 2007 at 3:22 am
Nah, too easy
N 56°04'39.16"
E 12°55'05.25"
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply