//***************************************************************************** // //! Converts a string into its numeric equivalent. //! //! param pcStr is a pointer to the string containing the integer. //! param ppcStrRet is a pointer that will be set to the first character past //! the integer in the string. //! param iBase is the radix to use for the conversion; can be zero to //! auto-select the radix or between 2 and 16 to explicitly specify the radix. //! //! This function is very similar to the C library <tt>strtoul()</tt> function. //! It scans a string for the first token (that is, non-white space) and //! converts the value at that location in the string into an integer value. //! //!
eturn Returns the result of the conversion. // //***************************************************************************** unsigned long ustrtoul(const char *pcStr, const char **ppcStrRet, int iBase) { unsigned long ulRet, ulDigit, ulNeg, ulValid; const char *pcPtr;
// // Initially, the result is zero. // ulRet = 0; ulNeg = 0; ulValid = 0;
// // Skip past any leading white space. // pcPtr = pcStr; while((*pcPtr == ' ') || (*pcPtr == ' ')) { pcPtr++; }
// // Take a leading + or - from the value. // if(*pcPtr == '-') { ulNeg = 1; pcPtr++; } else if(*pcPtr == '+') { pcPtr++; }
// // See if the radix was not specified, or is 16, and the value starts with // "0x" or "0X" (to indicate a hex value). // if(((iBase == 0) || (iBase == 16)) && (*pcPtr == '0') && ((pcPtr[1] == 'x') || (pcPtr[1] == 'X'))) { // // Skip the leading "0x". // pcPtr += 2;
// // Set the radix to 16. // iBase = 16; }
// // See if the radix was not specified. // if(iBase == 0) { // // See if the value starts with "0". // if(*pcPtr == '0') { // // Values that start with "0" are assumed to be radix 8. // iBase = 8; } else { // // Otherwise, the values are assumed to be radix 10. // iBase = 10; } }
// // Loop while there are more valid digits to consume. // while(1) { // // See if this character is a number. // if((*pcPtr >= '0') && (*pcPtr <= '9')) { // // Convert the character to its integer equivalent. // ulDigit = *pcPtr++ - '0'; }
// // Otherwise, see if this character is an upper case letter. // else if((*pcPtr >= 'A') && (*pcPtr <= 'Z')) { // // Convert the character to its integer equivalent. // ulDigit = *pcPtr++ - 'A' + 10; }
// // Otherwise, see if this character is a lower case letter. // else if((*pcPtr >= 'a') && (*pcPtr <= 'z')) { // // Convert the character to its integer equivalent. // ulDigit = *pcPtr++ - 'a' + 10; }
// // Otherwise, this is not a valid character. // else { // // Stop converting this value. // break; }
// // See if this digit is valid for the chosen radix. // if(ulDigit >= iBase) { // // Since this was not a valid digit, move the pointer back to the // character that therefore should not have been consumed. // pcPtr--;
// // Stop converting this value. // break; }
// // Add this digit to the converted value. // ulRet *= iBase; ulRet += ulDigit;
// // Since a digit has been added, this is now a valid result. // ulValid = 1; }
// // Set the return string pointer to the first character not consumed. // if(ppcStrRet) { *ppcStrRet = ulValid ? pcPtr : pcStr; }
- //*****************************************************************************
复制代码//
//! Converts a string into its numeric equivalent.
//!
//! param pcStr is a pointer to the string containing the integer.
//! param ppcStrRet is a pointer that will be set to the first character past
//! the integer in the string.
//! param iBase is the radix to use for the conversion; can be zero to
//! auto-select the radix or between 2 and 16 to explicitly specify the radix.
//!
//! This function is very similar to the C library <tt>strtoul()</tt> function.
//! It scans a string for the first token (that is, non-white space) and
//! converts the value at that location in the string into an integer value.
//!
//! eturn Returns the result of the conversion.
//
//*****************************************************************************
unsigned long
ustrtoul(const char *pcStr, const char **ppcStrRet, int iBase)
{
unsigned long ulRet, ulDigit, ulNeg, ulValid;
const char *pcPtr;
//
// Check the arguments.
//
ASSERT(pcStr);
ASSERT((iBase == 0) || ((iBase > 1) && (iBase <= 16)));
//
// Initially, the result is zero.
//
ulRet = 0;
ulNeg = 0;
ulValid = 0;
//
// Skip past any leading white space.
//
pcPtr = pcStr;
while((*pcPtr == ' ') || (*pcPtr == ' '))
{
pcPtr++;
}
//
// Take a leading + or - from the value.
//
if(*pcPtr == '-')
{
ulNeg = 1;
pcPtr++;
}
else if(*pcPtr == '+')
{
pcPtr++;
}
//
// See if the radix was not specified, or is 16, and the value starts with
// "0x" or "0X" (to indicate a hex value).
//
if(((iBase == 0) || (iBase == 16)) && (*pcPtr == '0') &&
((pcPtr[1] == 'x') || (pcPtr[1] == 'X')))
{
//
// Skip the leading "0x".
//
pcPtr += 2;
//
// Set the radix to 16.
//
iBase = 16;
}
//
// See if the radix was not specified.
//
if(iBase == 0)
{
//
// See if the value starts with "0".
//
if(*pcPtr == '0')
{
//
// Values that start with "0" are assumed to be radix 8.
//
iBase = 8;
}
else
{
//
// Otherwise, the values are assumed to be radix 10.
//
iBase = 10;
}
}
//
// Loop while there are more valid digits to consume.
//
while(1)
{
//
// See if this character is a number.
//
if((*pcPtr >= '0') && (*pcPtr <= '9'))
{
//
// Convert the character to its integer equivalent.
//
ulDigit = *pcPtr++ - '0';
}
//
// Otherwise, see if this character is an upper case letter.
//
else if((*pcPtr >= 'A') && (*pcPtr <= 'Z'))
{
//
// Convert the character to its integer equivalent.
//
ulDigit = *pcPtr++ - 'A' + 10;
}
//
// Otherwise, see if this character is a lower case letter.
//
else if((*pcPtr >= 'a') && (*pcPtr <= 'z'))
{
//
// Convert the character to its integer equivalent.
//
ulDigit = *pcPtr++ - 'a' + 10;
}
//
// Otherwise, this is not a valid character.
//
else
{
//
// Stop converting this value.
//
break;
}
//
// See if this digit is valid for the chosen radix.
//
if(ulDigit >= iBase)
{
//
// Since this was not a valid digit, move the pointer back to the
// character that therefore should not have been consumed.
//
pcPtr--;
//
// Stop converting this value.
//
break;
}
//
// Add this digit to the converted value.
//
ulRet *= iBase;
ulRet += ulDigit;
//
// Since a digit has been added, this is now a valid result.
//
ulValid = 1;
}
//
// Set the return string pointer to the first character not consumed.
//
if(ppcStrRet)
{
*ppcStrRet = ulValid ? pcPtr : pcStr;
}
//
// Return the converted value.
//
return(ulNeg ? (0 - ulRet) : ulRet);
}
一周热门 更多>