Hello

I need to do it in ansi c

Can't seem to get it to work at the moment.

unsigned char *s

    scanf("%x", &s);

I then want to do if statements based on the amount of characters contained in the variable s. Whats the easiest way to get this? An example of whats contained in s is "4200FF" as its a prompt for the user to insert hex address.

printf("\nContents of hex are %lx\n", hex");

Shows correctly what I want to count

Thanks

Recommended Answers

All 6 Replies

You are asking two different things expecting to mash them together. Also your initial expectation is wrong....

%x is for numbers, not characters.
unsigned char *s is a pointer to character, but it doesn't actually point to one. It points somewhere but no one knows where.
4200FF would be 3 characters, not a single character.
Is 4200FF the contents of s or *s?

An example of whats contained in s is "4200FF" as its a prompt for the user to insert hex address.

Why does 4200FF define a "prompt for the user?" I think it's just junk because of an uninitialized pointer.

Hello - sorry i'm being ambiguous I too am a bit lost.

All I know is

printf("\nPlease enter the start address (hex) for data to be written: ");
            scanf("%x", &s);

Populates s.

printf("\nTest %lx\n", s);

Shows what I want to count (in my test I put 4200FF into the prompt and it was printed back to me.

In that case, yes I want to count it to 3 as they are hex pairs. How can I do this?

number = 420068;
            while(number) 
            {
            digits ++;
            number /= 10;
            }

            printf("\n Digits: %i\n", digits);

This works until a hex letter is used in the variable number as I have both number and digits as int. I get the output as 6 - which is correct

If I do number = s;

I get the output as 7 for some reason?

Thanks

Ohhhhhhhhh. In other words:

I want to enter a hex value. Then analyze that value and determine how many hex pairs that value contains.

What's the largest value a hex pair can hold?
Simply divide by that number in a loop, the same way you did for 10.

int numofcharacters=0;
printf("enter the character: ");
fgets(ch,sizeof(ch),stdin);
for(int i=0;ch[i]!='\0';i++)
{
numofcharacters++;
}
printf("%d",numofcharacters);

but if u try this code it will count even the number of spaces in character so use or u can try like

int numofcharacters=0,c;
printf("enter the character: ");
fgets(ch,sizeof(ch),stdin);
for(int i=0;ch[i]!='\0';i++)
{
if(ch[i]==32)
{
c++;\\counts the number of spaces

}
else
{
numofcharacters++;\\ counts the number of charatcer
}
printf("%d",numofcharacters);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.