I've been trying to sprintf 32-bit unsigned integers. I've used %lu and %lx, and in either case, when the most significant bit is a 1, the data is extended with 1's to 64 bits. So if it's decimal, it'll be about 1.8e19, and if it's hex, it'll be extended with f's to 16 characters (instead of 8). The parameter list is properly being read as 32-bit parameters, and anything without a leading one looks fine. I'm using the Rowley CrossWorks compiler, if that matters. If anyone knows what I'm doing wrong, I'd love to hear it. Thanks, Miles PS: Here's an example: sprintf (message, "%10lx - %10lx - %10lx - %10lx", 0xfedcba98, 0x12345678, 0x87654321, 0x000100ff); "fffffffffedcba98 - 12345678 - ffffffff87654321 - 100ff"
printf confusion
Started by ●July 28, 2004
Reply by ●July 28, 20042004-07-28
In my opinion, I think the reason you are having a problem is because you told the compiler you had long integers, as opposed to unsigned long integers in the print specification string. Then, when they are extended to 10 digits, it extends the sign of the value you gave it. Any of the values with the uppermost bit set are negative so they are sign extended. Try casting the constants e.g.(unsigned long)0x87654321. Lou >From: Miles Gazic <mgazic@mgaz...> >Reply-To: msp430@msp4... >To: msp430@msp4... >Subject: [msp430] printf confusion >Date: Wed, 28 Jul 2004 14:56:28 -0400 > >I've been trying to sprintf 32-bit unsigned integers. I've used %lu and >%lx, and in either case, when the most significant bit is a 1, the data >is extended with 1's to 64 bits. So if it's decimal, it'll be about >1.8e19, and if it's hex, it'll be extended with f's to 16 characters >(instead of 8). The parameter list is properly being read as 32-bit >parameters, and anything without a leading one looks fine. > >I'm using the Rowley CrossWorks compiler, if that matters. If anyone >knows what I'm doing wrong, I'd love to hear it. > >Thanks, >Miles > > >PS: Here's an example: > >sprintf >(message, "%10lx - %10lx - %10lx - %10lx", > 0xfedcba98, 0x12345678, 0x87654321, 0x000100ff); > >"fffffffffedcba98 - 12345678 - ffffffff87654321 - 100ff" > _________________________________________________________________ Is your PC infected? Get a FREE online computer virus scan from McAfee Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid963
Reply by ●July 29, 20042004-07-29
Lou,
Thanks for the advice. I switched it to:
sprintf
(message, "%lx - %lx - %lx - %lx",
(unsigned long)0xfedcba98, (unsigned long)0x12345678,
(unsigned long)0x87654321, (unsigned long)0x000100ff);
but still no dice. That doesn't surprise me, because in my original
code I was passing variables of type unsigned long into the function.
Removing the width value doesn't make a difference. AFAIK there is no
way to tell sprintf that you want to print a "unsigned" hex number. A
d
is decimal, a u is unsigned decimal, and a x is hex. With either a x or
a u, it sign extends to 64 bits worth of data.
- Miles
Lou C wrote:
>In my opinion, I think the reason you are having a
problem is because you
>told the compiler you had long integers, as opposed to unsigned long
>integers in the print specification string. Then, when they are extended to
>10 digits, it extends the sign of the value you gave it. Any of the values
>with the uppermost bit set are negative so they are sign extended. Try
>casting the constants e.g.(unsigned long)0x87654321.
>
>Lou
>
>
>
>
>>From: Miles Gazic <mgazic@mgaz...>
>>Reply-To: msp430@msp4...
>>To: msp430@msp4...
>>Subject: [msp430] printf confusion
>>Date: Wed, 28 Jul 2004 14:56:28 -0400
>>
>>I've been trying to sprintf 32-bit unsigned integers. I've
used %lu and
>>%lx, and in either case, when the most significant bit is a 1, the data
>>is extended with 1's to 64 bits. So if it's decimal,
it'll be about
>>1.8e19, and if it's hex, it'll be extended with f's to 16
characters
>>(instead of 8). The parameter list is properly being read as 32-bit
>>parameters, and anything without a leading one looks fine.
>>
>>I'm using the Rowley CrossWorks compiler, if that matters. If
anyone
>>knows what I'm doing wrong, I'd love to hear it.
>>
>>Thanks,
>>Miles
>>
>>
>>PS: Here's an example:
>>
>>sprintf
>>(message, "%10lx - %10lx - %10lx - %10lx",
>> 0xfedcba98, 0x12345678, 0x87654321, 0x000100ff);
>>
>>"fffffffffedcba98 - 12345678 - ffffffff87654321 -
100ff"
>>
>>
>>
Reply by ●July 29, 20042004-07-29
I had to specify in my compiler a "large" option for formatted writes, prior to that I was running into problems printing doubles or floats. I'm using the IAR compiler. ken --- In msp430@msp4..., Miles Gazic <mgazic@s...> wrote: > Lou, > > Thanks for the advice. I switched it to: > sprintf > (message, "%lx - %lx - %lx - %lx", > (unsigned long)0xfedcba98, (unsigned long)0x12345678, > (unsigned long)0x87654321, (unsigned long)0x000100ff); > > but still no dice. That doesn't surprise me, because in my original > code I was passing variables of type unsigned long into the function. > Removing the width value doesn't make a difference. AFAIK there is no > way to tell sprintf that you want to print a "unsigned" hex number. A d > is decimal, a u is unsigned decimal, and a x is hex. With either a x or > a u, it sign extends to 64 bits worth of data. > > - Miles > > Lou C wrote: > > >In my opinion, I think the reason you are having a problem is because you > >told the compiler you had long integers, as opposed to unsigned long > >integers in the print specification string. Then, when they are extended to > >10 digits, it extends the sign of the value you gave it. Any of the values > >with the uppermost bit set are negative so they are sign extended. Try > >casting the constants e.g.(unsigned long)0x87654321. > > > >Lou > > > > > > > > > >>From: Miles Gazic <mgazic@s...> > >>Reply-To: msp430@msp4... > >>To: msp430@msp4... > >>Subject: [msp430] printf confusion > >>Date: Wed, 28 Jul 2004 14:56:28 -0400 > >> > >>I've been trying to sprintf 32-bit unsigned integers. I've used % lu and > >>%lx, and in either case, when the most significant bit is a 1, the data > >>is extended with 1's to 64 bits. So if it's decimal, it'll be about > >>1.8e19, and if it's hex, it'll be extended with f's to 16 characters > >>(instead of 8). The parameter list is properly being read as 32- bit > >>parameters, and anything without a leading one looks fine. > >> > >>I'm using the Rowley CrossWorks compiler, if that matters. If anyone > >>knows what I'm doing wrong, I'd love to hear it. > >> > >>Thanks, > >>Miles > >> > >> > >>PS: Here's an example: > >> > >>sprintf > >>(message, "%10lx - %10lx - %10lx - %10lx", > >> 0xfedcba98, 0x12345678, 0x87654321, 0x000100ff); > >> > >>"fffffffffedcba98 - 12345678 - ffffffff87654321 - 100ff" > >> > >> > >>