Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Aug 2012 10:33:46 +0200
From:      Daniel Grech <dgre090@gmail.com>
To:        freebsd-hackers@freebsd.org
Subject:   Unsigned Integer Encoding
Message-ID:  <CAG167sY3-i_MimZGJ7qv9NC-u6e2w6RWq_fZt3amFn3%2BjdeKbQ@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
Hi,

I have what is probably a really elementary question but I can't seem to
figure it out. In the following snippet of code, why is it that a and b do
not have the same value in the end ?  :

#define uint32_t unsigned int
#define uint16_t unsigned short
#define uint8_t unsigned char
#define uint64_t unsigned long long

int main ()
{
uint32_t a = 0x01020304;

/* This prints 01020304 */
printf ("a = %0X \n",a);

uint32_t * b;

uint8_t bytes [] = {0x01,0x02,0x03,0x04};

b = (uint32_t *) bytes;

/* This prints 04030201 */
printf ("b= %0X \n", *b);

return 1;
}

Im asking this as I am currently encoding a protocol in which i receive
data as a sequence of bytes. Casting for example 4 bytes from this stream
leaves me with the situation in variable b, while the situation I am
looking to accomplish is the one in A (i.e. the bytes are not encoded in
reverse form).

Thanks in advance for your help.

Regards,

Daniel



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAG167sY3-i_MimZGJ7qv9NC-u6e2w6RWq_fZt3amFn3%2BjdeKbQ>