byte problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rain

    byte problem

    hi,

    im a newbie here and in desperate need of help. Some of you might think this
    is a stupid question or a too easy to answer question but here goes. I am
    making a UDP client server application using C# everything is all set except
    for one thing. when sending a message, the message should contain 2 byte for
    squence number another 2 bytes for lenght and the rest is for data or the
    message. my problem is i dont know how to code the needed 2 bytes.. i mean
    how do i code 2 bytes or can anyone help me with the concept of using this
    bytes and how do i declare 2 bytes and use it? i really cant understand...
    thank you so much! i would really appreciate any help at all...
  • Adityanand Pasumarthi

    #2
    RE: byte problem

    Hi,

    The code below shows how to convert a short value to array of 2 bytes. The
    same class "BitConvert er" can be used to convert any numeric value to byte
    array and vice-versa.

    short sequenceNumber = 5;
    short msgLength = 12000;

    byte[] sequenceNumberB ytes = BitConverter.Ge tBytes(sequence Number);
    // The length of the byte array will be 2
    Console.WriteLi ne(sequenceNumb erBytes.Length) ;

    byte[] msgLengthBytes = BitConverter.Ge tBytes(msgLengt h);
    // The length of the byte array will be 2
    Console.WriteLi ne(msgLengthByt es.Length); // This will print 2

    Hope this helps.

    --
    Regards,
    Aditya.P


    "Rain" wrote:
    hi,
    >
    im a newbie here and in desperate need of help. Some of you might think this
    is a stupid question or a too easy to answer question but here goes. I am
    making a UDP client server application using C# everything is all set except
    for one thing. when sending a message, the message should contain 2 byte for
    squence number another 2 bytes for lenght and the rest is for data or the
    message. my problem is i dont know how to code the needed 2 bytes.. i mean
    how do i code 2 bytes or can anyone help me with the concept of using this
    bytes and how do i declare 2 bytes and use it? i really cant understand...
    thank you so much! i would really appreciate any help at all...

    Comment

    • Rain

      #3
      RE: byte problem

      Thanks Adityanand.. It was very helpful. Thank you so much for the help!!!!

      "Adityanand Pasumarthi" wrote:
      Hi,
      >
      The code below shows how to convert a short value to array of 2 bytes. The
      same class "BitConvert er" can be used to convert any numeric value to byte
      array and vice-versa.
      >
      short sequenceNumber = 5;
      short msgLength = 12000;
      >
      byte[] sequenceNumberB ytes = BitConverter.Ge tBytes(sequence Number);
      // The length of the byte array will be 2
      Console.WriteLi ne(sequenceNumb erBytes.Length) ;
      >
      byte[] msgLengthBytes = BitConverter.Ge tBytes(msgLengt h);
      // The length of the byte array will be 2
      Console.WriteLi ne(msgLengthByt es.Length); // This will print 2
      >
      Hope this helps.
      >
      --
      Regards,
      Aditya.P
      >
      >
      "Rain" wrote:
      >
      hi,

      im a newbie here and in desperate need of help. Some of you might think this
      is a stupid question or a too easy to answer question but here goes. I am
      making a UDP client server application using C# everything is all set except
      for one thing. when sending a message, the message should contain 2 byte for
      squence number another 2 bytes for lenght and the rest is for data or the
      message. my problem is i dont know how to code the needed 2 bytes.. i mean
      how do i code 2 bytes or can anyone help me with the concept of using this
      bytes and how do i declare 2 bytes and use it? i really cant understand...
      thank you so much! i would really appreciate any help at all...

      Comment

      • Rain

        #4
        RE: byte problem

        if i have a string, how do i get the first 2 bytes of it? thanks

        "Adityanand Pasumarthi" wrote:
        Hi,
        >
        The code below shows how to convert a short value to array of 2 bytes. The
        same class "BitConvert er" can be used to convert any numeric value to byte
        array and vice-versa.
        >
        short sequenceNumber = 5;
        short msgLength = 12000;
        >
        byte[] sequenceNumberB ytes = BitConverter.Ge tBytes(sequence Number);
        // The length of the byte array will be 2
        Console.WriteLi ne(sequenceNumb erBytes.Length) ;
        >
        byte[] msgLengthBytes = BitConverter.Ge tBytes(msgLengt h);
        // The length of the byte array will be 2
        Console.WriteLi ne(msgLengthByt es.Length); // This will print 2
        >
        Hope this helps.
        >
        --
        Regards,
        Aditya.P
        >
        >
        "Rain" wrote:
        >
        hi,

        im a newbie here and in desperate need of help. Some of you might think this
        is a stupid question or a too easy to answer question but here goes. I am
        making a UDP client server application using C# everything is all set except
        for one thing. when sending a message, the message should contain 2 byte for
        squence number another 2 bytes for lenght and the rest is for data or the
        message. my problem is i dont know how to code the needed 2 bytes.. i mean
        how do i code 2 bytes or can anyone help me with the concept of using this
        bytes and how do i declare 2 bytes and use it? i really cant understand...
        thank you so much! i would really appreciate any help at all...

        Comment

        • Jon Skeet [C# MVP]

          #5
          RE: byte problem

          Rain <Rain@discussio ns.microsoft.co mwrote:
          if i have a string, how do i get the first 2 bytes of it? thanks
          Strings are sequences of *characters*, not bytes. If you have arbitrary
          binary data in your string at the start, you're asking for trouble.

          How did you read the string to start with? You might find it better to
          read two bytes and then read the string.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...