2

I have done the following to convert the byte array to string to store in the db

byte[] value;
String stValue = BitConverter.ToString(value);

Now I just want do do the opposite

String stValue;
byte[] value= (Convert) stValue ???

How to do this??

LynAs
  • 6,407
  • 14
  • 48
  • 83

6 Answers6

4

Since you're using the BitConverter, the string you get is specifically formatted in a less than friendly way. To reverse this process, you can write a custom method to deserialize the bytes like this:

public static byte[] GetBytes(string value)
{
    return value.Split('-').Select(s => byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToArray();
}

Or as Ben Voigt suggests:

public static byte[] GetBytes(string value)
{
    return Array.ConvertAll(value.Split('-'), s => byte.Parse(s, System.Globalization.NumberStyles.HexNumber));
}
...

var originalBytes = new byte[] { 1, 2, 3, 4, 5 };
var stValue = BitConverter.ToString(originalBytes); // "01-02-03-04-05"
var bytes = GetBytes(stValue); // [ 1, 2, 3, 4, 5 ]

However, usually don't need to use the BitConverter. Base64 is a more compact and efficient way of encoding random bytes, and you won't have to create a custom decoder:

var originalBytes = new byte[] { 1, 2, 3, 4, 5 };
var stValue = Convert.ToBase64String(originalBytes); // "AQIDBAU="
var bytes = Convert.FromBase64String(stValue); // [ 1, 2, 3, 4, 5 ]

On the other hand, the Encoding class offers similar functionality for converting between strings and bytes. Although a given encoding may not be able to translate a random sequence of bytes into a string so it's best to use this method only if you know the string is valid first:

var originalValue = "Hello World";
var bytes = Encoding.UTF8.GetBytes(originalValue); // [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 ]
var stValue = Encoding.UTF8.GetString(bytes); // "Hello World"
Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • It gives me different value – LynAs Apr 08 '13 at 07:05
  • I am getting the following error. (Argument 1: cannot convert from 'string' to 'double') while using byte[] value = BitConverter.GetBytes(stValue); – LynAs Apr 09 '13 at 19:49
  • @LynAs I apologize, I could've sworn there was an override for `BitConverter.GetBytes(string)`, I've updated my answer with a few different alternatives. – p.s.w.g Apr 09 '13 at 22:13
  • For the first case, `Array.ConvertAll` is a little nicer than LINQ (no need for an extra `ToArray()`) – Ben Voigt Jun 13 '14 at 23:34
  • @BenVoigt Good point. I've updated for completeness. – p.s.w.g Jun 16 '14 at 15:12
2

Use Encoding.UTF8.GetBytes method;

byte[] value = Encoding.UTF8.GetBytes(stValue);

Encodes all the characters in the specified string into a sequence of bytes.

bashis
  • 1,200
  • 1
  • 16
  • 35
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

You can do it like this:

byte[] value = Encoding.Default.GetBytes(stValue);
Alex
  • 8,827
  • 3
  • 42
  • 58
0

Depending on which conversion you desire, there are different ways to achieve this. There is System.Text.Encoding and in there UTF8, Unicode, and ASCII. So as you desire

byte[] strArray = System.Text.Encoding.(UTF8 | ASCII | Unicode).GetBytes(str);

See here for a reference.

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

Try this

const string input = "Dot Net Perls";

    // Invoke GetBytes method.
    // ... You can store this array as a field!
    byte[] array = Encoding.ASCII.GetBytes(input);

    // Loop through contents of the array.
    foreach (byte element in array)
    {
        Console.WriteLine("{0} = {1}", element, (char)element);
    }

OutPut

68 = D, 111 = o, 116 = t, 32 =, 78 = N, 101 = e, 116 = t, 32 =, 80 = P, 101 = e, 114 = r, 108 = l, 115 = s,

Sagar Hirapara
  • 1,677
  • 13
  • 24
0
String[] arr=str.Split('-');
byte[] array=new byte[arr.Length];
for(int i=0; i<arr.Length; i++) array[i]=Convert.ToByte(arr[i],16);

This is what i needed. Thanks @Shekhar

LynAs
  • 6,407
  • 14
  • 48
  • 83