Solved

Converting a Binary to a Varchar

Posted on 2002-04-14
14
2,193 Views
Last Modified: 2012-08-13
I am trying to convert a binary number to a varchar so I can include it into a string.  If do a straight Convert then I get blank, if I convert to an integer I get a different number.  Help tells me that converting from a binary to an integer and then converting back to a binary will not result in the same binary number that you started out with.

I need to know if anyone has been able to successfully convert a binary to a varchar and back again, and how they did it.....
0
Comment
Question by:Garfy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 4

Expert Comment

by:urim
ID: 6941277
Why not use cast (@a as int)

as in the next example:

declare @a binary(16), @b bigint

select @a = 2147483647
select @b = cast (@a as int)
select @a, @b

Regards,
Uri
0
 
LVL 1

Author Comment

by:Garfy
ID: 6943433
Read the question again, I have already tried that, and Microsoft themselves do not recommend this course of action, but they do not give a solution......
0
 
LVL 1

Author Comment

by:Garfy
ID: 6943434
Read the question again, I have already tried that, and Microsoft themselves do not recommend this course of action, but they do not give a solution......
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 1

Author Comment

by:Garfy
ID: 6943488
print @CompanyID returns 0x0000000000002E66


set @Message = 'Company No: ' + Convert(varchar, Cast (@CompanyID as bigint))
Print @Message

returns

Company No: 11878
0
 
LVL 3

Expert Comment

by:bobj_kirk
ID: 6943955
Garfy,

Well, I am not sure of your purpose, but it is an interesting challenge, so I wrote some code for you. Here it is:

declare @CompanyID binary(8)
declare @Byte int
declare @Digit int
declare @String varchar(30)
declare @I int

select @CompanyID = 0x0000000000002E66

select @I=1
select @String = 'Value is 0x'
while @I < 9
  begin
  select @Byte = substring(@CompanyID, @I, 1)
  select @Digit = floor(@Byte / 16)
  select @String = @String + substring('0123456789ABCDEF', @Digit + 1, 1)
  select @Digit = @Byte % 16
  select @String = @String + substring('0123456789ABCDEF', @Digit + 1, 1)
  select @I = @I + 1
  end

select @string as 'Converted String'

This code returns the following:
Converted String              
------------------------------
Value is 0x0000000000002E66

(1 row(s) affected)

BTW, the value 0x0000000000002E66 is a hexadecimal number equal to 11878 decimal as your query shows. You can confirm it by using windows calculator in scientific mode to convert the decimal number to hex.

Hope this helps.

-Bob

0
 
LVL 1

Author Comment

by:Garfy
ID: 6944012
Sadly, by using substring, and I was still not able to add the binary number to another string.......  it keeps saying that you cannot add a string and binary.....    I am using SQL 2000.  Microsoft appear to be aware of a problem with binaries and strings but they have not posted a resolution.  I am hoping someone here has one...  

All I want to do is run these lines

********************************************************
/*set @Message = ('Update ' + @Tname + ' set ' + @Fname + ' = ' + Convert(varchar,@CompanyID) + ' where ' + @Fname + ' = ' + CONVERT(varchar, @ContactID))

exec (@Message)
*********************************************************

CompanyID and ContactID are binary numbers.......
0
 
LVL 4

Expert Comment

by:urim
ID: 6944036
Garfy,

If this what you looking for:

declare @a binary(16)
select @a = 0x0000000000002E66
select @a, '<' + dbo.fn_sqlvarbasetostr(@a) + '>'

output:
0x0000000000002E660000000000000000     <0x0000000000002e660000000000000000>

Uri
0
 
LVL 4

Expert Comment

by:urim
ID: 6944045
or even better use dbo.fn_varbintohexstr(@a)
0
 
LVL 1

Author Comment

by:Garfy
ID: 6944081
I got so excited....  but then I got this error message

Server: Msg 208, Level 16, State 1, Procedure rc_spCompIDsameContIDnotOrphans, Line 143
Invalid object name 'dbo.fn_varbintohexstr'.
0
 
LVL 3

Expert Comment

by:bobj_kirk
ID: 6944173
Garfy,

I am only using SQL 7 and I don't know why you are having so much trouble with SQL 2000. Anyway, here's a hack which works on SQL 7 and doesn't use substring on binary data:

declare @CompanyID binary(8)
declare @Binary binary(4)
declare @Integer int
declare @Decimal decimal(20)
declare @Digit int
declare @String varchar(30)
declare @I int

select @CompanyID = 0x0000000000002E66
select @Binary = @CompanyID
select @Integer = @Binary
select @Decimal = convert(decimal, @Integer) * power(convert(decimal,2), 32)
select @Integer = @CompanyID
select @Decimal = @Decimal + convert(decimal, @Integer)
if @Integer < 0 select @Decimal = @Decimal + power(convert(decimal, 2), 32)

select @I=1
select @String = ''
while @I < 17
 begin
 select @Digit = @Decimal - floor(@Decimal / 16) * 16
 select @String = substring('0123456789ABCDEF', @Digit + 1, 1) + @String
 select @Decimal = floor(@Decimal / 16)
 select @I = @I + 1
 end
Select @String = 'Value is 0x' + @String

select @string as 'Converted String'

Converted String              
------------------------------
Value is 0x0000000000002E66

(1 row(s) affected)


It only works with data up to 8 bytes (2 integers) long, but it could be extended by adding more interim binary variables. Not elegant code, but may help.

-Bob
0
 
LVL 4

Accepted Solution

by:
urim earned 200 total points
ID: 6944179
write master.dbo.fn_varbintohexstr
0
 
LVL 1

Author Comment

by:Garfy
ID: 6946439
thank you thank you thank you.  You are a marvel.  Is there a function that does the reverse??????
0
 
LVL 1

Author Comment

by:Garfy
ID: 6946441
thank you thank you thank you.  You are a marvel.  Is there a function that does the reverse??????
0
 
LVL 6

Expert Comment

by:JohnDesautels
ID: 13872773
I hunted for a way to concatenate a binary field with text.

Master.dbo.fn_varbintohexstr worked for me!
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question