?
Solved

copying integers to a character array

Posted on 2003-03-08
13
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 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