How does one parse an integer to string(char* || char[])
in C? Is there an equivalent to the Integer.parseInt(String)
method from Java in C?
相关问题
- Multiple sockets for clients to connect to
- Correctly parse PDF paragraphs with Python
- how to split a list into a given number of sub-lis
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
It sounds like you have a string and want to convert it to an integer, judging by the mention of
parseInt
, though it's not quite clear from the question...To do this, use
strtol
. This function is marginally more complicated thanatoi
, but in return it provides a clearer indication of error conditions, because it can fill in a pointer (that the caller provides) with the address of the first character that got it confused. The caller can then examine the offending character and decide whether the string was valid or not.atoi
, by contrast, just returns 0 if it got lost, which isn't always helpful -- though if you're happy with this behaviour then you might as well use it.An example use of
strtol
follows. The check for error is very simple: if the first unrecognised character wasn't the'\x0'
that ends the string, then the string is considered not to contain a valid int.This function fills in *i with the integer and returns 1, if the string contained a valid integer. Otherwise, it returns 0.
You can also check out the
atoi()
function (ascii to integer) and it's relatives,atol
andatoll
, etc.Also, there are functions that do the reverse as well, namely
itoa()
and co.This is not an optimal solution. This is my solution given multiple restrictions, so if you had limited resources based on your course/instructor's guidelines, this may be a good fit for you.
Also note that this is a fraction of my own project implement, and I had to read in operands as well as digits, so I used getchars. Otherwise, if you only need integers and no other type of characters, I like using this:
The rules were no specific, "advanced" C concepts: no String variables, no structs, no pointers, no methods not covered, and the only include we were allowed was #include
So with no simple method calls like atoi() available or any String variables to use, I chose to just brute force it.
1: read chars in using getchar (fgets was banned). return 1 (exit status 1) if there is an invalid character. For your problem based of parseInt in Java 1 11 13 is valid but 1 11 1a is invalid, so for all values we have a valid "string" iff all chars are 0-9 ignoring whitespace.
2: convert the ASCII value of a char to its integer value (eg. 48 => 0)
3: use a variable val to store an int such that for each char in a "substring" there is a corresponding integer digit. i.e. "1234" => 1234 append this to an int array and set val to 0 for reuse.
The following code demonstrates this algorithm:
Again, this is the brute force method, but in cases like mine where you can't use the method concepts people use professionally or various C99 implements, this may be what you are looking for.