0

I have an web application which reads varbinary from SQL as a byte array in code. Basically it does a ExecuteScalar of the column and returns it as a byte array.

I am now doing a mini version of the application as a Windows application. For this, I am trying to skip the DB connection. I am directly inputting the varbinary value from SQL to a RichTextBox and trying to read it as a byte array. However, I do not get the same byte array. What am I doing wrong and how should I proceed ?

6
  • 1
    What encoding was used to create the binary? Commented Mar 9, 2015 at 9:40
  • How exactly are you entering binary data into a richtextbox? Commented Mar 9, 2015 at 9:45
  • @Izzy I get the details as an arraylist and Serialize it using BinaryFormatter class. Not sure if I answered your question though. Let me know if you need more info. Commented Mar 9, 2015 at 11:10
  • @RhysJones I am just copying the varbinary value from DB and pasting it into a textbox string. Commented Mar 9, 2015 at 11:11
  • possible duplicate of SQL Server converting varbinary to string Commented Mar 9, 2015 at 11:22

1 Answer 1

0

I got it using this piece of code

    string hex = richTextBox1.Text
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
    bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2).ToString(), 16);
    return bytes;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.