martes, 13 de marzo de 2012 7:13
I have a line of code -
//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
It works, and works absolutely fine.
May I know, why Format behaving in such manner?
Todas las respuestas
martes, 13 de marzo de 2012 8:00
...is not correct if you are usong the type LONGLONG for variable num. you must use the correctponding prefix. See here:
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.
martes, 13 de marzo de 2012 8:14
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'?
martes, 13 de marzo de 2012 8:50
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":
The same variable as 64 bit looks like the followed:
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.
martes, 13 de marzo de 2012 11:14
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?
martes, 13 de marzo de 2012 11:55
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! :-(
- Editado BordonMicrosoft Community Contributor martes, 13 de marzo de 2012 11:57