【发布时间】: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