From owner-freebsd-questions@FreeBSD.ORG Sat Jun 21 05:55:03 2003 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3573E37B401 for ; Sat, 21 Jun 2003 05:55:03 -0700 (PDT) Received: from argosy.ca (www.argosy.ca [138.73.18.1]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7285743FD7 for ; Sat, 21 Jun 2003 05:55:02 -0700 (PDT) (envelope-from hhwoo@argosy.ca) Received: from a7n8x (mctn1-7860.nb.aliant.net [156.34.22.184]) by argosy.ca (8.12.9/8.12.9) with SMTP id h5LCu9t2008835; Sat, 21 Jun 2003 09:56:10 -0300 (ADT) (envelope-from hhwoo@argosy.ca) Message-ID: <002b01c337f4$85d829f0$0200a8c0@a7n8x> From: "Han Hwei Woo" To: , "freebsd-questions" References: <200306211014.h5LAECvr038054@en26.ai1.anchorage.mtaonline.net> Date: Sat, 21 Jun 2003 09:56:24 -0300 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Subject: Re: is this a FBSD printf bug? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 Jun 2003 12:55:03 -0000 Hrrm... your question and the code seem to contradict each other. Firstly, you are assigning hex values to the characters in your array. Secondly, you are printing the values stored in the array as hex values by specifying %02x. I think you meant to ask why you are getting the preceding trail of ffff...'s. If that's the case, it's because you are assigning values to signed character variables and printing them as unsigned hex values. Thus, when you assign values greater than 127 (starting with 88), you are overriding the sign of the character. To get the output you would expect, simply declare a as an unsigned character array. e.g. unsigned char a[LEN_ARRAY+1]; ----- Original Message ----- From: To: "freebsd-questions" Sent: Saturday, June 21, 2003 7:14 AM Subject: is this a FBSD printf bug? > FBSD 4.8 > > i hope this isn't a question based on extreme > ignorance - i haven't programmed in C in a > long time, and i don't have another machine > to test this on. i can't understand why > the output of the following code produces > "ints" when given variables of type "char", > so it looks like a bug to me ... > > #include > //////////////////////////////////////////////////////////////////////////// /// > int main(int argc, char *argv[]) { > > #define LEN_ARRAY 16 > char a[LEN_ARRAY+1]; > int i; > > a[0]=0x00; > a[1]=0x11; > a[2]=0x22; > a[3]=0x33; > a[4]=0x44; > a[5]=0x55; > a[6]=0x66; > a[7]=0x77; > a[8]=0x88; > a[9]=0x99; > a[10]=0xaa; > a[11]=0xbb; > a[12]=0xcc; > a[13]=0xdd; > a[14]=0xee; > a[15]=0xff; > > for ( i = 0; i < LEN_ARRAY; ++i ) printf("[%02i]%02x\n", i, a[i]); > } > //////////////////////////////////////////////////////////////////////////// /// > // > // OUTPUT: > // > // [00]00 > // [01]11 > // [02]22 > // [03]33 > // [04]44 > // [05]55 > // [06]66 > // [07]77 > // [08]ffffff88 ??? > // [09]ffffff99 ??? > // [10]ffffffaa ??? > // [11]ffffffbb ??? > // [12]ffffffcc ??? > // [13]ffffffdd ??? > // [14]ffffffee ??? > // [15]ffffffff ??? > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" >