简体   繁体   English

将字符串转换为char c

[英]convert string to char c

I am working on a project where I can to convert a api digit to a char. 我正在一个项目中,我可以将api数字转换为char。 I have used an array of string pointers to get the conversion. 我使用了一个字符串指针数组来获取转换。 However, I want to return just a single ch, as my api that I am using will only accept a char. 但是,我只想返回一个ch,因为我正在使用的api仅接受一个char。 So ap_five will return "5". 因此ap_five将返回“ 5”。 But I want to five to be a single char '5'. 但是我想让五个成为单个字符'5'。

I thought maybe I could cast to a char in the return. 我以为也许可以在返回中转换为char。 However, I just get rubbish. 但是,我只是垃圾。

I am sure there is other ways to solve this, how I am just wondering how can I solve with my present program. 我确信还有其他方法可以解决此问题,我只是想知道如何用当前程序解决。

char *digits_conversion[][2]=
{
    {"ap_zero", "0"},
    {"ap_one", "1"},
    {"ap_two", "2"},
    {"ap_three", "3"},
    {"ap_four", "4"},
    {"ap_five", "5"},
    {"ap_six", "6"},
    {"ap_seven", "7"},
    {"ap_eight", "8"},
    {"ap_nine", "9"},
    {"ap_star", "*"},
    {"ap_hash", "#"},
    {NULL, NULL}
};

char convert_to_char(const char *digit)
{
    int i = 0;
    for(i = 0; *digits_conversion[i][1]; i++)
    {
        if(strcmp(digits_conversion[i][0], digit) == 0)
        {
            return (char) digits_conversion[i][1];
        }
    }

    return '\0';
}

int main(void)
{
    char ch;

    ch = convert_to_char("ap_five");

    printf("Converted digit: %c\n", ch);

    return 0;
}

The simplest fix to your existing code is just to change: 现有代码最简单的解决方法就是更改:

return (char) digits_conversion[i][1];

into 进入

return digits_conversion[i][1][0];

However, you might find that changing digits_conversion into an array of structures will give you code that is easier to understand and maintain. 但是,您可能会发现将digits_conversion更改为结构数组将使您的代码更易于理解和维护。 For example: 例如:

struct digit_mapping {
    char *api_name;
    char digit;
};
struct digit_mapping conversion_table[] = {
    { "ap_zero", '0' },
    {"ap_one", '1'},
    {"ap_two", '2'},
    {"ap_three", '3'},
    {"ap_four", '4'},
    {"ap_five", '5'},
    {"ap_six", '6'},
    {"ap_seven", '7'},
    {"ap_eight", '8'},
    {"ap_nine", '9'},
    {"ap_star", '*'},
    {"ap_hash", '#'},
    {NULL, '\0'}
};

char convert_to_char(const char *digit)
{
    int i = 0;
    for(i = 0; conversion_table[i].digit; i++)
    {
            if(strcmp(conversion_table[i].api_name, digit) == 0)
            {
                    return conversion_table[i].digit;
            }
    }

    return '\0';
}

int main(void)
{
    char ch;

    ch = convert_to_char("ap_five");

    printf("Converted digit: %c\n", ch);

    return 0;
}
digits_conversion[i][1]

is a C-style string. 是C样式的字符串。 Use: 采用:

digits_conversion[ i ][ 1 ][ 0 ] // if you want the character 

char convert_to_char(const char *digit)
{
    int i = 0;
    for(i = 0; *digits_conversion[i][1]; i++)
    {
            if(strcmp(digits_conversion[i][0], digit) == 0)
            {
                    return digits_conversion[i][1][0];
            }
    }

    return '\0';
}
return digits_conversion[i][1][0];

The termination of your 'for' loop looks incorrect: 您的“ for”循环的终止看起来不正确:

//...
    {"NULL", "NULL"}
//...
//...
for(i = 0; *digits_conversion[i][1]; i++)
//...

The "NULL" you've got at the end of your array is literally, the string "NULL". 数组末尾的“ NULL”字面上是字符串“ NULL”。 If you pass a string that doesn't match any of the "ap_zero" through "ap_hash" your program will likely crash as the loop starts dereferencing garbage beyond the end of your array. 如果您通过“ ap_hash”传递的字符串与“ ap_zero”中的任何一个都不匹配,则该程序可能会崩溃,因为循环开始在数组末尾开始引用垃圾。

Eric fixed this in his example, but I thought I'd point this out specifically. 埃里克(Eric)在他的示例中解决了这个问题,但我想我要特别指出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM