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?
 
masterpassConnect With a Mentor Commented:
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.