linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Randi Botse <nightdecoder@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Byte ordering
Date: Fri, 4 Sep 2009 19:04:49 +0700	[thread overview]
Message-ID: <34e1241d0909040504l7a60b9d6xe5daaf2f53658824@mail.gmail.com> (raw)

Hi all,

Given some codes:

...
unsigned int val = 0x0a0b0c0d;
unsigned char *str;

str = (unsigned char *) &val;
printf("%.2x %.2x %.2x %.2x\n", *str, *(str + 1), *(str + 2), *(str + 3));
...

the output is  " 0d 0c 0b 0a "

the given codes try to divide a 32 bit integer into it's 8 bit nibbles.
with bit shifting and masking ( that you all teach me.... thanks u ;)
) i can get it correctly:

printf("%.2x %.2x %.2x %.2x\n", val >> 24 & 0xff, val >> 16 & 0xff,
val >> 8 & 0xff, val & 0xff));  /* 0a 0b 0c 0d */

but when address it with pointer to character i got it reversed, could
you please explain me why pointer to character reverse the ordering?
if what i need is only to reverse the ordering (pointer to character
index) to get a valid order? and if this a valid method?

              - Randi

             reply	other threads:[~2009-09-04 12:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-04 12:04 Randi Botse [this message]
2009-09-04 12:57 ` Byte ordering Jonathan Nell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=34e1241d0909040504l7a60b9d6xe5daaf2f53658824@mail.gmail.com \
    --to=nightdecoder@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).