Home > Back-end >  Small white asked a questions about the long integer is converted into a string
Small white asked a questions about the long integer is converted into a string

Time:12-17


The final * STR='\ 0';
How is add terminator at the end of the string, * STR is not a pointer to the first string array? How here again become the last one?
Please help have a look at the great god, thank you

CodePudding user response:

The original poster can study * str++, STR (*) + +, * (str++)

CodePudding user response:

Because the previous operation str++; This operation will make the position of the pointer move back one element;
Long plastic string, also can consider to use sprintf, snprintf function, more simple,
 sprintf (buf, "% lu", dat); 

CodePudding user response:

If you are your own practice, for reference only, otherwise is to use system apis that ltoa

 void Long2Str (long _l, char * outStr) 
{
Char cTemp [20]={0};//an array of temporarily is the reverse of the string
Char * pTemp=cTemp;
If (_l & lt; 0)
{
* outStr++='-';
_l *=1;
}
Long l1.
Long l2=_l;
Do
{
L1=l2%10;
* pTemp++=l1 + '0'.
L2/=10;
} while (l2 & gt; 0);
PTemp -;

//will reverse the string of positive sequence copied to the output string
While (pTemp & gt;=cTemp)
{
* outStr++ pTemp=* -;
}
}

Int _tmain (int arg c, _TCHAR * argv [])
{
Long l=1125412;
Char STR [20]={0};
Long2Str (l, STR);
Printf (" % s \ n ", STR);
}

CodePudding user response:


The reason on this line:
 * str++=buf [I] + '0'. 
//each loop first take STR value: * STR=buf [I] + '0'
//then STR value add 1: str++
//while loop at the end of the STR pointer to the end of the string,


CodePudding user response:

refer to the second floor cfjtaishan response:
str++ anymore because previous operations; This operation will make the position of the pointer move back one element;
Long plastic string, also can consider to use sprintf, snprintf function, more simple,
 sprintf (buf, "% lu", dat); 


Is single chip inside of one line of code, and finally add end there don't understand, thank you moderator greatly

CodePudding user response:

reference 5 floor qq_37596462 reply:
Quote: refer to the second floor cfjtaishan response:

Because the previous operation str++; This operation will make the position of the pointer move back one element;
Long plastic string, also can consider to use sprintf, snprintf function, more simple,
 sprintf (buf, "% lu", dat); 


Is the inside of the single chip microcomputer one line of code, finally add end there don't understand, thank you moderator greatly




As reference is wrong,, embarrassed, small white rarely Posting,, sorry

CodePudding user response:

reference starytx reply: 3/f
if you are your own practice, for reference only, otherwise is to use system apis that ltoa

 void Long2Str (long _l, char * outStr) 
{
Char cTemp [20]={0};//an array of temporarily is the reverse of the string
Char * pTemp=cTemp;
If (_l & lt; 0)
{
* outStr++='-';
_l *=1;
}
Long l1.
Long l2=_l;
Do
{
L1=l2%10;
* pTemp++=l1 + '0'.
L2/=10;
} while (l2 & gt; 0);
PTemp -;

//will reverse the string of positive sequence copied to the output string
While (pTemp & gt;=cTemp)
{
* outStr++ pTemp=* -;
}
}

Int _tmain (int arg c, _TCHAR * argv [])
{
Long l=1125412;
Char STR [20]={0};
Long2Str (l, STR);
Printf (" % s \ n ", STR);
}





Thank you moderator greatly

CodePudding user response:

Can take it in turn, low in high before? I use this for the countdown, 10 s bottom to 9 s, into 90 and 80, in turn down
  • Related