Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

copying integers to a character array

Posted on 2003-03-08
13
Medium Priority
?
361 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:mjeet
  • 8
  • 5
13 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8096566
>> 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
 

Author Comment

by:mjeet
ID: 8096591
thanx that worked but is there any other work around if the length is greater than 10 ???
0
 

Author Comment

by:mjeet
ID: 8096600
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:Exceter
ID: 8096633
>> 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
 
LVL 8

Expert Comment

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

Exceter
0
 

Author Comment

by:mjeet
ID: 8096655
whoops.. iam sorry can't find the function itoa()
have included stdlib.h and stdio.h but still dosen't recognize the function
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8096658
>> 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
 

Author Comment

by:mjeet
ID: 8096665
yup tried that..
using g++
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8096667
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8096669
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
 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 8096680
>> 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
 

Author Comment

by:mjeet
ID: 8096947
thanx..
i guess i had asked the wrong question but it's my fault and your answer works..
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8100450
You are most welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question