From owner-freebsd-hackers@FreeBSD.ORG Sat Jul 4 07:17:20 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 28267106567E for ; Sat, 4 Jul 2009 07:17:20 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from poseidon.ceid.upatras.gr (poseidon.ceid.upatras.gr [150.140.141.169]) by mx1.freebsd.org (Postfix) with ESMTP id 9269C8FC18 for ; Sat, 4 Jul 2009 07:17:19 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from mail.ceid.upatras.gr (unknown [10.1.0.143]) by poseidon.ceid.upatras.gr (Postfix) with ESMTP id 48182EB4F4C; Sat, 4 Jul 2009 09:59:25 +0300 (EEST) Received: from localhost (europa.ceid.upatras.gr [127.0.0.1]) by mail.ceid.upatras.gr (Postfix) with ESMTP id 3A3AC4509B; Sat, 4 Jul 2009 09:59:25 +0300 (EEST) X-Virus-Scanned: amavisd-new at ceid.upatras.gr Received: from mail.ceid.upatras.gr ([127.0.0.1]) by localhost (europa.ceid.upatras.gr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JonymHWOUCQU; Sat, 4 Jul 2009 09:59:25 +0300 (EEST) Received: from kobe.laptop (adsl70-250.kln.forthnet.gr [77.49.117.250]) by mail.ceid.upatras.gr (Postfix) with ESMTP id EE52144002; Sat, 4 Jul 2009 09:59:24 +0300 (EEST) Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id n646xOLq066166; Sat, 4 Jul 2009 09:59:24 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id n646xOVm066165; Sat, 4 Jul 2009 09:59:24 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: Alexander Best References: Date: Sat, 04 Jul 2009 09:59:24 +0300 In-Reply-To: (Alexander Best's message of "Wed, 01 Jul 2009 00:06:05 +0200 (CEST)") Message-ID: <87hbxtrl0z.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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: Sat, 04 Jul 2009 07:17:20 -0000 On Wed, 01 Jul 2009 00:06:05 +0200 (CEST), Alexander Best wrote: > thanks for all the help. i decided to take the pill and coded all the > fprintfs by hand. here's the result. usually i'd stick to a higher > level languag, but i need C's inline assembly support: > > struct Header > { > u_int8_t rom_entry[4]; > u_int8_t nintendo_logo[156]; ... > fprintf(stderr, "\nNintendo Logo: 0x"); > for (i=0; i < sizeof(hdr->nintendo_logo); i++) > fprintf(stderr, "%x", hdr->nintendo_logo[i]); Note that %x will only print *one* digit for values smaller than 16, i.e. printf("%x", 10) => "a", so it may be useful to use "%02x" instead. > fprintf(stderr, "\nFixed Value: 0x"); > fprintf(stderr, "%x", hdr->fixed_val); For multi-byte fields, it makes sense to print "0x" first and then iterate. For single-byte values, it's probably a tiny bit faster to go only _once_ through for *printf() family formatter, i.e.: - fprintf(stderr, "\nFixed Value: 0x"); - fprintf(stderr, "%x", hdr->fixed_val); + fprintf(stderr, "\nFixed Value: 0x%x", hdr->fixed_val); Another nit that I noticed is that your last line doesn't end with "\n": > fprintf(stderr, "\nJoybus Entry Point: 0x"); > for (i=0; i < sizeof(hdr->joybus_entry); i++) fprintf(stderr, "%x", > hdr->joybus_entry[i]); Some terminal setups will *not* output the last line if it does not finish properly with a "\n", so it may be worth editing the code to avoid the "\nXXX" format idiom, and go for a format style that puts "\n" at the _end_ of output lines: fprintf(stderr, "Nintendo Logo: 0x"); for (i = 0; i < sizeof(hdr->nintendo_logo); i++) fprintf(stderr, "%02x", hdr->nintendo_logo[i]); fprintf(stderr, "\n"); fprintf(stderr, "Fixed Value: 0x%02x\n", hdr->fixed_val);