473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

convert string to binary

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
5 12896
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
4
by: CSharpener | last post by:
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C# In JavaScript, it goes like this for an int: javascript:(123).toString(2 or...
8
by: John A Grandy | last post by:
could someone please discuss the pros and cons of CType(MyDouble,Decimal) versus Convert.ToDecimal(MyDouble) .... and other such conversions ...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
3
by: bussiere maillist | last post by:
i've got a very long string and i wanted to convert it in binary like string = """Monty Python, or The Pythons, is the collective name of the creators of Monty Python's Flying Circus, a British...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.