Keil Logo

Incorrect work of the sprintf function

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Author
Sergey Zorin
Posted
4-Dec-2017 12:38 GMT
Toolset
C51
New! Incorrect work of the sprintf function

char str [4];

sprintf (str, "%.2x", 0x01);//Conversion of byte to the 2nd bit hexadecimal value

I expect in str[0] 0 character, in str[1] 1 character, that is '0' and '1'.
As a result function returns '1' and 'C'.
Remaining numbers - it is normal...

Author
j embedded
Posted
4-Dec-2017 13:17 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

The reason is, that you only give ONE byte as value (0x01) and %x expects a TWO byte value. Then two bytes are used with only one valid, the other just undefined.

Therefore you may cast that value to 16bits like

sprintf (str, "%.2x", (unsigned int)0x01);
Author
Paul Simmons
Posted
4-Dec-2017 13:47 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

The reason is, that you only give ONE byte as value (0x01) and %x expects a TWO byte value.

Total and absolute rubbish!

If you want to get 2 digits from sprintf, you need to change it to

sprintf (str, "%2.2x", 0x01)


Separate note: This is such a strange forum, I was told my message was in error because it contained the spam word Wod. Took me too long to notice it was embedded in "two digits".

Maybe I'm naive, but i don't see why that word would be considered spam.

Author
j embedded
Posted
4-Dec-2017 16:46 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

I appreciate your "Total and absolute rubbish".
Well, I have it tested, confirmed the problem in the first place, and the fix using 16bit.
Beside that, the problem is also visible using just a simple "%x" format sting.
And I do really assume that YOU have also reproduced and tested your fix, don't you?
I will not use inappropiate wording in my response, sorry.

Author
j embedded
Posted
4-Dec-2017 17:00 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

You might also carefully read

http://www.keil.com/support/man/docs/c51/c51_printf.htm

and there understand the difference between %bx and %x, another option to solve the primary problem:

printf ("xchar %bx xint %x xlong %lx\n",x,y,z);

Got it? Reading helps!

Author
Paul Simmons
Posted
4-Dec-2017 20:00 GMT
Toolset
C51
New! Got it? Reading helps!

Oh dear. Why post a link to a page that you've clearly not fully comprehended?

Author
Kevin Map
Posted
4-Dec-2017 20:31 GMT
Toolset
C51
New! RE: Got it? Reading helps!

Nice to see active discussion.

As an arbitrator I will say that both of you are right. The (unsigned int) is there to ensure a 16 bit value is passed as a parameter and the 2.2X to ensure the resultant string consists of two hex characters. There are likely many ways to do it, but the combination works for me. In the same way that any of the C51 workarounds for the weird architecture ever made sense.

IMHO, the sooner the x51 is taken out of college teaching the better.

Author
Sergey Zorin
Posted
5-Dec-2017 10:15 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

All thanks for discussion.
Works correctly:
sprintf (str, "%.2x", (unsigned int)0x01)
sprintf (str, "%2.2x", (unsigned int)0x01)
There is a question. Why in Embarcadero® C++Builder 10.1 Berlin transformation of types isn't required.

Author
Sergey Zorin
Posted
5-Dec-2017 10:19 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

And yes. MCS51 shouldn't offend. :)
Similar it is just such realization of the sprintf function in the environment of KEIL.

Author
Paul Simmons
Posted
5-Dec-2017 10:49 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

There is a question. Why in Embarcadero® C++Builder 10.1 Berlin transformation of types isn't required.

A better question is: Why does C51 require it?

The C51 compiler does an extremely good job of making C and its libraries available to a processor that is not really suitable for such things. It requires a number of extensions to make best use of the limited resources of the old processors.

But things have really moved on since the 1990s (I was an original tester of quite a few C51 releases back in those days). Many processors (such as ones having an ARM Cortex core) don't require these extensions and 'just work'.

My suggestion to you is move across to the currently available processors and technologies as soon as you can. You'll find things a whole lot easier :)

Author
Sergey Zorin
Posted
5-Dec-2017 11:09 GMT
Toolset
C51
New! RE: Incorrect work of the sprintf function

But in case of what here processor type? It is feature of implementation of function in the environment of KEIL, only...

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.