Thread: Everything is functioning, but output is undesirable?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Everything is functioning, but output is undesirable?

    I'm writing a program to do some manipulation to user inputted variables and redisplay it. Nothing difficult. However, the output isn't formatted the way it should be, although it's correct. With some (by hand) formatting to the outputted data, the correct results are given, but that's too much work to do to a quarter-hundred strings.

    I've edited the placeholders for text to be %2x instead of %x in an attempt to get things such as 4 to be displayed as 04, but I had no luck with that. I need only the last four digits to be shown for things such as "fffffab8" and for there to be 0s where they should be, if that makes sense. Take a look at what it gives me:

    Output:
    4 ff 4 ff 9 230 fffffab8 fef2 0 0

    What it should be:
    04 ff 04 ff 00 09 02 30 fa b8 fe f2 00 00 00 00

    Here's the source code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    unsigned int ofs,aa,bb,cc,dd,ee,xx,yy,zz,rr,vv;
    
    printf("\nBe prepared to give data! No spaces!\n\nExample:\n00 FF 00 FF 00 09 04 C4 01 04 02 0C C0 00 01 BF\nAA BB CC DD EE EE XX XX YY YY ZZ ZZ RR RR VV VV\n\n");
    
    printf("AA = Map to load when triggered from the front\nBB = Front trigger camera setting\nCC = Map to load when triggered from the rear\nDD = Rear trigger camera setting\nEE EE = Actor\nXX XX = X Position\nYY YY = Y Position\nZZ ZZ = Z Position\nRR RR = Rotation\nVV VV = Variable\n");
    printf("\nAA = ");
    scanf("%x",&aa);
    printf("\nBB = ");
    scanf("%x",&bb);
    printf("\nCC = ");
    scanf("%x",&cc);
    printf("\nDD = ");
    scanf("%x",&dd);
    printf("\nEE EE = ");
    scanf("%x",&ee);
    printf("\nXX XX = ");
    scanf("%x",&xx);
    printf("\nYY YY = ");
    scanf("%x",&yy);
    printf("\nZZ ZZ = ");
    scanf("%x",&zz);
    printf("\nRR RR = ");
    scanf("%x",&rr);
    printf("\nVV VV = ");
    scanf("%x",&vv);
    
    
    while(aa<0x72)
    {
        printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x \n",aa,bb,cc,dd,ee,xx,yy,zz,rr,vv);
    
        xx=xx+0x120;
        zz=zz+0x120;
        aa=aa+0x1;
        cc=cc+0x1;
        yy=yy-0x152;
    
        printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x \n",aa,bb,cc,dd,ee,xx,yy,zz,rr,vv);
        xx=xx-0x120;
        zz=zz+0x120;
        aa=aa+0x1;
        cc=cc+0x1;
        yy=yy-0x152;
    
        printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x \n",aa,bb,cc,dd,ee,xx,yy,zz,rr,vv);
        xx=xx-0x120;
        zz=zz-0x120;
        aa=aa+0x1;
        cc=cc+0x1;
        yy=yy-0x152;
    
        printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x \n",aa,bb,cc,dd,ee,xx,yy,zz,rr,vv);
        xx=xx+0x120;
        zz=zz-0x120;
        aa=aa+0x1;
        cc=cc+0x1;
        yy=yy-0x152;
    
    }
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %02x


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    40
    I still get the error of a "digit overflow" for some of them:

    02 ff 01 ff 0009 0350 fffffeae 10012 0000 0000
    ^ ^

    However, your advice worked nicely! I used %02x for the single bytes and %04x for the double bytes. When I finish a few aspects of the program later today, I'll be sure to use "Replace All" in Notepad to remove all occurrences of "FFFF" and " 1" to do the job. I may even post the final results.

    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The problem is %02x will insure there is a leading zero (if there is only one digit) to make at least 2 digits. But what if the integer is huge or negative? You could get as much as ffffffff (eight hex digits) because default integer is 32 bits worth.

    Perhaps you could use unsigned short to limit it to ffff (four hex digits worth).

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    The problem is %02x will insure there is a leading zero (if there is only one digit) to make at least 2 digits. But what if the integer is huge or negative? You could get as much as ffffffff (eight hex digits) because default integer is 32 bits worth.

    Perhaps you could use unsigned short to limit it to ffff (four hex digits worth).
    Actually if he only wants two digit results ...
    Code:
    number &= 255;
    Will guarantee that.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yup. Or could use unsigned char for all the variables.

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    40
    Thanks for the support! The program works flawlessly now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  4. Output of If-else ladder not functioning
    By SWAT_LtLen in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2005, 05:41 AM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM