Functions for coversion strings to intergers


Konstantin Aladyshev
 

Are there any library functions that can convert a string with a
decimal number to a number, and tell if the conversion was successful?

I've found StrDecimalToUintnS and StrDecimalToUint64S functions in a
https://github.com/tianocore/edk2/blob/master/MdePkg/Library/BaseLib/SafeString.c
But it looks like they always return `RETURN_SUCCESS`, no matter if
the passed string contains a number. In this case the function simply
fills the number value with a 0.

Also why aren't there any functions for conversion of signed numbers?
Something like StrDecimalToIntnS/StrDecimalToInt64S ?

Best regards,
Konstantin Aladyshev


Andrew Fish
 

On Jul 21, 2021, at 10:01 AM, Konstantin Aladyshev <aladyshev22@...> wrote:

Are there any library functions that can convert a string with a
decimal number to a number, and tell if the conversion was successful?
What is your definition of successful?

I've found StrDecimalToUintnS and StrDecimalToUint64S functions in a
https://github.com/tianocore/edk2/blob/master/MdePkg/Library/BaseLib/SafeString.c
But it looks like they always return `RETURN_SUCCESS`, no matter if
the passed string contains a number. In this case the function simply
fills the number value with a 0.

Also why aren't there any functions for conversion of signed numbers?
Something like StrDecimalToIntnS/StrDecimalToInt64S ?
Most of the library functions evolved from code duplication in the firmware. Thus we only pick functions that commonly got used.

Thanks,

Andrew Fish

Best regards,
Konstantin Aladyshev



Konstantin Aladyshev
 

What I meant is there is no difference in the result between the "0"
string, and "hhh" string. Both would get translated to the number
zero.
And something like "16dfgdf" would get translated to 16.

But I think I've found a C library function, that is the base for
these StrDecimalToUintnS/StrDecimalToUint64S functions. It is
`strtol`.

And in the case of strtol it is a caller responsibility to check for
all the possible error cases via comparing first and second argument:
https://stackoverflow.com/questions/26080829/detecting-strtol-failure/26083517

```
CHAR16* End;
UINTN MyVar;
RETURN_STATUS Status = StrDecimalToUintnS(Argv[Index], &End, &MyVar);
if ((Status != RETURN_SUCCESS) || (Argv[Index] == End) || (*End != 0))
Print(L"Error! incorrect number\n");
}
```
So I think that clears my question.
Sorry to bother you.

Best regards,
Konstantin Aladyshev

On Wed, Jul 21, 2021 at 9:23 PM Andrew Fish <afish@...> wrote:



On Jul 21, 2021, at 10:01 AM, Konstantin Aladyshev <aladyshev22@...> wrote:

Are there any library functions that can convert a string with a
decimal number to a number, and tell if the conversion was successful?


What is your definition of successful?

I've found StrDecimalToUintnS and StrDecimalToUint64S functions in a
https://github.com/tianocore/edk2/blob/master/MdePkg/Library/BaseLib/SafeString.c
But it looks like they always return `RETURN_SUCCESS`, no matter if
the passed string contains a number. In this case the function simply
fills the number value with a 0.

Also why aren't there any functions for conversion of signed numbers?
Something like StrDecimalToIntnS/StrDecimalToInt64S ?


Most of the library functions evolved from code duplication in the firmware. Thus we only pick functions that commonly got used.

Thanks,

Andrew Fish

Best regards,
Konstantin Aladyshev