none
How do I according to the length of the class, to specify the length of the byte?

    Question

  • public class tt { public char[] MsgCod; public char[] Sender; public char[] Recver; public tt() { MsgCod = new char[6]; Sender = new char[6]; Recver = new char[6]; } }

        MySeis Station = new MySeis();
                Station.TypeNum = CmbVer.Text.PadRight(16, '\0');
                Station.Supplier = txtSup.Text.PadRight(32, ' ');
                Station.Serial = txtNum.Text.PadRight(16, ' ');
                Station.Type=cmbRecord.Text.PadRight(4,' ');
                Station.Standard=cmbStan.Text.PadRight(12,' ');
                Station.Wid=txtWid.Text.PadRight(16,' ');
                Station.Buried=Convert.ToSingle(txtBurial.Text);
                Station.Batholith = txtBatho.Text.PadRight(32, ' ');
                Station.Status = Status.PadRight(4, ' ');
                Station.ResFile = ResFile.PadRight(32, ' ');
                Station.Branch = Convert.ToSingle(txtBran.Text);
                Station.DAS = Das;
                string[] cb=new string[3];
                cb[0]=cmbBran1.Text.PadRight(8,' ');
                 cb[1]=cmbBran2.Text.PadRight(8,' ');
                 cb[2]=cmbBran3.Text.PadRight(8,' ');
                 Station.Code = cb;
                //Station.Status=tx
                //Station.Channels = Convert.ToInt16(CboAD.Text);
                //Station.Lng = float.Parse(txtLng.Text);
                //Station.Lat = float.Parse(txtLat.Text);
                //Station.Alti = float.Parse(txtSenior.Text);
                //Station.StaData = FirDate.PadRight(32, ' ');
                //Station.Sym = txtIden.Text.PadRight(4, ' ');
                //Station.Soft = Version.PadRight(32, ' ');
    
                 byte[] TypeNum = System.Text.Encoding.Default.GetBytes(Station.TypeNum);
                 byte[] Supplier = System.Text.Encoding.Default.GetBytes(Station.Supplier);
                 byte[] Serial = System.Text.Encoding.Default.GetBytes(Station.Serial);
                 byte[] Type = System.Text.Encoding.Default.GetBytes(Station.Type);
    
    
                 byte[] Standard = System.Text.Encoding.Default.GetBytes(Station.Standard);
                 byte[] Wid = System.Text.Encoding.Default.GetBytes(Station.Wid);
                 byte[] Buried = BitConverter.GetBytes(Station.Buried);
                 byte[] Batholith = System.Text.Encoding.Default.GetBytes(Station.Batholith);
                 byte[] Status1 = System.Text.Encoding.Default.GetBytes(Station.Status);
                 byte[] ResFile1 = System.Text.Encoding.Default.GetBytes(Station.ResFile);
                 byte[] Branch = BitConverter.GetBytes(Station.Branch);
                 byte[] DAS1 = BitConverter.GetBytes(Station.DAS[0]);
                 byte[] DAS2 = BitConverter.GetBytes(Station.DAS[1]);
                 byte[] DAS3 = BitConverter.GetBytes(Station.DAS[2]);
    
                 byte[] Code1 = System.Text.Encoding.Default.GetBytes(Station.Code[0]);
                 byte[] Code2 = System.Text.Encoding.Default.GetBytes(Station.Code[1]);
                 byte[] Code3 = System.Text.Encoding.Default.GetBytes(Station.Code[2]);

    Put the class into a byte array, the length of the byte array is 18, how to do?

    please verify my account


    • Edited by lctk Wednesday, April 19, 2017 11:35 PM
    Wednesday, April 19, 2017 11:31 AM

All replies

  • It seems that you think that there is a relation between a char and a byte. 

    That is not in .Net. A char is in unicode and consist of more bytes depending from the UTF which is in use.

    https://en.wikipedia.org/wiki/Unicode

    And therefore this way of conversion is from around 1960 when there was a papertape with 8 holes.

    https://en.wikipedia.org/wiki/Punched_tape


    Success
    Cor

    Wednesday, April 19, 2017 11:38 AM
  • You can convert to byte arrays like this

    tt myData = new tt()
    
    // ...populate char arrays
    
    byte[] b1 = Encoding.ASCII.GetBytes(myData.MsgCod);
    byte[] b2 = Encoding.ASCII.GetBytes(myData.Sender);
    byte[] b3 = Encoding.ASCII.GetBytes(myData.Recver);
    

    Then combine the arrays into a single byte array with:

    byte[] combined = b1.Concat(b2).Concat(b3).ToArray();

    However, this will ONLY work properly if the chars in your arrays are 'ascii' characters, that is, have a code value between 0 and 255 (generally English characters and basic punctuation with a few European accented characters though these vary based on the code page of the PC).

    If there is any chance that your char arrays will contain other characters such as mathematical symbols, Chinese characters or anything other than English characters or basic punctuation then you will just end up with garbage.

    • Edited by RJP1973 Wednesday, April 19, 2017 12:07 PM
    Wednesday, April 19, 2017 12:04 PM
  • As the others have stated a C++ char is not a C# char. C++ chars are 1 byte and C# chars are 2. This looks like a message you're sending across the network so you should probably be using byte[] instead of char[]. On the receiving side you can use Encoding to convert from the byte array back to a string if you need it.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Wednesday, April 19, 2017 2:29 PM
    Moderator
  •    

    byte[] TypeNum = System.Text.Encoding.Default.GetBytes(Station.TypeNum); byte[] Supplier = System.Text.Encoding.Default.GetBytes(Station.Supplier); byte[] Serial = System.Text.Encoding.Default.GetBytes(Station.Serial); byte[] Type = System.Text.Encoding.Default.GetBytes(Station.Type); byte[] Standard = System.Text.Encoding.Default.GetBytes(Station.Standard); byte[] Wid = System.Text.Encoding.Default.GetBytes(Station.Wid); byte[] Buried = BitConverter.GetBytes(Station.Buried); byte[] Batholith = System.Text.Encoding.Default.GetBytes(Station.Batholith); byte[] Status1 = System.Text.Encoding.Default.GetBytes(Station.Status); byte[] ResFile1 = System.Text.Encoding.Default.GetBytes(Station.ResFile);

    I write so, according to each string into an array of bytes, then merged into one big an array of bytes, sent to the server side, the c + + but is said to be more troublesome also error-prone, so is there a way? According to the size of the class attribute, directly generated and merged into the specified byte array length

    please verify my account

    Wednesday, April 19, 2017 11:33 PM
  • I write so, according to each string into an array of bytes, then merged into one big an array of bytes, sent to the server side, the c + + but is said to be more troublesome also error-prone, so is there a way? According to the size of the class attribute, directly generated and merged into the specified byte array length

    please verify my account

    Wednesday, April 19, 2017 11:34 PM
  • I write so, according to each string into an array of bytes, then merged into one big an array of bytes, sent to the server side, the c + + but is said to be more troublesome also error-prone, so is there a way? According to the size of the class attribute, directly generated and merged into the specified byte array length

    please verify my account

    Wednesday, April 19, 2017 11:34 PM
  • If you have strings on the client and need to send them to the server then converting to a byte array is fine. However you cannot simply combine the byte arrays together as you have no way of knowing when one string ends and other begins. You need to send more information.

    "According to the size of the class attribute"

    There is no class attribute or size. You can get the size of a type but it isn't useful for serialization. You also don't want to simply merge the byte arrays because, again, you have no way of knowing when one ends and another starts unless you also include the length.

    Personally I would serialize the Station type as a string structure. .NET can do this easily. I would look into binary formatter if you're targeting binary. If you're going from C++ to C# (or vice versa) then you'd need to target ANSI (probably) but it should mostly just work.

    Thursday, April 20, 2017 2:47 AM
    Moderator
  • Maybe adopt the next technique: for each string, send four bytes containing the length, then the Unicode bytes:

       byte [] bytes = Encoding.Unicode.GetBytes( Station.Supplier );

       byte [] prefix = BitConverter.GetBytes( bytes.Length );

       Send( prefix );

       Send( bytes );

    The C++ receiver will read four bytes to determine the size, then will allocate and read the bytes. The received data can be treated as an array of WCHAR or wchar_t. This will be repeated for all members. The receiver knows how many strings to read.


    • Edited by Viorel_MVP Thursday, April 20, 2017 5:04 AM
    Thursday, April 20, 2017 5:03 AM