Hi,

I have a legacy application in vc++ 6.0 which communicates to a server. The server follows c++ data types. I want to convert my legacy application to c#. Now I am facing some conversion issues. The server accepts data in binary format. But when I declare a char data type in C# it takes 2 bytes, whereas char data type in c++ takes 1 byte. How do I convert the char in c# so that it is 1 byte.

C# has byte and sbyte types depending on if you care about the sign or not.

Char is 2 byte in .NET because that is how big a Unicode character is. The best thing to do is take the value in as a byte datatype and use the Convert.ToChar() method on that input value.

I just tried it with this code, and you'll see it produces an "H":

using System;

namespace Unicode_test
{
    class Program
    {
        static void Main(string[] args)
        {
            byte myByte = 0x48; // 'H' in ASCII, single byte
            char myChar = Convert.ToChar(myByte); // should still be 'H"
            Console.WriteLine(myChar);
            Console.ReadLine();
        }
    }
}
Be a part of the DaniWeb community

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