By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

convert string to binary

P: n/a
I am having a problem converting string to binary and I am hoping someone can
help me out

I have a sql query that does an update that updates a binary field calles
password

password=CAST(@password as binary)

However when I go to sql server and convert that binary field to varchar,
all I get is the first letter of the password that I typed in. How do I
convert a string field so that when I do that query in sql server, I get the
appropriate value?

Any help would be appreciated
Apr 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Thu, 05 Apr 2007 18:38:01 +0200, bbdobuddy <bb*******@discussions.microsoft.comwrote:
I am having a problem converting string to binary and I am hoping someone can
help me out

I have a sql query that does an update that updates a binary field calles
password

password=CAST(@password as binary)

However when I go to sql server and convert that binary field to varchar,
all I get is the first letter of the password that I typed in. How doI
convert a string field so that when I do that query in sql server, I get the
appropriate value?

Any help would be appreciated
Hi,

I suggest using SqlParameter and set the parameter type to the proper SqlDBType
You may need to convert the string to a byte[] before sending it to the sql server, in which case Encoding plays an important role.

--
Happy coding!
Morten Wennevik [C# MVP]
Apr 5 '07 #2

P: n/a
bbdobuddy,

What is the type of the @password parameter? My guess here is that it
is of type nvarchar. Because of this, the first letter is going to be
represented as two bytes, the second byte most likely being a value of 0
(since you probably are using strings that are encodable in ASCII as well).

Now, when you are casting that binary value back, you are probably
casting it to varchar, which is looking at the first byte (the letter) and
then the second byte as a string terminator (since it is 0).

Because of this, you have to be consistent with the parameters that you
are casting to binary, and how you are casting them on the way out.

I would be remiss if I didn't say that storing passwords in a database
is a BAD idea. It looks like you are trying to remedy the situation by
storing them in a binary format, but looking at the field with the naked
eye, you could figure out pretty quickly how the data is stored.

If you MUST store a password in the database, then at least encrypt the
column. Sql server has facilities to do this, and you can do it on the
client as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"bbdobuddy" <bb*******@discussions.microsoft.comwrote in message
news:8D**********************************@microsof t.com...
>I am having a problem converting string to binary and I am hoping someone
can
help me out

I have a sql query that does an update that updates a binary field calles
password

password=CAST(@password as binary)

However when I go to sql server and convert that binary field to varchar,
all I get is the first letter of the password that I typed in. How do I
convert a string field so that when I do that query in sql server, I get
the
appropriate value?

Any help would be appreciated

Apr 5 '07 #3

P: n/a
Your cast is specifying a fixed length of 1, thus the single character - you
need to specify length, and I would hazard a guess you may want it to be
variable width:

CAST(@password AS varbinary(100))

100 is just for example, you may want 12, 24, 42, 99, ...
"bbdobuddy" wrote:
I am having a problem converting string to binary and I am hoping someone can
help me out

I have a sql query that does an update that updates a binary field calles
password

password=CAST(@password as binary)

However when I go to sql server and convert that binary field to varchar,
all I get is the first letter of the password that I typed in. How do I
convert a string field so that when I do that query in sql server, I get the
appropriate value?

Any help would be appreciated
Apr 5 '07 #4

P: n/a
I found a solution

"KH" wrote:
Your cast is specifying a fixed length of 1, thus the single character - you
need to specify length, and I would hazard a guess you may want it to be
variable width:

CAST(@password AS varbinary(100))

100 is just for example, you may want 12, 24, 42, 99, ...
"bbdobuddy" wrote:
I am having a problem converting string to binary and I am hoping someone can
help me out

I have a sql query that does an update that updates a binary field calles
password

password=CAST(@password as binary)

However when I go to sql server and convert that binary field to varchar,
all I get is the first letter of the password that I typed in. How do I
convert a string field so that when I do that query in sql server, I get the
appropriate value?

Any help would be appreciated
Apr 5 '07 #5

P: n/a
KH,

That's not true. Run the following query against SQL Server, and you
will get 'hey' returned to you, which can't be the case if it was taking
only the first character:

select cast(cast('hey' as binary) as varchar)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"KH" <KH@discussions.microsoft.comwrote in message
news:5A**********************************@microsof t.com...
Your cast is specifying a fixed length of 1, thus the single character -
you
need to specify length, and I would hazard a guess you may want it to be
variable width:

CAST(@password AS varbinary(100))

100 is just for example, you may want 12, 24, 42, 99, ...
"bbdobuddy" wrote:
>I am having a problem converting string to binary and I am hoping someone
can
help me out

I have a sql query that does an update that updates a binary field calles
password

password=CAST(@password as binary)

However when I go to sql server and convert that binary field to varchar,
all I get is the first letter of the password that I typed in. How do I
convert a string field so that when I do that query in sql server, I get
the
appropriate value?

Any help would be appreciated

Apr 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.