LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-03-2011, 07:15 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
bytes problem c++


Hi,i am receing a packet through sockets that contains this bytes:
Code:
510175126-94-51080
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.
 
Old 02-03-2011, 09:37 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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;
}
 
Old 02-03-2011, 09:46 AM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by vbx_wx View Post
Hi,i am receing a packet through sockets that contains this bytes:
Code:
510175126-94-51080
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
 
Old 02-03-2011, 04:03 PM   #4
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
Andi - I'd guess old school proprietary legacy format.

dwhiteney67 - But if you can, definitely change it to Andi's suggestion.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] archive problem (multiple types of compression, i think), some files are 0 bytes djmarian Linux - Software 2 12-13-2009 01:52 PM
Problem with serial port, it lost some bytes !!!!!! andaya Linux - Newbie 3 10-23-2008 02:45 AM
PROBLEM: all bytes of extracted payload from a dump file appears to be zero ahm_irf Programming 1 11-07-2007 10:29 AM
bytes in/out ilnli Programming 4 07-26-2007 02:44 AM
TX bytes vs. httpd bytes ovrload Linux - Networking 3 10-12-2005 04:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration