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)
        byte[] data= byte.Parse(msg.ToString());
    catch(IOException e)
        throw e;

how can I solve this problem?

Recommended Answers

All 4 Replies

I guess this can help.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.