Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

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
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

For a char array consisting of digits, + or -, in a normal manner, then I have used atoi.
http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.

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 '-'.
>> 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?
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.

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.

Open in new window


when using c++ you may find std::istringstream::operator<< convenient to convert a string to number:

// 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
}

Open in new window


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