locked
Format API giving unexpected results.

    Question

  • Hi All,

    I have a line of code -

    str.Format("E%i-%i",Num,i);

    //Here 'Num' is of type LONGLONG and 'i' is of type 'int'

    When I am executing this code, the formatted string always have '0' for 'i', no matter what is the value of i.

    e.g. Num is 412 and 'i' is 2. So it should return E412-2, but it returns E412-0.

    If I correct the string as below

    str.Format("E%0I64d-%i",Num,i);

    It works, and works absolutely fine.

    May I know, why Format behaving in such manner?

    Tuesday, March 13, 2012 7:13 AM

Answers

  • some internal detail:

    Your Num value 412 is 0x0000019C as 32 bit value and 0x000000000000019C as 64 bit value.

    Your variable as 32 bit will be stored in the followed way in memory ad the base address "addr":

    addr+0: 0x9C
    addr+1: 0x01
    addr+2: 0x00
    addr+3: 0x00

    The same variable as 64 bit looks like the followed:

    addr+0: 0x9C
    addr+1: 0x01
    addr+2: 0x00
    addr+3: 0x00
    addr+4: 0x00
    addr+5: 0x00
    addr+6: 0x00
    addr+7: 0x00

    If yo use "str.Format("E%i-%i",Num,i);" for your 64 bit variable you will grab the first 4 bytes for variable "Num" and the next 4 bytes for "i" since you did not use the correct size prefix. You can check this easiliy if you set the value of Num to "0x000000040000019C". You will get "E412-4" as result.

    I used the followed piece of code:

      LONGLONG Num=0x000000040000019C;
      int i=2;
      CString str;
      str.Format(_T("E%i-%i"),Num,i);
    I assume as addition the compiler calculates the adresses of your variables you are handling in the Format function with relative offsets. If you exactly want to knwo what happens you should check out the assembly code that is generated. But I'm sure it is something like that and the compiler used offset adresses.

     

     



    • Edited by Bordon Tuesday, March 13, 2012 11:57 AM fixed description
    • Marked as answer by Ansh84 Wednesday, March 14, 2012 3:04 AM
    Tuesday, March 13, 2012 8:50 AM

All replies

  • Your above piece of code...

    str.Format("E%i-%i",Num,i);

    ...is not correct if you are usong the type LONGLONG for variable num. you must use the correctponding prefix. See here:

    http://msdn.microsoft.com/en-us/library/56e442dc.aspx

    http://msdn.microsoft.com/en-us/library/tcxf1dw6(v=vs.71).aspx

    In your 2nd example you used the correct size prefix...

    str.Format("E%0I64d-%i",Num,i);
    ...for the variable LONGLONG. Because of the missing size prefix the output of your first piece of code is wrong.
    Tuesday, March 13, 2012 8:00 AM
  • Thanks Bordon!

    I get that type specifier was wrong in first place.

    I was looking for some internal detail. In above example i used wrong type specifier for 'Num' but i got the wrong value for 'i'?

    Tuesday, March 13, 2012 8:14 AM
  • some internal detail:

    Your Num value 412 is 0x0000019C as 32 bit value and 0x000000000000019C as 64 bit value.

    Your variable as 32 bit will be stored in the followed way in memory ad the base address "addr":

    addr+0: 0x9C
    addr+1: 0x01
    addr+2: 0x00
    addr+3: 0x00

    The same variable as 64 bit looks like the followed:

    addr+0: 0x9C
    addr+1: 0x01
    addr+2: 0x00
    addr+3: 0x00
    addr+4: 0x00
    addr+5: 0x00
    addr+6: 0x00
    addr+7: 0x00

    If yo use "str.Format("E%i-%i",Num,i);" for your 64 bit variable you will grab the first 4 bytes for variable "Num" and the next 4 bytes for "i" since you did not use the correct size prefix. You can check this easiliy if you set the value of Num to "0x000000040000019C". You will get "E412-4" as result.

    I used the followed piece of code:

      LONGLONG Num=0x000000040000019C;
      int i=2;
      CString str;
      str.Format(_T("E%i-%i"),Num,i);
    I assume as addition the compiler calculates the adresses of your variables you are handling in the Format function with relative offsets. If you exactly want to knwo what happens you should check out the assembly code that is generated. But I'm sure it is something like that and the compiler used offset adresses.

     

     



    • Edited by Bordon Tuesday, March 13, 2012 11:57 AM fixed description
    • Marked as answer by Ansh84 Wednesday, March 14, 2012 3:04 AM
    Tuesday, March 13, 2012 8:50 AM
  • One confusion above,

    You divided everything in chunk of two 8 bytes data? isn't it equal to to 16 bytes and so 128 bits?

    Tuesday, March 13, 2012 11:14 AM
  • I made a mistake, a int is 4 bytes not 8 bytes and LONGLONG is 8 bytes not 16. I correct the posting above.

    Sorry for the confusion! :-(

    • Edited by Bordon Tuesday, March 13, 2012 11:57 AM
    Tuesday, March 13, 2012 11:55 AM