rba1988
April 9th, 2009, 05:44 AM
Hello.
I'm trying to create a function that gets the source IP address given an ARP packet header. It has to return a string of the IP address. However, there are some compile errors when trying to add integers to the return string. I can print the results which works but I need it to return a string to make it portable and usable to other parts of the program.
The problem really is being able to concatinate each integer representation of each bit in the IP address. For example: string ip = 127.0.0.1. I have to add "27" + "." + "0" + "." + "0" + "." + "1".
Here's the function:
string getSourceIpAddress( arp_header *header )
{
string ret_value = ""; // Null at first
int firstBit = (int)header->ar_tip[0];
int secondBit = (int)header->ar_tip[1];
int thirdBit = (int)header->ar_tip[2];
int fourthBit = (int)header->ar_tip[3];
// This doesn't work:
ret_value = firstBit + "." + secondBit + "." + thirdBit + "." + fourthBit;
// This does: (debug)
cout << firstBit << "." << secondBit << "." << thirdBit << "." << fourthBit << endl;
return ret_value;
}
Here, the array ar_tip is a 4 element u_char array from the arp_header struct which I defined somewhere in the proram.
Hope you guys could help. Thanks.
I'm trying to create a function that gets the source IP address given an ARP packet header. It has to return a string of the IP address. However, there are some compile errors when trying to add integers to the return string. I can print the results which works but I need it to return a string to make it portable and usable to other parts of the program.
The problem really is being able to concatinate each integer representation of each bit in the IP address. For example: string ip = 127.0.0.1. I have to add "27" + "." + "0" + "." + "0" + "." + "1".
Here's the function:
string getSourceIpAddress( arp_header *header )
{
string ret_value = ""; // Null at first
int firstBit = (int)header->ar_tip[0];
int secondBit = (int)header->ar_tip[1];
int thirdBit = (int)header->ar_tip[2];
int fourthBit = (int)header->ar_tip[3];
// This doesn't work:
ret_value = firstBit + "." + secondBit + "." + thirdBit + "." + fourthBit;
// This does: (debug)
cout << firstBit << "." << secondBit << "." << thirdBit << "." << fourthBit << endl;
return ret_value;
}
Here, the array ar_tip is a 4 element u_char array from the arp_header struct which I defined somewhere in the proram.
Hope you guys could help. Thanks.