Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?

Basically, I have in hand the varbinary value (eg: "B0015379737"). This is a varbinary value i have taken from SQL. Now, I want to simply paste this value in a textbox and read it as byte array. Basically the source is varbinary. I am giving input as a string and I want the output as byte array which matches the source varbinary
Posted
Updated 9-Mar-15 0:08am
v2

Hi,

Depending on which encoding was used, use the following code:
C#
byte[] binaryString = (byte[])reader[1];

// if the original encoding was ASCII
string ascii = Encoding.ASCII.GetString(binaryString);

// if the original encoding was UTF-8
string utf = Encoding.UTF8.GetString(binaryString);

// if the original encoding was UTF-16
string utfs = Encoding.Unicode.GetString(binaryString);
 
Share this answer
 
Comments
Divakar Raj M 9-Mar-15 6:04am    
See, I have in hand the varbinary value (eg: "B0015379737"). This is a varbinary value i have taken from SQL. Now, I want to simply paste this value in a textbox and read it as byte array. Basically the source is varbinary. I am giving input as a string and I want the output as byte array which matches the source varbinary
phil.o 9-Mar-15 9:12am    
B0015379737 is not a binary value. It the the hexadecimal representation (so, a string) of binary value 0000000000000000000010110000000000010101001101111001011100110111.
Divakar Raj M 9-Mar-15 9:13am    
It's a value of datatype varbinary in SQL
phil.o 9-Mar-15 9:47am    
No, it is its string hexadecimal representation.
If you start from the hexadecimal representation of the value, and you want to work with it, you have to do it in several steps:

- First, translate this hexadecimal string representation of a byte array into a byte array

- Second, pass this byte array to the GetString() method of the chosen Encoding.

Something like:
C#
using System.Globalization;
using System.Text;

public static byte[] ByteArrayFromHexaString(string hexa) {
   int length = hexa.Length;
   List<byte> result = new List<byte>();
   // Fetch whether there are an odd or even number of chars
   bool isOdd = ((length & 1) == 1);
   if (isOdd) {
      result.Add(byte.Parse(hexa[0], NumberStyles.HexNumber));
   }
   string s;
   for (int i = (isOdd) ? 1 : 0; i < length; i += 2) {
      s = hexa.SubString(i, 2);
      result.Add(byte.Parse(s, NumberStyles.HexNumber));
   }
   return result.ToArray();
}

public static string StringFromByteArray(byte[] bytes, Encoding encoding) {
   return encoding.GetString(bytes);
}


You now have both methods you need to get a string from the hexadecimal representation of its encoded raw value.

Good luck. Hope this helps.
 
Share this answer
 
Comments
Divakar Raj M 10-Mar-15 9:44am    
Thanks. You got me thinking in the right path. Though this code didn't work for me, I did some googling and was able to figure out a piece of code which works to my requirement.
Member 10377494 24-Nov-17 1:04am    
what is this NumberStyles
phil.o 24-Nov-17 2:06am    
I got it using this piece of code

C#
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;
 
Share this answer
 
Comments
phil.o 10-Mar-15 9:53am    
hex.Substring(i, 2).ToString() is ugly :)
SubString method already returns a string. There is no need to transform a string to a string.
Divakar Raj M 11-Mar-15 7:14am    
Yup. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900