bachra04
asked on
convert char array to number in c
Hi guys,
I have a char array with size 5 that contains a numerical values e.g.
char myValue [5]; can have two or three numerical characters and can be null terminated or not
myValue can be {'1','3','4', '', ''}
or
{ '5', '6', '\0', '' , ''}
or
{'1','6','', '', ''}
or
{'7','8','\0', '', ''}
etc ...
any help on how to do that in C I.E a method that takes a char array as an argument and return a numerical value:
uint16_t getMyNumValue ( char* array);
Thanks
I have a char array with size 5 that contains a numerical values e.g.
char myValue [5]; can have two or three numerical characters and can be null terminated or not
myValue can be {'1','3','4', '', ''}
or
{ '5', '6', '\0', '' , ''}
or
{'1','6','', '', ''}
or
{'7','8','\0', '', ''}
etc ...
any help on how to do that in C I.E a method that takes a char array as an argument and return a numerical value:
uint16_t getMyNumValue ( char* array);
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> sscanf (myvalue, "%d", &i);
>> That should scan the string starting at myvalue[0], store the integer in *i*, and quit scanning when a non-digit is encountered.
Haven't tried this to confirm, but if the array has digit chars all the way to the end of a virtual address space on a page, with no null terminator, then couldn't this crash as the sscanf goes off the page trying to find a non-digit?
>> That should scan the string starting at myvalue[0], store the integer in *i*, and quit scanning when a non-digit is encountered.
Haven't tried this to confirm, but if the array has digit chars all the way to the end of a virtual address space on a page, with no null terminator, then couldn't this crash as the sscanf goes off the page trying to find a non-digit?
in C there is strtol which should preferred over atoi since it returns the position where the parsing ended such that the caller can check whether the end was prior to end of string what is an indication that the 'number' contains some non-numeric garbage. atoi would return the number already parsed (0 if the string begins neither with a digit nor a sign) if the conversion stopped but no error.
when using c++ you may find std::istringstream::operat or<< convenient to convert a string to number:
// seed the stream with input string:
note, the streaming operator >> would return a reference to the stream itself on success. on fail it returns a void pointer of NULL which then can be tested with ! operator.
Sara
char * sznum = "123.456"; // not an integer
char * pend = NULL;
// 10 means decimal
int num = strtol(sznum, &pend, 10);
if (pend != NULL && *pend != '\0')
{
// that's an error since the string was NOT parsed
// until zero termination character was reached.
when using c++ you may find std::istringstream::operat
// seed the stream with input string:
std::string str = "123.456";
char * sznum = "9999";
// fill the input stream with str
std::istringstream iss1(str);
int num1 = 0;
if (!(iss1 >> num1))
{
//error
}
int num2 = 0;
std::istringstream iss2(sznum);
if (!(iss2 >> num2))
{
//error
}
note, the streaming operator >> would return a reference to the stream itself on success. on fail it returns a void pointer of NULL which then can be tested with ! operator.
Sara
http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi
To use atoi, your must append a null terminator to your string if it doesn't already have one. Otherwise, you may suffer serious errors such as segmentation faults as the atoi function continues testing for '\0', and possibly falls off your virtual address space.
If your string is, for example, "-123abc", then atoi will return -123.
If your string is, for example, "abc123", then atoi will return 0 since the first char in the string was not a digit, or a '+', or a '-'.