CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    38

    fscanf issues (final question)

    im a bit confused by the following action
    say i have a file
    /////////////////////////////
    20
    34 04422886
    /////////////////////////////

    if i read this file using the following code

    while(fgets(buff,95,infile)) {
    sscanf(buff,"%x %x",&memAddress,&memContents);
    if(memContents == -1){
    /* code to skip first line */
    }
    else{
    memAdd[i] = memAddress;
    memCont[i] = memContents;
    }

    *note* i do have other error checking within this
    block i reduced it to the most basic read stuff for posting
    where memAddr should be 34
    and memContents should be 04422886

    and i do the folllowing

    printf("memContents = %x\n", memCont[i]);

    my output is
    memContents = 4422886

    what happened to the 0 that began the hexidecimal number

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    It is just like with any other bases (position system)... leading zeros don't count, ie 00ff == ff.

  3. #3
    Join Date
    Aug 2002
    Posts
    78
    If you're printing out a 32-bit hex number, do this:


    printf("0x%08x", theHexNumber);


    For 003abcde, will print:

    0x003abcde

    Use capital X if you want capital hex letters. FYI, with the 08 in the formatting spec, the 0 tells it to pad using "0", and the 8 tells it to output, minimum, 8 characters. Also, the leading "0x" is just pretty print. You can leave it out of the formatting spec if you like.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured