I'm currently working in a java to c# conversion project. In one part I got this Java code snippet.

public CustomType someMethod(Message msg) throws IOException 
{
    byte[] data = msg.toString().getBytes();
}

to my understanding, data contains the byte representation of the string msg.toString()

But when I try to write it in C# like following format, i get errors.

public CustomType someMethod(Message msg)
{
    try
    {
        byte[] data= byte.Parse(msg.ToString());
    }
    catch(IOException e)
    {
        throw e;
    }
}

how can I solve this problem?

Recommended Answers

I guess this can help.

Jump to Post

All 4 Replies

Something like this?

byte[] data = Encoding.Default.GetBytes(msg.ToString());

You also need to include this namespace: System.Text

Make sure you get the encoding right otherwise you might end up with garbage.

Thanks @ddanbe and @TheApex, solved my problem.