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

Re: C# string to byte conversion 80 80

I guess this can help.

Re: C# string to byte conversion 80 80

Something like this?

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

You also need to include this namespace: System.Text

Re: C# string to byte conversion 80 80

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

Re: C# string to byte conversion 80 80

Thanks @ddanbe and @TheApex, solved my problem.

Be a part of the DaniWeb community

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