Convert VARBINARY data into C# String data type

Hi All,

In SQL Server I have a data that is stored using VARBINARY(200)  data type and I need to convert this within my C# to the String data type.

Here is my attempts so far:

This throws the error:

cannot implicitly conert type byte[] to byte

Please could someone show me the errors of my ways.

Kind Regards,

Rit



byte btPassord = System.Text.Encoding.Unicode.GetBytes(ireader["Password"].ToString());
string strSavedHashedPassword = System.Text.Encoding.Unicode.GetString(btPassord);

Open in new window

LVL 1
rito1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robastaCommented:
change this:
byte btPassord = System.Text.Encoding.Unicode.GetBytes(ireader["Password"].ToString());

to:

byte[] btPassord = System.Text.Encoding.Unicode.GetBytes(ireader["Password"].ToString());


0
rito1Author Commented:
Hi robasta,

Thank you for your input. I have applied that change and now the variable strSavedHashedPassword seems to contain the value 'System.Byte[]' rather than the actual string version of the byte data.

Any ideas?.. I haven't dealt with binary/byte data too much before.

Thanks,

Rit
0
masterpassCommented:
Try this

byte[] btPassord = System.Text.Encoding.Unicode.GetBytes(ireader["Password"].ToString());
string strSavedHashedPassword = System.Text.Encoding.UTF8.GetString(btPassord);

Open in new window

0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

robastaCommented:
try changing line2 to this:
string strSavedHashedPassword =  System.Text.ASCIIEncoding.ASCII.GetString(btPassord)
0
rito1Author Commented:
Hi,

Both, this still doesn't convert to the same stored value that ireader["Password"].ToString() is.

Long story short, The Password column stores the hashed version of a readable password using SHA1 and  I was advised to store this value as VARBINARY rather than NVARCHAR... is this really necessary?

If not then this would solve all my problems I think.

Thanks,

Rit
0
lazyberezovskyCommented:

string strSavedHashedPassword = Encoding.Unicode.GetString((byte[])ireader["Password"]);

Open in new window

0
masterpassCommented:
I would recommend nvarchar .... varbinary is usually used for storing the large files such as image files

How ever if you just need the casting, then you can do it in the DB itself

DECLARE @X varbinary(20)
Set @X = CAST('somevalue' As varbinary(20))
Select @X As [varbinary], CAST(@X as varchar(20)) As [varchar]

it will give you both varchar and varbinary values and you can pass it to aspx and do any thing you want ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lazyberezovskyCommented:
ireader["Password"] contains byte array of unicode string
What you are were doing wrong:
byte[] btPassord = System.Text.Encoding.Unicode.GetBytes(ireader["Password"].ToString());
string strSavedHashedPassword = System.Text.Encoding.Unicode.GetString(btPassord);
1) ireader["Password"].ToString() returns (byte[]).ToString() // System.Byte[]
2) System.Text.Encoding.Unicode.GetBytes(ireader["Password"].ToString()) converts  "System.Byte[]" to byte array
3) System.Text.Encoding.Unicode.GetString(btPassord) converts byte array back to string  "System.Byte[]"
0
rito1Author Commented:
Excellent, thanks.

Rit
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.