0

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.

4
Contributors
3
Replies
5
Views
9 Years
Discussion Span
Last Post by alc6379
0

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();
        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.