Hello,

I have a function that looks like this:

CMyMessage(char type, char *name, char namelen, char *text, char textlen){ 
    ... ?!         
}

...

void main()
{
   char name[50]="JOHN";
   char text[200]="testing123";

   CMyMessage(1, name, strlen(name), text, strlen(text));

}

The ranges are:

Type = 0/1;
namelen = 0..50;
textlen = 0..200

How exactly do I turn these chars into numerical values? If it involves using ATOI/ITOA, can you please a very short example?

Thank you.

There are a number of functions in the atoi() family, some of which can be used for floating point values. These functions are strtof(), strtod(), and strtold(). Don't use atoi() or atof() functions - it is better to use strtol() and strtof() type functions instead as they give you better support when parsing input. If you are running Unix/Linux systems, see the man pages. If Windows, see the MSDN library documentation, or look up online.

Well thanks, but I need to convert to values ranging from 0 to 255. I also asked for an example, as I am not proficient with pointers.

const char* ptr = "255";
unsigned int i = (unsigned int)strtoul(ptr, 0, 10);

This works, thank you.

CMyMessage(char type, char *name, char namelen, char *text, char textlen){ 
    //access type      
}
 

void main()
{ 

   const char* ptr = "255";
   unsigned int i = (unsigned int)strtoul(ptr, 0, 10);

   char name[50]="JOHN";
   char text[200]="testing123";
 
   CMyMessage(ptr, name, strlen(name), text, strlen(text));
  
}

But how do I pass it to this function? the type param is a char, not char *.

Also, the point of this was to save memory. I've done nothing if I pass it as a char and then create an integer to store the value.

commented: If you edited the post once, you could edit it to remove the color tag in the CODE block, too. -3

So pass a char* instead of a char?

Are you going to keep asking obvious questions until we write your whole program for you?

How exactly is this an obvious question?

Who asked you to write my program for me?

This is a specification and it must be met. The function head has a char, so I must pass a char, not a char *.

How exactly is this an obvious question?

Well-

How exactly do I turn these chars into numerical values? If it involves using ATOI/ITOA, can you please a very short example?

You answer your own question, and refused to look up how atoi works, asking us to teach you the simplest function.

Well thanks, but I need to convert to values ranging from 0 to 255. I also asked for an example, as I am not proficient with pointers.

Can't bother to try something. And can't bother to look up an example. You require us to hand-feed you.

But how do I pass it to this function? the type param is a char, not char *.

Another no-brainer. Don't define the value as a pointer.

If these aren't obvious questions, what is?

------------------

Who asked you to write my program for me?

Not directly, but every question you've asked you could have solved easily by looking it up. But you want us to supply an example (IOW, write the code)

How exactly is this an obvious question?

Who asked you to write my program for me?

This is a specification and it must be met. The function head has a char, so I must pass a char, not a char *.

Well, atoi() and the rest do NOT take char types. They take const char* types. You can pass an array of chars, but that is just the same as a pointer.

Anyway, I have been doing this for 30+ years. Don't try to teach your grandmother how to suck eggs!

Thank you for your trouble, rubberman.

The winner is:

unsigned char x=255;
   
   printf("value: %d",x);

This prints 255.

Yes, well your example is treating a char as an integer, which it is (a really short one). That really isn't what the original question was about, as far as I could see. It seemed to be about converting a character representation of a number (a string) to a real number, hence the comments about using atoi(), stroul(), etc. Next time, be more precise.

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.