swapping bytes in an integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john@fcs.uga.edu

    swapping bytes in an integer

    If I have a 32 bit unsigned int that is in the wrong byte order, how
    can I convert it? For example, if have a number 0x090a0b0c how can I
    reverse this to 0x0c0b0a09 ?

    Thanks,
    -John

  • W Marsh

    #2
    Re: swapping bytes in an integer

    On 8 May 2006 14:03:08 -0700, john@fcs.uga.ed u wrote:
    [color=blue]
    >If I have a 32 bit unsigned int that is in the wrong byte order, how
    >can I convert it? For example, if have a number 0x090a0b0c how can I
    >reverse this to 0x0c0b0a09 ?
    >
    >Thanks,
    >-John[/color]

    The shift operators << and >> shift a value left and right by one bit,
    respectively. Shifting by 4 bits will move the value by one hex digit.
    You can combine values with the bitwise OR operator, |

    Comment

    • Ian Collins

      #3
      Re: swapping bytes in an integer

      john@fcs.uga.ed u wrote:[color=blue]
      > If I have a 32 bit unsigned int that is in the wrong byte order, how
      > can I convert it? For example, if have a number 0x090a0b0c how can I
      > reverse this to 0x0c0b0a09 ?
      >[/color]
      It my be non-standard, bit if you system has the networking library
      function ntohl and you don't require portability, you can use that.

      --
      Ian Collins.

      Comment

      • Tomás

        #4
        Re: swapping bytes in an integer

        [color=blue]
        > Monkey monkey = 0x090a0b0c;[/color]


        Opps! Should have written:


        Monkey monkey;

        monkey.four_byt es = 0x090a0b0c;


        -Tomás

        Comment

        • W Marsh

          #5
          Re: swapping bytes in an integer

          On Mon, 08 May 2006 21:35:03 GMT, "Tomás" <NULL@NULL.NULL > wrote:
          [color=blue]
          >[color=green]
          >> Monkey monkey = 0x090a0b0c;[/color]
          >
          >
          >Opps! Should have written:
          >
          >
          > Monkey monkey;
          >
          > monkey.four_byt es = 0x090a0b0c;
          >
          >
          >-Tomás[/color]

          It makes no difference - we have no idea what Monkey is anyway. It was
          probably simpler the first way, we could just assume it was an integer
          of some description.

          Comment

          • Keith Thompson

            #6
            Re: swapping bytes in an integer

            W Marsh <wayne.marsh@gm ail.com> writes:[color=blue]
            > On 8 May 2006 14:03:08 -0700, john@fcs.uga.ed u wrote:[color=green]
            >>If I have a 32 bit unsigned int that is in the wrong byte order, how
            >>can I convert it? For example, if have a number 0x090a0b0c how can I
            >>reverse this to 0x0c0b0a09 ?[/color]
            >
            > The shift operators << and >> shift a value left and right by one bit,
            > respectively. Shifting by 4 bits will move the value by one hex digit.
            > You can combine values with the bitwise OR operator, |[/color]

            No, the shift operators shift a value by a specified number of bits.
            For example, x<<1 yields x shifted left by one bit, x<<4 yields x
            shifted left by 4 bits, and x<<N yields x shifted left by N bits.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Harald van Dijk

              #7
              Re: swapping bytes in an integer

              Ian Collins wrote:[color=blue]
              > john@fcs.uga.ed u wrote:[color=green]
              > > If I have a 32 bit unsigned int that is in the wrong byte order, how
              > > can I convert it? For example, if have a number 0x090a0b0c how can I
              > > reverse this to 0x0c0b0a09 ?
              > >[/color]
              > It my be non-standard, bit if you system has the networking library
              > function ntohl and you don't require portability, you can use that.[/color]

              If I understand correctly, ntohl() converts a big endian integer to a
              system integer, meaning if system integers are already big endian, it
              simply returns whatever you pass it. So it may not be useful even when
              it does exist.

              Treating the number when it has the wrong byte order as an array of
              unsigned char, and manually combining octets (or manually splitting the
              number into them, if you have to go the other way), is a more portable
              alternative, which if done right should work on any system.

              Comment

              • W Marsh

                #8
                Re: swapping bytes in an integer

                On Mon, 08 May 2006 21:53:59 GMT, Keith Thompson <kst-u@mib.org>
                wrote:
                [color=blue]
                >W Marsh <wayne.marsh@gm ail.com> writes:[color=green]
                >> On 8 May 2006 14:03:08 -0700, john@fcs.uga.ed u wrote:[color=darkred]
                >>>If I have a 32 bit unsigned int that is in the wrong byte order, how
                >>>can I convert it? For example, if have a number 0x090a0b0c how can I
                >>>reverse this to 0x0c0b0a09 ?[/color]
                >>
                >> The shift operators << and >> shift a value left and right by one bit,
                >> respectively. Shifting by 4 bits will move the value by one hex digit.
                >> You can combine values with the bitwise OR operator, |[/color]
                >
                >No, the shift operators shift a value by a specified number of bits.
                >For example, x<<1 yields x shifted left by one bit, x<<4 yields x
                >shifted left by 4 bits, and x<<N yields x shifted left by N bits.[/color]

                I meant to write "shift a value left and right through bits", yes.

                Comment

                • Ian Collins

                  #9
                  Re: swapping bytes in an integer

                  Harald van Dijk wrote:[color=blue]
                  > Ian Collins wrote:
                  >[color=green]
                  >>john@fcs.uga. edu wrote:
                  >>[color=darkred]
                  >>>If I have a 32 bit unsigned int that is in the wrong byte order, how
                  >>>can I convert it? For example, if have a number 0x090a0b0c how can I
                  >>>reverse this to 0x0c0b0a09 ?
                  >>>[/color]
                  >>
                  >>It my be non-standard, bit if you system has the networking library
                  >>function ntohl and you don't require portability, you can use that.[/color]
                  >
                  >
                  > If I understand correctly, ntohl() converts a big endian integer to a
                  > system integer, meaning if system integers are already big endian, it
                  > simply returns whatever you pass it. So it may not be useful even when
                  > it does exist.
                  >[/color]
                  Yes, you are correct. My advice was wrong.

                  --
                  Ian Collins.

                  Comment

                  • Flash Gordon

                    #10
                    Re: swapping bytes in an integer

                    john@fcs.uga.ed u wrote:[color=blue]
                    > If I have a 32 bit unsigned int that is in the wrong byte order, how
                    > can I convert it? For example, if have a number 0x090a0b0c how can I
                    > reverse this to 0x0c0b0a09 ?[/color]

                    Depends on whether you want to convert it in place or not. Anyway, the
                    simple way to understand is using shift operators >> and << and bitwise
                    and & for masking.
                    --
                    Flash Gordon, living in interesting times.
                    Web site - http://home.flash-gordon.me.uk/
                    comp.lang.c posting guidelines and intro:

                    Comment

                    • john@fcs.uga.edu

                      #11
                      Re: swapping bytes in an integer

                      Ian Collins wrote:[color=blue]
                      > john@fcs.uga.ed u wrote:[color=green]
                      > > If I have a 32 bit unsigned int that is in the wrong byte order, how
                      > > can I convert it? For example, if have a number 0x090a0b0c how can I
                      > > reverse this to 0x0c0b0a09 ?
                      > >[/color]
                      > It my be non-standard, bit if you system has the networking library
                      > function ntohl and you don't require portability, you can use that.
                      >
                      > --
                      > Ian Collins.[/color]

                      I just tried ntohl() in my program and this works perfectly. I think I
                      will use this method. I want to thank everyone who replied for all of
                      their great ideas! This help is greatly appreciated.

                      -John

                      Comment

                      • W Marsh

                        #12
                        Re: swapping bytes in an integer

                        On 8 May 2006 15:18:42 -0700, john@fcs.uga.ed u wrote:
                        [color=blue]
                        >Ian Collins wrote:[color=green]
                        >> john@fcs.uga.ed u wrote:[color=darkred]
                        >> > If I have a 32 bit unsigned int that is in the wrong byte order, how
                        >> > can I convert it? For example, if have a number 0x090a0b0c how can I
                        >> > reverse this to 0x0c0b0a09 ?
                        >> >[/color]
                        >> It my be non-standard, bit if you system has the networking library
                        >> function ntohl and you don't require portability, you can use that.
                        >>
                        >> --
                        >> Ian Collins.[/color]
                        >
                        >I just tried ntohl() in my program and this works perfectly. I think I
                        >will use this method. I want to thank everyone who replied for all of
                        >their great ideas! This help is greatly appreciated.
                        >
                        >-John[/color]

                        Then bear in mind that it's not portable, and will have no effect when
                        compiled for a machine with a different endianess to your own.

                        Comment

                        • Andrew Poelstra

                          #13
                          Re: swapping bytes in an integer

                          On 2006-05-08, W Marsh <wayne.marsh@gm ail.com> wrote:[color=blue]
                          > On 8 May 2006 15:18:42 -0700, john@fcs.uga.ed u wrote:
                          >[color=green]
                          >>Ian Collins wrote:[color=darkred]
                          >>> john@fcs.uga.ed u wrote:
                          >>> > If I have a 32 bit unsigned int that is in the wrong byte order, how
                          >>> > can I convert it? For example, if have a number 0x090a0b0c how can I
                          >>> > reverse this to 0x0c0b0a09 ?
                          >>> >
                          >>> It my be non-standard, bit if you system has the networking library
                          >>> function ntohl and you don't require portability, you can use that.
                          >>>
                          >>> --
                          >>> Ian Collins.[/color]
                          >>
                          >>I just tried ntohl() in my program and this works perfectly. I think I
                          >>will use this method. I want to thank everyone who replied for all of
                          >>their great ideas! This help is greatly appreciated.
                          >>
                          >>-John[/color]
                          >
                          > Then bear in mind that it's not portable, and will have no effect when
                          > compiled for a machine with a different endianess to your own.[/color]

                          If his code depends on big-endianness, then it doesn't matter that the function
                          has no effect; it simply means that the machine is already set up correctly
                          for your functions.

                          I worded that really poorly, but basically I mean that it is irrelevant what
                          effect a certain function actually has; only the end result is important.

                          Comment

                          • W Marsh

                            #14
                            Re: swapping bytes in an integer

                            On Mon, 08 May 2006 22:41:34 GMT, Andrew Poelstra
                            <apoelstra@loca lhost.localdoma in> wrote:
                            [color=blue]
                            >On 2006-05-08, W Marsh <wayne.marsh@gm ail.com> wrote:[color=green]
                            >> On 8 May 2006 15:18:42 -0700, john@fcs.uga.ed u wrote:
                            >>[color=darkred]
                            >>>Ian Collins wrote:
                            >>>> john@fcs.uga.ed u wrote:
                            >>>> > If I have a 32 bit unsigned int that is in the wrong byte order, how
                            >>>> > can I convert it? For example, if have a number 0x090a0b0c how can I
                            >>>> > reverse this to 0x0c0b0a09 ?
                            >>>> >
                            >>>> It my be non-standard, bit if you system has the networking library
                            >>>> function ntohl and you don't require portability, you can use that.
                            >>>>
                            >>>> --
                            >>>> Ian Collins.
                            >>>
                            >>>I just tried ntohl() in my program and this works perfectly. I think I
                            >>>will use this method. I want to thank everyone who replied for all of
                            >>>their great ideas! This help is greatly appreciated.
                            >>>
                            >>>-John[/color]
                            >>
                            >> Then bear in mind that it's not portable, and will have no effect when
                            >> compiled for a machine with a different endianess to your own.[/color]
                            >
                            >If his code depends on big-endianness, then it doesn't matter that the function
                            >has no effect; it simply means that the machine is already set up correctly
                            >for your functions.
                            >
                            >I worded that really poorly, but basically I mean that it is irrelevant what
                            >effect a certain function actually has; only the end result is important.[/color]

                            No - he asked how to swap bytes, not how to manifest a specific
                            endianess.

                            If he has data he wants to treat natively, then ntohl is fine.

                            If he actually wants to swap the bytes of an integer in any case
                            (implementing a swap opcode in a CPU emulator, for example), he won't
                            be able to use ntohl.

                            Comment

                            • EventHelix.com

                              #15
                              Re: swapping bytes in an integer

                              > If I have a 32 bit unsigned int that is in the wrong byte order, how[color=blue]
                              > can I convert it? For example, if have a number 0x090a0b0c how can I
                              > reverse this to 0x0c0b0a09 ?[/color]

                              The following article should help:


                              --
                              EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
                              Sequence Diagram Based System Design and Object Interaction Modeling
                              Tool

                              Comment

                              Working...