简体   繁体   English

在C中将ASCII转换为十六进制时,跳过特殊字符

[英]Skip special characters while converting ASCII to HEX in C

I need help to get the ascii to hex data output only for alphanumeric characters(excluding special characters). 我需要帮助以仅将字母数字字符(不包括特殊字符)的ascii转换为十六进制数据输出。

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

Output breaks after non alphanumeric characters. 输出在非字母数字字符后中断。 It contains output string only until non alphanumeric characters. 它仅包含输出字符串,直到非字母数字字符为止。

Code: 码:

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;
}

Can anyone help me to get expected output? 谁能帮助我获得预期的输出?

Thanks in advance. 提前致谢。

Use different variables for loop rotation and adding data into array. 使用不同的变量进行循环旋转并将数据添加到数组中。

#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;
}

This code will give you the expected result 38363734313330303131. 该代码将为您提供预期的结果38363734313330303131。

You need to count input and output position separately. 您需要分别计算输入和输出位置。

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]);
    }
}

If your condition is true and you print the text, the counter i is incremented. 如果您的条件为真并且您打印文本,则计数器i会递增。 This is not only used to get to the next in-character, but also do define the position in output-array. 这不仅用于到达下一个字符,而且还定义了输出数组中的位置。 This means the 2 bytes in the out-array are not touched while you parse the input. 这意味着在解析输入时不会触及数组外的2个字节。

If by accident you have 0 bytes there, your string is terminated here. 如果偶然在此处有0个字节,则字符串在此处终止。

This would lead to the following layout: "3836373431\\0\\03330303131" which is printed as "3836373431" . 这将导致以下布局: "3836373431\\0\\03330303131"打印为"3836373431"

You might add another variable for output and only increment when you really convert to hex. 您可以为输出添加另一个变量,并且仅在真正转换为十六进制时才递增。

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++;
    }
}

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

相关问题 用于将4个十六进制字符转换为C中的整数的小片段 - Tiny snippet for converting 4 hex characters to an integer in C 在不使用库函数的情况下将 C 中的 ASCII 数字转换为十六进制数字 - Converting an ASCII number to a Hex number in C without using library functions 将十六进制转换为Ascii内存访问 - Converting Hex To Ascii Memory Access 从 c 中的文件读取和打印特殊 ascii 字符 - Read and print special ascii characters from file in c C 字符串中的特殊字符和不可打印的 ASCII 的奇怪之处 - Strangeness with special characters in C-strings and unprintable ASCII 如何让 gdb 以十六进制而不是八进制打印字符串的不可打印字符,同时以 ascii 形式保留 ascii 字符? - How can I make gdb print unprintable characters of a string in hex instead of octal while preserving the ascii characters in ascii form? 在一行C上将字符串的各个字符转换为ASCII值 - Converting individual characters of a string into ASCII values on one line, C 需要转换数据(字节值转换为十六进制,十六进制字符作为ASCII字符) - Need to convert data (byte value into hex and hex characters as ASCII characters) 将十六进制字符数组转换为 ascii 字符数组 - Converting hex char array to ascii char array 将 ASCII 字符数组转换为十六进制字符数组 - converting ASCII char array to hex char array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM