copying integers to a character array

working on a dns client and i need to save the length of the domain before each of them..
so here's the code i wrote

char query[100];
int currentIndex=0;

tmp = strtok(hostname,".")
do{
length = strlen(tmp);
query[currentIndex++]= (char) length;
strcat (query,tmp)
currentIndex+=length;
}while (tmp = strtok(NULL,"."))

now i get the domain in there except for the lengths
for example ..
if i input www.google.com
i get _www_google_com  rather than 3www6google3com

the "_" are spaces..

any help would be appreciated
mjeetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ExceterCommented:
>> query[currentIndex++]= (char) length;

Try changing this to this,

query[currentIndex++] = length + 48;

Note: This assumes that length is less than 10(One decimal digit).

Exceter
0
mjeetAuthor Commented:
thanx that worked but is there any other work around if the length is greater than 10 ???
0
mjeetAuthor Commented:
is this written as a character or an integer ??
i had previously written a test code like
query[0]=6;
strcat(query,"google");
query[7]=3;
strcat(query,"com");

and that worked fine !!

but the above way does not pass the numbers the correct way.. the server responds with an error in the query
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ExceterCommented:
>> thanx that worked but is there any other work around if the length is greater than 10 ???

Of course, try this.

char number[6];
length = strlen(tmp);
strcat( query, _itoa( length, number, 10 ) );
strcat(query,tmp)

Exceter
0
ExceterCommented:
Note: If you are not using a Microsoft compiler you should remove the underscore that precedes the call to itoa().

Exceter
0
mjeetAuthor Commented:
whoops.. iam sorry can't find the function itoa()
have included stdlib.h and stdio.h but still dosen't recognize the function
0
ExceterCommented:
>> have included stdlib.h and stdio.h but still dosen't recognize the function

Have you tried,

strcat( query, itoa( length, number, 10 ) );

itoa() is found in stdlib.h. What compiler are you using?

Exceter
0
mjeetAuthor Commented:
yup tried that..
using g++
0
ExceterCommented:
Failing that, you could try this function as well. Although this is not exactly what it was intended to do but it still works.

int d,s;
length = strlen(tmp);
char* number = _fcvt( (double)x, 0, &d, &s  );
strcat( query, number );
strcat(query,tmp)

If you are not using a Microsoft compiler,

int d,s;
length = strlen(tmp);
char* number = fcvt( (double)x, 0, &d, &s  );
strcat( query, number );
strcat(query,tmp)

Note: fcvt() is also found in stdlib.h

Exceter
0
ExceterCommented:
If this function is not supported by your compiler either then I think the only solution would be to manually convert the number into a char array.

Exceter
0
ExceterCommented:
>> If this function is not supported by your compiler either then I think the only solution would be to manually convert the number into a char array.

SMACK(hitting self)! I take that back, this can be accomplish with ease using sprintf().

char number[6];
length = strlen(tmp);
sprintf( number, "%i", length );
strcat( query, number );
strcat(query,tmp)

Exceter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjeetAuthor Commented:
thanx..
i guess i had asked the wrong question but it's my fault and your answer works..
0
ExceterCommented:
You are most welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.