0

I need help to get the ascii to hex data output only for alphanumeric characters(excluding special characters).

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.

Ashwini
  • 19
  • 4
  • 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 – Ashwini Jun 06 '18 at 08:19
  • It seems you whish to break the `for loop` when reading a non alphanumeric character. Have a look at https://www.tutorialspoint.com/cprogramming/c_break_statement.htm – nicolallias Jun 06 '18 at 08:37
  • No, I need to skip nonalphanumeric character and continue to asciitohex data conversion until end of the string. – Ashwini Jun 06 '18 at 08:42
  • Oh yes, excuse me I inverted your two lines. The printf 'not and alphanumeric' does never occur? – nicolallias Jun 06 '18 at 08:48
  • It prints for input char '-'(non alphanumeric). But issue is output fails to get characters after non alphanumeric characters. – Ashwini Jun 06 '18 at 08:54
  • Playing around with your code, http://codepad.org/bgBv1dOJ I am sure the loop goes further the caret character, but have no idea why sprintf stops catenating. – nicolallias Jun 06 '18 at 09:05
  • your `outword` array only has room for 20 characters, and you're placing exactly that amount in it (10 digits times 2 hex characters each). That leaves no room for the null terminator `'\0'`. – Sander De Dycker Jun 06 '18 at 09:20
  • What do you think `if(word[len-1]=='\n') word[--len] = '\0';` does? That is a 'partial' check for use after a *line-oriented input* function such as `fgets` or POSIX `getline` -- it has absolutely nothing to do with `word`. The only possible relevance would be if you initialized `word` as `char word[13] = { '8', '6','7','4','1','-','3','0','0','1','1','\n'};`. Don't guess at using code found elsewhere -- know exactly what each line does and why you are using it. `char word[] = "86741-30011";` would be a better way to initialize your array to contain the nul-terminated string. – David C. Rankin Jun 06 '18 at 09:32
  • If you got an answer that solved your problem, you might accept it. – Gerhardh Jun 19 '18 at 07:59

2 Answers2

2

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.

1

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. 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.

If by accident you have 0 bytes there, your string is terminated here.

This would lead to the following layout: "3836373431\0\03330303131" which is printed as "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++;
    }
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39