From owner-freebsd-hackers@FreeBSD.ORG Wed Aug 15 08:48:01 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id ADAD4106567E for ; Wed, 15 Aug 2012 08:48:01 +0000 (UTC) (envelope-from vhaisman@gmail.com) Received: from mail-bk0-f54.google.com (mail-bk0-f54.google.com [209.85.214.54]) by mx1.freebsd.org (Postfix) with ESMTP id 295888FC08 for ; Wed, 15 Aug 2012 08:48:00 +0000 (UTC) Received: by bkcje9 with SMTP id je9so499928bkc.13 for ; Wed, 15 Aug 2012 01:47:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type; bh=+SFQeTvb1qficjmaQN9VFlKu9Xy/3AgCVFL/efq/RPw=; b=Q5GrAQd6L4HCSfvwtJ5BlHnlH/gYimz6Zgy/giWjgZrO6bHXOphrEEjF4IPiWkZjLy 5zE1ppo1zeV0iNx+xl/Mf9WEQcGE1zEYMYLv2S4q9n+X9E04OLij6DZ4olIySsfAVh0n rR9KxBm9dj9ECFLyWpUrcH3S9M9OouOWsMU2StaMtqaE5syXe1OnH/qfyVwfGc//MgsB V0RdEiy3VB/lH0WKFGkJGvZ0JesvG78VdP+bo/utSIefOih3fPXvXEi4ezgyheUxlxy0 AdWcT99t4x48FCxnNFF6rsE2+wgwbJoWjQGGJh/PrFfEZalJYrR27E+4w/bBK3FYb60Y dGdw== Received: by 10.204.130.211 with SMTP id u19mr7326339bks.24.1345020479659; Wed, 15 Aug 2012 01:47:59 -0700 (PDT) Received: from [10.0.0.1] (242.91.broadband5.iol.cz. [88.100.91.242]) by mx.google.com with ESMTPS id t23sm380630bks.4.2012.08.15.01.47.57 (version=SSLv3 cipher=OTHER); Wed, 15 Aug 2012 01:47:58 -0700 (PDT) Message-ID: <502B623B.2070606@gmail.com> Date: Wed, 15 Aug 2012 10:47:55 +0200 From: =?ISO-8859-1?Q?V=E1clav_Zeman?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: freebsd-hackers@freebsd.org References: In-Reply-To: X-Enigmail-Version: 1.5a1pre Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------enig73BEA7D173F9960B8AA395F1" Subject: Re: Unsigned Integer Encoding X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Aug 2012 08:48:01 -0000 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig73BEA7D173F9960B8AA395F1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 08/15/2012 10:33 AM, Daniel Grech wrote: > Hi, > > I have what is probably a really elementary question but I can't seem t= o > 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 =3D 0x01020304; > > /* This prints 01020304 */ > printf ("a =3D %0X \n",a); > > uint32_t * b; > > uint8_t bytes [] =3D {0x01,0x02,0x03,0x04}; > > b =3D (uint32_t *) bytes; You cannot do this cast. The bytes array does not have to be sufficiently aligned and you will get SIGBUS errors on strict alignment platforms. Use memcpy() to copy from the bytes array to b instead, that is the only portable way to do this. > > /* This prints 04030201 */ This is correct for LE platforms. The least significant byte (1) is first in the array. You need to know which order of bytes does the protocol use for transport (LE or BE) and then have the implementation either swap or not swap the bytes appropriately. > printf ("b=3D %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 stre= am > 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 i= n > reverse form). > > Thanks in advance for your help. --=20 VZ --------------enig73BEA7D173F9960B8AA395F1 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iF4EAREIAAYFAlArYjsACgkQ6OWUyaYNY6MRzgD7BdmGsUzPIYKZKFZ/QofJsLdn L9f4HG0GC8fH9eL2U74A/2ONwGMLRWtpQs93ftDtO91x6nZoq5SPzfbxtknEUmIN =QQ7b -----END PGP SIGNATURE----- --------------enig73BEA7D173F9960B8AA395F1--