-2
string[] s1 = { "5", "168", "789", "28", "29", "155" };
string s2[] = {"abc","bcs"};

private byte[] byteBuffer;

public ClientState(Socket clntSock)
{
    this.clntSock = clntSock;
    rcvBuffer = new byte[BUFSIZE]; // Receive buffer

    byteBuffer = new byte[strings.Length];

    for (int i = 0; i < s1.Length; i++)
    {
       byteBuffer[i] = Byte.Parse(strings[i]);
    }

}

string[] strings = { "5", "168", "23", "28", "29", "155" };

private const int BUFSIZE = 32; // Size of receive buffer
private byte[] rcvBuffer;
private Socket clntSock;
private byte[][] byteBuffer;
private NetworkStream netStream;

public ClientState(Socket clntSock)
{
    this.clntSock = clntSock;
    rcvBuffer = new byte[BUFSIZE]; // Receive buffer

    byteBuffer = new byte[strings.Length][];
    byteBuffer = ToBytes(strings);

}
static byte[][] ToBytes(string[] ascii)
{
    byte[][] results = ascii.AsEnumerable().Select(x => Encoding.UTF8.GetBytes(x)).ToArray();
    return results;
}
public ClientState(NetworkStream netStream, byte[][] byteBuffer)
{
    this.netStream = netStream;
    this.byteBuffer = byteBuffer;

}
public NetworkStream NetStream
{
    get
    {
        return netStream;
    }
}

public byte[][] BByteBuffer
{
    set
    {
        byteBuffer = value;
    }
    get
    {
        return byteBuffer;
    }
}  

string[] strings = { "5", "168", "23", "28", "29", "155" };

private const int BUFSIZE = 32; // Size of receive buffer
private byte[] rcvBuffer;
private Socket clntSock;
private byte[][] byteBuffer;
private NetworkStream netStream;

public ClientState(Socket clntSock)
{
    this.clntSock = clntSock;
    rcvBuffer = new byte[BUFSIZE]; // Receive buffer

    byteBuffer = new byte[strings.Length][];
    byteBuffer = ToBytes(strings);
}

static byte[][] ToBytes(string[] ascii)
{
    byte[][] results = ascii.AsEnumerable().Select(x => Encoding.UTF8.GetBytes(x)).ToArray();
    return results;
}

public ClientState(NetworkStream netStream, byte[][] byteBuffer)
{
    this.netStream = netStream;
    this.byteBuffer = byteBuffer;
}
public NetworkStream NetStream
{
    get
    {
        return netStream;
    }
}

public byte[][] BByteBuffer
{
    set
    {
        byteBuffer = value;
    }
    get
    {
        return byteBuffer;
    }
}


public  void ReceiveCallback(IAsyncResult asyncResult)
{
    ClientState cs = (ClientState)asyncResult.AsyncState;

    try
    {
            int recvMsgSize = cs.ClntSock.EndReceive(asyncResult);

        if (recvMsgSize > 0)
        {

            Console.WriteLine("Thread {0} ({1}) - ReceiveCallback(): received {2} bytes",
                              Thread.CurrentThread.GetHashCode(),
                              Thread.CurrentThread.ThreadState,
                              recvMsgSize);

            cs.ClntSock.BeginSend(cs.BByteBuffer, 0, cs.BByteBuffer.Length, SocketFlags.None,
                                  new AsyncCallback(SendCallback), cs);
        }
        else
        {
            cs.ClntSock.Close();
        }
    }
    catch (SocketException se)
    {
        Console.WriteLine(se.ErrorCode + ": " + se.Message);
        cs.ClntSock.Close();
    }
}

I am converting s1 string array to byte array and I send it to a client . This is a server. So at the client I want to convert the byte array back to a string array. With Byte.Parse I am unable to convert string s2. So how do I convert s1 string array and s2 string array to byte arrays.

I want to convert the byte arrays back to strings arrays and out put the result at the client side.

it says cannot convert from byte[][] to byte[] at cs.ClntSock.BeginSend(cs.BByteBuffer, 0, cs.BByteBuffer.Length, SocketFlags.None,new AsyncCallback(SendCallback), cs);

John Saunders
  • 160,644
  • 26
  • 247
  • 397
vaibhav
  • 41
  • 9
  • 1
    Check [How can I convert String to Int?](http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) and [Convert int to string?](http://stackoverflow.com/questions/3081916/convert-int-to-string) and [Convert string array to int array in one line of code using LINQ](http://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-line-of-code-using-linq) – Blas Soriano Jun 08 '15 at 23:20

2 Answers2

2

Well what bytes you want depends on your encoding. But it would be something like this:

string myString = "hello";
byte[] bytes = Encoding.UTF8.GetBytes(myString);
string fromBytes = Encoding.UTF8.GetString(bytes);
Console.Write(myString == fromBytes);
Zer0
  • 7,191
  • 1
  • 20
  • 34
  • Its an array of string not a single string – vaibhav Jun 08 '15 at 23:32
  • I want to convert an array of strings to an array of bytes. and convert them back to strings and display them – vaibhav Jun 08 '15 at 23:34
  • @vaibhav You can add any additional explanation by editing your question, so it would be easier to understand for anybody. Check [Converting a string to byte-array without using an encoding (byte-by-byte)](http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte?rq=1) – Blas Soriano Jun 08 '15 at 23:34
  • Its not just a string to byte array. It string array to byte array string[] to byte[] – vaibhav Jun 08 '15 at 23:48
  • Check [Convert String array to byte array](http://stackoverflow.com/questions/10531148/convert-string-to-byte-array?rq=1) – Blas Soriano Jun 08 '15 at 23:53
  • cs.ClntSock.BeginSend(cs.BByteBuffer, 0, cs.BByteBuffer.Length, SocketFlags.None,new AsyncCallback(SendCallback), cs); – vaibhav Jun 09 '15 at 00:06
  • the above line says cannot convert from byte[][] to byte[] – vaibhav Jun 09 '15 at 00:07
  • 1
    @vaibhav, the question title says string[] to byte[] and back to string[], which is what Zer0 has given you. If you have more than one (e.g. an array of strings), you'll have to do this more than once. You can post another question about ways to do things more than once. – Erik Eidt Jun 09 '15 at 00:48
0

Try this

       static void Main(string[] args)
        {
            string[] s1 = { "5", "168", "789", "28", "29", "155" };
            byte[][] s1_byte = ToBytes(s1);
            string[] s1_out = ToString(s1_byte);
            DisplayBytes(s1_byte);
            string[] s2 = { "sdf", "dsfds", "233" };
            byte[][] s2_byte = ToBytes(s2);
            DisplayBytes(s2_byte);
            string[] s2_out = ToString(s2_byte);
        }
        static byte[][] ToBytes(string[] ascii)
        {
            byte[][] results = ascii.AsEnumerable().Select(x => Encoding.UTF8.GetBytes(x)).ToArray();
            return results;
        }
        static void DisplayBytes(byte[][] bytes)
        {
            for (int i = 0; i < bytes.LongLength; i++)
            {
                Console.WriteLine(string.Join(" ", new List<byte>(bytes[i]).Select(x => "0x" + x.ToString("x2")).ToArray()));
            }
        }
        static string[] ToString(byte[][] bytes)
        {
            List<string> results = new List<string>();
            for (int i = 0; i < bytes.LongLength; i++)
            {
                string newString = Encoding.UTF8.GetString(bytes[i]);
                results.Add(newString);
            }
            return results.ToArray();

        }
jdweng
  • 33,250
  • 2
  • 15
  • 20