ahmdmoadi
ahmdmoadi

Reputation: 1

what's the problem in this ascii char to hex c program?

As you can see, I am trying to make a program that receives getche() and then show it and its hex and finally putting all ´getche()´es into a string but that resulted in printing garbage chars!

Can you tell me what's the problem?

#include <conio.h>
#include <stdlib.h>

void main(void) {

    char allchars[64];

    int ctr = -1;

    char ch = 'a';

    while (ch != '\r') {
        printf("\nType a character:");
        ch=getche();
        printf("\nhex=%x\nch=%c",ch,ch);
        ctr++;
        allchars[ctr] += ch;
    }
    char terminate = 'a';
    printf("\nAll what you typed: %s",allchars);
    printf("\nPress any key to continue:");
    terminate=getche();
    if(getche()=='\n'){
        exit(0);
    }


}

output:

Type a character:t
hex=74
ch=t
Type a character:e
hex=65
ch=e
Type a character:s
hex=73
ch=s
Type a character:t
hex=74
ch=t
Type a character:
hex=d
ch=
All what you typed: ä{│t
Press any key to continue:
Process returned 13 (0xD)   execution time : 21.835 s
Press any key to continue.

Upvotes: 0

Views: 74

Answers (2)

Adrian Mole
Adrian Mole

Reputation: 51825

There are a couple of errors in your code, and a few other issues.

First, your line allchars[ctr] += ch; is adding the value of ch to the uninitialized existing value of the element of that array; you should use the simple assignment operator, instead: allchars[ctr] = ch;.

Second, all character strings in C that are to be used by functions like printf (with the %s format specifier) must be terminated with the NUL character (a zero value); so you need to add that after your while loop.

Another probable error (depends on your compiler/platform) is that you have not included the <stdio.h> header file - so there is no formal declaration of the printf function.

And, finally (I think), you really should be declaring your main function as returning an int (not void - although that is allowed, and was more common in days past).

Here is a version of your code with the above changes made (and commented):

#include <conio.h>
#include <stdlib.h>
#include <stdio.h> // MUST include this for the "printf" function

int main(void) // Should really use "int" return type
{
    char allchars[64];
    int ctr = -1;
    char ch = 'a';
    while (ch != '\r') {
        printf("\nType a character:");
        ch = getche();
        printf("\nhex=%x\nch=%c", ch, ch);
        ctr++;
        allchars[ctr] = ch; // Just assign! (Don't add to unitialized existing value.)
    }
    allchars[ctr + 1] = '\0'; // We MUST add a NUL-terminator to use the "%s" format

    char terminate = 'a';
    printf("\nAll what you typed: %s", allchars);
    printf("\nPress any key to continue:");
    terminate = getche();
    if (getche() == '\n') {
        exit(0);
    }
    return 0;
}

Feel free to ask for any further clarification and/or explanation.

Upvotes: 0

First of all 'allchars' is not null terminated (add a '\0' at the end of the char array).

What do you think this expression does?

allchars[ctr] += ch;

Since you do not initialize 'allchars' with any value beforehand, the value at the index 'ctr' is unknown (could be anything), but you add the character 'ch' to it (add equal).

As a result, you will have garbage at that index, at least not that what you expect it to be.

At last: what if you type more than 64 characters?

Upvotes: 2

Related Questions