【问题标题】:Skip special characters while converting ASCII to HEX in C在 C 中将 ASCII 转换为 HEX 时跳过特殊字符
【发布时间】:2018-06-06 08:18:40
【问题描述】:

我需要帮助才能获得仅用于字母数字字符(不包括特殊字符)的 ascii 到十六进制数据输出。

Input String is: 86741-30011
Expected result is: 38363734313330303131 
Actual Result  is: 3836373431

在非字母数字字符后输出中断。它仅包含输出字符串,直到非字母数字字符。

代码:

int main(void){
    char word[12] = { '8', '6','7','4','1','-','3','0','0','1','1'};
    char outword[20];
    int i, len;
    len = strlen(word);
    printf("Input string: %s\n", word);
    //printf("Total length: %d\n", len);
    if(word[len-1]=='\n') word[--len] = '\0';
    for(i = 0; i<len; i++) {
        if (isalnum(word[i]) == 0) {
            printf("%c is not an alphanumeric character.\n", word[i]);
        } else {
            sprintf(outword+i*2 , "%02X", word[i]);
        }
    }
    printf("Hex output:%s\n", outword); return 0;
}

谁能帮我获得预期的输出?

提前致谢。

【问题讨论】:

  • 代码:int main(void){ char word[12] = { '8', '6','7','4','1','-','3' ,'0','0','1','1'}; char outword[20];诠释 i, len; len = strlen(word); printf("输入字符串:%s\n", word); //printf("总长度:%d\n", len); if(word[len-1]=='\n') word[--len] = '\0'; for(i = 0; i
  • 您似乎希望在阅读非字母数字字符时打破for loop。看看tutorialspoint.com/cprogramming/c_break_statement.htm
  • 不行,我需要跳过非字母数字字符,继续进行 asciitohex 数据转换,直到字符串结束。
  • 哦,是的,对不起,我颠倒了你的两行。 printf 'not and alphanumeric' 永远不会出现?
  • 它打印输入字符'-'(非字母数字)。但问题是输出无法在非字母数字字符之后获取字符。

标签: c hex ascii data-conversion


【解决方案1】:

使用不同的变量进行循环旋转并将数据添加到数组中。

#include <stdio.h>

int main(void){
    char word[12] = { '8', '6','7','4','1','-','3','0','0','1','1'};
    char outword[20];
    int i, j, len;
    len = strlen(word);
    printf("Input string: %s\n", word);
    //printf("Total length: %d\n", len);
    if(word[len-1]=='\n') word[--len] = '\0';
    for(i = 0,j=0; i<len; i++) {
        if (isalnum(word[i]) == 0) {
            printf("%c is not an alphanumeric character.\n", word[i]);
        } else {
            sprintf(outword+j*2 , "%02X", word[i]);
            j=j+1;
        }
    }
    printf("Hex output:%s\n", outword); return 0;
}

此代码将为您提供预期的结果 38363734313330303131。

【讨论】:

    【解决方案2】:

    你需要分别计算输入和输出位置。

    for(i = 0; i<len; i++) 
    {
        if (isalnum(word[i]) == 0) {
            printf("%c is not an alphanumeric character.\n", word[i]);
        } else {
            sprintf(outword+i*2 , "%02X", word[i]);
        }
    }
    

    如果您的条件为真并且您打印文本,则计数器 i 会递增。这不仅用于获取下一个字符,还用于定义输出数组中的位置。这意味着在解析输入时不会触及 out-array 中的 2 个字节。

    如果您不小心有 0 个字节,则您的字符串将在此处终止。

    这将导致以下布局: "3836373431\0\03330303131" 打印为"3836373431"

    您可以为输出添加另一个变量,并且仅在您真正转换为十六进制时才增加。

    int outpos;
    for(i = 0, outpos = 0; i<len; i++)
    {
        if (isalnum(word[i]) == 0) {
            printf("%c is not an alphanumeric character.\n", word[i]);
        } else {
            sprintf(outword+outpos*2 , "%02X", word[i]);
            outpos++;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多