From owner-freebsd-hackers@FreeBSD.ORG Thu Jul 2 01:48:24 2009 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 D318F1065678 for ; Thu, 2 Jul 2009 01:48:24 +0000 (UTC) (envelope-from unixmania@gmail.com) Received: from mail-bw0-f216.google.com (mail-bw0-f216.google.com [209.85.218.216]) by mx1.freebsd.org (Postfix) with ESMTP id 506968FC18 for ; Thu, 2 Jul 2009 01:48:23 +0000 (UTC) (envelope-from unixmania@gmail.com) Received: by bwz12 with SMTP id 12so1094344bwz.43 for ; Wed, 01 Jul 2009 18:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=hv1U3MIqY0OcMHAFDAfQvAhDxYfGmg4Y8+ZLbbWk0ac=; b=QG9zjqs3BT4ne5vlrlesAZjxoNDMgmAvQ/xMAeLAmWdY5ku4jQ6sAkomLRGXAF7uoS 2nJSrSO1nHar1FKSkYYgqEpsir4DMeGoZtoF/sWmlgtW4aBCuWy8dQbW20ZkeomOaWJr Y2qMih8VudeLk3n57DQ9WveB4uroGAoJlHgdQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=Mb/1KAZ8GfrtU0raMoLIUcJ5WUHPL+Y4wDMuvl1pOR4De21+DRoLfbFHkpsm1RzvKm pATvqu0t4gqlWwqb77otbh52vFY6ymv14TYOAfhoWlZhZSr9drm1ADVCRb5la7fSVfDm hmpNDFhkqwmNQvJoAylDpyIvx41+dFMTbQ89k= MIME-Version: 1.0 Received: by 10.239.141.68 with SMTP id b4mr888956hba.11.1246499303066; Wed, 01 Jul 2009 18:48:23 -0700 (PDT) In-Reply-To: References: <20090630210238.GA33849@keira.kiwi-computer.com> Date: Wed, 1 Jul 2009 22:48:23 -0300 Message-ID: From: "Carlos A. M. dos Santos" To: Alexander Best Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: rick-freebsd2008@kiwi-computer.com, freebsd-hackers@freebsd.org Subject: Re: c question: *printf'ing arrays 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: Thu, 02 Jul 2009 01:48:25 -0000 On Tue, Jun 30, 2009 at 7:06 PM, Alexander Best wrote: > thanks for all the help. i decided to take the pill and coded all the fpr= intfs > by hand. here's the result. usually i'd stick to a higher level languag, = but i > need C's inline assembly support: > > =A0 =A0struct Header > =A0 =A0{ > =A0 =A0 =A0 =A0u_int8_t rom_entry[4]; > =A0 =A0 =A0 =A0u_int8_t nintendo_logo[156]; > =A0 =A0 =A0 =A0u_char game_title[12]; > =A0 =A0 =A0 =A0u_char game_code[4]; > =A0 =A0 =A0 =A0u_char maker_code[2]; > =A0 =A0 =A0 =A0u_int8_t fixed_val; > =A0 =A0 =A0 =A0u_int8_t unit_code; > =A0 =A0 =A0 =A0u_int8_t device_type; > =A0 =A0 =A0 =A0u_int8_t reserved_area1[7]; > =A0 =A0 =A0 =A0u_int8_t software_version; > =A0 =A0 =A0 =A0u_int8_t complement_check; > =A0 =A0 =A0 =A0u_int8_t reserved_area2; > =A0 =A0 =A0 =A0u_int8_t ram_entry[4]; > =A0 =A0 =A0 =A0u_int8_t boot_mode; > =A0 =A0 =A0 =A0u_int8_t slave_id; > =A0 =A0 =A0 =A0u_int8_t unused_area[26]; > =A0 =A0 =A0 =A0u_int8_t joybus_entry[4]; > =A0 =A0}; > > =A0 =A0struct Header * hdr =3D rom; > =A0 =A0int i; > > =A0 =A0fprintf(stderr, "ROM Entry: 0x"); > =A0 =A0for (i=3D0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]); > =A0 =A0fprintf(stderr, "\nNintendo Logo: 0x"); > =A0 =A0for (i=3D0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, "= %x", > =A0 =A0hdr->nintendo_logo[i]); > =A0 =A0fprintf(stderr, "\nGame Title: %s", =A0hdr->game_title); > =A0 =A0fprintf(stderr, "\nGame Code: %s", =A0hdr->game_code); > =A0 =A0fprintf(stderr, "\nMaker Code: %s", =A0hdr->maker_code); > =A0 =A0fprintf(stderr, "\nFixed Value: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->fixed_val); > =A0 =A0fprintf(stderr, "\nUnit Code: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->unit_code); > =A0 =A0fprintf(stderr, "\nDevice Type: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->device_type); > =A0 =A0fprintf(stderr, "\nReserved Area: 0x"); > =A0 =A0for (i=3D0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr, = "%x", > =A0 =A0hdr->reserved_area1[i]); > =A0 =A0fprintf(stderr, "\nSoftware Version: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->software_version); > =A0 =A0fprintf(stderr, "\nComplement Check: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->complement_check); > =A0 =A0fprintf(stderr, "\nReserved Area: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->reserved_area2); > =A0 =A0fprintf(stderr, "\nRAM Entry Point: 0x"); > =A0 =A0for (i=3D0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr, "%x", > =A0 =A0hdr->ram_entry[i]); > =A0 =A0fprintf(stderr, "\nBoot Mode: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->boot_mode); > =A0 =A0fprintf(stderr, "\nSlave ID: 0x"); > =A0 =A0fprintf(stderr, "%x", hdr->slave_id); > =A0 =A0fprintf(stderr, "\nUnused Area: 0x"); > =A0 =A0for (i=3D0; i < sizeof(hdr->unused_area); i++) fprintf(stderr, "%x= ", > =A0 =A0hdr->unused_area[i]); > =A0 =A0fprintf(stderr, "\nJoybus Entry Point: 0x"); > =A0 =A0for (i=3D0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%= x", > =A0 =A0hdr->joybus_entry[i]); The code below is a bit more efficient and easier to read. It is also safer, since it limits the width of the %s conversions to the sizes of the corresponding arrays. fprintf(stderr, "ROM Entry: 0x"); for (i=3D0; i < 4; i++) fprintf(stderr, "%x", hdr->rom_entry[i]); fprintf(stderr, "\nNintendo Logo: 0x"); for (i=3D0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, "%x", hdr->nintendo_logo[i]); fprintf(stderr, "\nGame Title: %.*s" "\nGame Code: %.*s" "\nMaker Code: %.*s" "\nFixed Value: 0x%x" "\nUnit Code: 0x%x" "\nDevice Type: 0x%x" "\nReserved Area: 0x", sizeof(hdr->game_title), hdr->game_title, sizeof(hdr->game_code), hdr->game_code, sizeof(hdr->maker_code), hdr->maker_code, hdr->fixed_val, hdr->unit_code, hdr->device_type); for (i=3D0; i < sizeof(hdr->reserved_area1); i++) fprintf(stderr, "%x", hdr->reserved_area1[i]); fprintf(stderr, "\nSoftware Version: 0x%x" "\nComplement Check: 0x%x" "\nReserved Area: 0x%x" "\nRAM Entry Point: 0x", hdr->software_version, hdr->complement_check, hdr->reserved_area2); for (i=3D0; i < sizeof(hdr->ram_entry); i++) fprintf(stderr, "%x", hdr->ram_entry[i]); fprintf(stderr, "\nBoot Mode: 0x""%x" "\nSlave ID: 0x""%x" "\nUnused Area: 0x", hdr->boot_mode, hdr->slave_id); for (i=3D0; i < sizeof(hdr->unused_area); i++) fprintf(stderr, "%x", hdr->unused_area[i]); fprintf(stderr, "\nJoybus Entry Point: 0x"); for (i=3D0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%x", hdr->joybus_entry[i]); fprintf(stderr, "\n"); --=20 My preferred quotation of Robert Louis Stevenson is "You cannot make an omelette without breaking eggs". Not because I like the omelettes, but because I like the sound of eggs being broken.