2

I wrote a function to convert byte[] to string, and I add ";" after each byte. Now I want to convert this string to byte[] by splitting the string (similar to a CSV string).

public string ByteArrayToString(byte[] byteArray,string s)
{       
    for (int i = 0; i < byteArray.Length; i++)
    {
        s += byteArray[i].ToString() + ";";
    }
    s = s.Substring(0, s.Length - 1);
    return s;
}

How could I write a function to convert this string to that byte array again?

2
  • Is the string like T;*;|;m; or 84;42;124;109;? Commented Nov 30, 2010 at 12:06
  • "thanks BUT it couldn't help me! i want to split my string by ";" then put it in the array then convert it to byte [] " try to write some pseudo code for this so we can see what you want. My understading of that qoute is you want to cast a string[] to a byte[] while interpreting the string values as a byte each. Which is not possible. You need to convert each value Commented Nov 30, 2010 at 12:29

9 Answers 9

7

try this

var byteArray = new byte[] {123, 11, 111};
var stringBytes = string.Join(";", byteArray.Select(b => b.ToString()));
var newByteArray = stringBytes.Split(';').Select(s => byte.Parse(s)).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

3

I guess that you want to get rid of the ; when converting also. I think you want to do something like this:

byte[] result = Encoding.UTF8.GetBytes(s.Replace(";",""));

This will fail if the original byte array actually contains a ;that is valid data, but in that case you will have lots of problems anyway since your "CSV" file will be wrongly formatted.

Comments

1

Consider using Split String

1 Comment

thanks BUT it couldn't help me! i want to split my string by ";" then put it in the array then convert it to byte []
1

StringBuilder will be useful instead of String (Performance wise).

With StringBuilder:

byte[] buffer = System.Text.Encoding.UTF8.GetBytes(objStringBuilder.ToString());

with String:

byte[] buffer = System.Text.Encoding.UTF8.GetBytes(objString);

Comments

0
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(yourStringVariable);

1 Comment

the values are "printed" some 32 is "32" not " " as your code expects
0

I know you already know the answer by now... but this code solves the problem, i hope it helps someone else.

        int counter= 0;
        string cadena = "8,5,6,3,4,6,3"
        string[] foto = cadena.Split(',');
        byte[] fotoFinal = new byte[foto.Length];
        foreach (string s in foto)
        {
            fotoFinal[contador] = Convert.ToByte(s);
            counter++;
        }

Comments

0
str.Split(new char[]{';'}, 
          StringSplitOptions.RemoveEmptyEntries).Select(s => byte.Parse(s)).ToArray();

3 Comments

thanks BUT it couldn't help me! i want to split my string by ";" then put it in the array then convert it to byte []
Try this: Encoding.UTF8.GetBytes(str.Replace(";", string.Empty);
wont work byteArray[i].ToString() + ";" does not concatenate the byte values as char but uses 1-3 char pr. byte
0

Simply :)

public static byte[] Bytes ( this string Key )
{
    return Enumerable.Range(0, Key.Binary().Length / 8 )
                     .Select(Index => Convert.ToByte(
                         Key.Binary().Substring(Index * 8, 8), 2))
                     .ToArray();
}

Comments

0
string[] sbytes   = sl.Split(',');
                    byte[] b          = new byte[sbytes.Length];
                    for (int j = 0; j < sbytes.Length; j++)
                    {
                        byte newByte  = byte.Parse(sbytes[j], System.Globalization.NumberStyles.HexNumber);
                        b[j]          = newByte;
                    }

I like using number styles hex number.

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.