Convert char to int in C and C++

前端 未结 12 2141
梦毁少年i
梦毁少年i 2020-11-22 02:41

How do I convert a char to an int in C and C++?

12条回答
  •  天涯浪人
    2020-11-22 03:21

    I was having problems converting a char array like "7c7c7d7d7d7d7c7c7c7d7d7d7d7c7c7c7c7c7c7d7d7c7c7c7c7d7c7d7d7d7c7c2e2e2e" into its actual integer value that would be able to be represented by `7C' as one hexadecimal value. So, after cruising for help I created this, and thought it would be cool to share.

    This separates the char string into its right integers, and may be helpful to more people than just me ;)

    unsigned int* char2int(char *a, int len)
    {
        int i,u;
        unsigned int *val = malloc(len*sizeof(unsigned long));
    
        for(i=0,u=0;i

    Hope it helps!

提交回复
热议问题