I have a char[] that contains a value such as "0x1800785" but the function I want to give the value to requires an int, how can I convert this to an int? I have searched around but cannot find an answer. Thanks.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Use xtoi ( stdlib.h ). The string has "0x" as first two indexes so trim val[0] and val[1] off by sending xtoi &val[2].
xtoi( &val[2] );
Have you tried
strtol()
?strtol - convert string to a long integer
Example:
In case the string representation of the number begins with a
0x
prefix, onemustshould use 0 as base:(It's as well possible to specify an explicit base such as 16, but I wouldn't recommend introducing redundancy.)
i have done a similar thing, think it might help u its actually working for me
here i have only taken a string of 8 characters. if u want u can add similar logic for 'a' to 'f' to give their equivalent hex values,i haven't done that cause i didn't needed it.
Assuming you mean it's a string, how about strtol?