Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Portable way of reading str[4] as uint32_t Date: Thu, 02 Apr 2020 19:09:07 -0700 Organization: A noiseless patient Spider Lines: 41 Message-ID: <86eet5pbgc.fsf@linuxsc.com> References: <40d1f926-8737-4332-93f2-c37c46f731f9@googlegroups.com> <5a25b5b4-bd49-4a4a-a64b-3799a000b7f9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="764a289b4fffae23755ccdbd2c0254f8"; logging-data="17123"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ckzV/nwGvwzhxyb2W/bXctPDPmEbA1VE=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:/xM3elZIzL5TDecYWCXBOh4Ii44= sha1:WZxbK/hWEl9R+eTQFZ7gSFBcm7I= Xref: csiph.com comp.lang.c:150966 Spiros Bousbouras writes: > On Mon, 30 Mar 2020 23:28:33 -0700 (PDT) > wyniijj@gmail.com wrote: > >> Richard Damon >> >>> On 3/30/20 2:42 PM, wyniijj@gmail.com wrote: >>> >>>> I'd like to read the value a.value in the following program as >>>> 0x78563412 in a portable way, none of hton or ntoh is useful. >>>> How should I do? >>>> >>>> #include >>>> #include >>>> >>>> int main() >>>> { >>>> union { >>>> char str[4]; >>>> uint32_t value; >>>> } a; >>>> >>>> a.str[0]=0x12; >>>> a.str[1]=0x34; >>>> a.str[2]=0x56; >>>> a.str[3]=0x78; >>>> >>>> printf("host= %08x\n",a.value); >>>> printf("hton= %08x\n",htonl(a.value)); >>>> printf("ntoh= %08x\n",ntohl(a.value)); >>>> return 0; >>>> } [...] > I note also that to have uint32_t you need > #include Actually not, because was already #include'd. See for example the man page for htonl().