Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting string to hex/binary

Status
Not open for further replies.

AnStr

Technical User
Feb 25, 2002
3
DE
Hello,

I have a 8-digit hex number given as a character column
in my database running under SQL Server 7.0 (not 2000).
I want to convert it into a binary variable in order
to increment it.
I did not succeed with the following statements:
Code:
 -- Test binary conversion, 
 -- Vers. 2.x for T-SQL in Query Analyzer --  March 18, 2002
 USE MMGUeb
 go
 SET NOCOUNT ON 
 declare @strVar1 as varchar(20)
 declare @binVar1 as binary(4)
 declare @binVar2 as binary(4)
 declare @IntVar1 as integer
 declare @IntVar2 as integer
 -- Example 1
 SET @binVar1 = 0xABCD1234
 PRINT "Example 1: @binVar1 loaded with hex number in code"
 PRINT "SET @binVar1 = 0xabcde123  ----- PRINT @binVar1"
 PRINT @binVar1
 PRINT "This is not what I wanted to do - "
 PRINT " - it simply demonstrates hex numbers"
 PRINT "--------------------------------------"
 -- Example 2
 SET @strVar1 = 'ABCD1234'
 SET @binVar1 = cast(@strVar1 as binary)
 PRINT "Example 2: Converts the ASCII-Values to hex "
 PRINT ".......... not the represented 8-digit hex number"
 PRINT "SET @strVar1 = 'ABCD1234'  ---  SET @binVar1 = cast(@strVar1 as binary))  --- PRINT @binVar1"
 PRINT @binVar1
 PRINT "... not the desired result .... "
 PRINT "... I wanted to see '0xABCDE1234'"
 PRINT "--------------------------------------"
 -- Example 3
 SET @strVar1 = '0xABCD1234'
 SET @binVar1 = cast(@strVar1 as binary)
 PRINT "Example 3: Converts the ASCII-Values to hex ... "
 PRINT ".........  not the represented 8-digit hex number"
 PRINT "SET @strVar1 = '0xABCD1234'  ---  SET @binVar1 = cast(@strVar1 as binary))  --- PRINT @binVar1"
 PRINT @binVar1
 PRINT "... not the desired result .... "
 PRINT "... I wanted to see '0xABCDE1234'"
 PRINT "--------------------------------------"
 -- Example 4
 SET @IntVar1 =  4*power(16,0) +  3* power(16,1)+  2* power(16,2)+  1* power(16,3)  -- 0xABCD1234
 SET @IntVar2 = 13*power(16,0) + 12* power(16,1)+ 11* power(16,2)+ 10* power(16,3)  -- 0xABCD1234
 SET @binVar1 = cast(@IntVar1 as binary(4))
 SET @binVar2 = cast(@IntVar2 as binary(4))
 SET @binVar2 =  @binVar2 * 256 
 PRINT "Example 4: Converts integer to hex ... not string to hex"
 PRINT "SET @IntVar1 =  4*power(16,0) +.... ---  @binVar1 = cast(@IntVar1 as binary) --- PRINT @binVar1"
 PRINT @binVar1
 PRINT @binVar2
 PRINT "... building hex numbers this way is very complicated"
 PRINT "... no binary arithmetic exceeding 7 hex digits ... "
 PRINT "... no 8-digit hex numbers"
 PRINT "... BIGINT datatype is not available in SQL Server 7.0"
 PRINT "-------------------------------------------------------"
 go

============== the results are: =========================

 Example 1: @binVar1 loaded with hex number in code
 SET @binVar1 = 0xabcde123  ----- PRINT @binVar1
 0xABCD1234
 This is not what I wanted to do - 
  - it simply demonstrates hex numbers
 --------------------------------------
 Example 2: Converts the ASCII-Values to hex 
 .......... not the represented 8-digit hex number
 SET @strVar1 = 'ABCD1234'  ---  SET @binVar1 = cast(@strVar1 as binary))  --- PRINT @binVar1
 0x41424344
 ... not the desired result .... 
 ... I wanted to see '0xABCDE1234'
 --------------------------------------
 Example 3: Converts the ASCII-Values to hex ... 
 .........  not the represented 8-digit hex number
 SET @strVar1 = '0xABCD1234'  ---  SET @binVar1 = cast(@strVar1 as binary))  --- PRINT @binVar1
 0x30784142
 ... not the desired result .... 
 ... I wanted to see '0xABCDE1234'
 --------------------------------------
 Example 4: Converts integer to hex ... not string to hex
 SET @IntVar1 =  4*power(16,0) +.... ---  @binVar1 = cast(@IntVar1 as binary) --- PRINT @binVar1
 0x00001234
 0x00ABCD00
 ... building hex numbers this way is very complicated
 ... no binary arithmetic exceeding 7 hex digits ... 
 ... no 8-digit hex numbers
 ... BIGINT datatype is not available in SQL Server 7.0
 -------------------------------------------------------

Does anyone have a hint for a better solution ?

AnStr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top