Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. convert uint to byte[]

convert uint to byte[]

Scheduled Pinned Locked Moved C#
comhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Huisheng Chen
    wrote on last edited by
    #1

    Hi all, I have a byte[] to unit function, I would like to have a reverse function that convert unit to byte[], please help. thanks a lot.

        public static uint ParseUint(byte\[\] buffer, int uintLengthInBytes, ref int offset)
        {
            uint value = 0;
            int i, j;
            for (i = offset + uintLengthInBytes - 1, j = 0; i >= offset; i--, j++)
                value |= (uint)(buffer\[i\] << (8 \* j));
            offset += uintLengthInBytes;
            return value;
        }
    

    Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com

    L 1 Reply Last reply
    0
    • H Huisheng Chen

      Hi all, I have a byte[] to unit function, I would like to have a reverse function that convert unit to byte[], please help. thanks a lot.

          public static uint ParseUint(byte\[\] buffer, int uintLengthInBytes, ref int offset)
          {
              uint value = 0;
              int i, j;
              for (i = offset + uintLengthInBytes - 1, j = 0; i >= offset; i--, j++)
                  value |= (uint)(buffer\[i\] << (8 \* j));
              offset += uintLengthInBytes;
              return value;
          }
      

      Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      BitConverter.GetBytes

      H 1 Reply Last reply
      0
      • L Lost User

        BitConverter.GetBytes

        H Offline
        H Offline
        Huisheng Chen
        wrote on last edited by
        #3

        hi, I know that method, the result is wrong. please take a closer look at the original code :doh:

        Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com

        L 1 Reply Last reply
        0
        • H Huisheng Chen

          hi, I know that method, the result is wrong. please take a closer look at the original code :doh:

          Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          What? Is it in Big Endian or something? Just Array.Reverse the result?

          H A 2 Replies Last reply
          0
          • L Lost User

            What? Is it in Big Endian or something? Just Array.Reverse the result?

            H Offline
            H Offline
            Huisheng Chen
            wrote on last edited by
            #5

            sorry, my debugging mistake, you are right. thanks a lot.:rose:

            Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com

            L 1 Reply Last reply
            0
            • L Lost User

              What? Is it in Big Endian or something? Just Array.Reverse the result?

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              Byte order reversal? Use System.BitConverter and System.Net.IPAddress.NetworkToHostOrder/HostToNetworkOrder. One I did earlier:

              /// <summary>
              /// Byte reverse the value and write to the buffer at ofs
              /// </summary>
              /// <param name="i">value to write</param>
              /// <param name="ofs">Write offset in the buffer</param>
              internal void WriteUInt32(UInt32 i, Int32 ofs, Byte\[\] buffer) {
                Int32 j = IPAddress.HostToNetworkOrder(unchecked((Int32)i));
                Byte\[\] b = BitConverter.GetBytes(j);
                b.CopyTo(buffer, ofs);
              }
              

              Alan.

              L 1 Reply Last reply
              0
              • A Alan N

                Byte order reversal? Use System.BitConverter and System.Net.IPAddress.NetworkToHostOrder/HostToNetworkOrder. One I did earlier:

                /// <summary>
                /// Byte reverse the value and write to the buffer at ofs
                /// </summary>
                /// <param name="i">value to write</param>
                /// <param name="ofs">Write offset in the buffer</param>
                internal void WriteUInt32(UInt32 i, Int32 ofs, Byte\[\] buffer) {
                  Int32 j = IPAddress.HostToNetworkOrder(unchecked((Int32)i));
                  Byte\[\] b = BitConverter.GetBytes(j);
                  b.CopyTo(buffer, ofs);
                }
                

                Alan.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Nice! I'm going to remember that one

                1 Reply Last reply
                0
                • H Huisheng Chen

                  sorry, my debugging mistake, you are right. thanks a lot.:rose:

                  Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You're welcome

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups