Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-03-2011, 07:15 AM
|
#1
|
Member
Registered: Feb 2010
Posts: 181
Rep:
|
bytes problem c++
Hi,i am receing a packet through sockets that contains this bytes:
The first 4 bytes should be ignored(5101) and the last 3(080,witch is the port) how can i extract the ip part from that(75126-94-51) ? Just to mention the ip it supossed to be 75.126.162.205. Thanks in advanced.
|
|
|
02-03-2011, 09:37 AM
|
#2
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541
|
Use the STL string, and it's substr() function.
I know this looks a wee bit amateurish, but consider the following:
Code:
#include <string>
#include <iostream>
int main()
{
const std::string data = "510175126-94-51080";
const std::string ip = data.substr(4, data.size() - 3 - 4);
std::cout << "ip = " << ip << std::endl;
}
|
|
|
02-03-2011, 09:46 AM
|
#3
|
Member
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205
Rep:
|
Quote:
Originally Posted by vbx_wx
Hi,i am receing a packet through sockets that contains this bytes:
The first 4 bytes should be ignored(5101) and the last 3(080,witch is the port) how can i extract the ip part from that(75126-94-51) ? Just to mention the ip it supossed to be 75.126.162.205. Thanks in advanced.
|
Hey,
may I ask why an IP address is encoded this way? When I would have to send an IP-adress across the network, I would not send it in ASCII, but in binary format, though 4 bytes per IP-address and 2 bytes per port. This is then much easier to parse/reconstruct and reduces the network load :-)
Andi
|
|
|
02-03-2011, 04:03 PM
|
#4
|
Member
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106
Rep:
|
Andi - I'd guess old school proprietary legacy format.
dwhiteney67 - But if you can, definitely change it to Andi's suggestion.
|
|
|
All times are GMT -5. The time now is 10:44 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|