LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-19-2011, 03:19 AM   #1
hayf
Member
 
Registered: Aug 2011
Distribution: Debian
Posts: 35

Rep: Reputation: Disabled
get 2 bytes of __be32 variable (ip address)


hi,

I have a __be32 variable that is an IP address.
I need to get the last 2 bytes of that IP and store it in an __be16 variable.
How this can be done?

Thanks,
hayf
 
Old 08-19-2011, 08:28 PM   #2
rustek
Member
 
Registered: Jan 2010
Location: Melbourne, IA, USA
Distribution: Ubuntu
Posts: 93

Rep: Reputation: 8
Hi, you really haven't provided enough information but I'll take a stab.

Using bash, if the variable __be32 = 192.168.12.36

__be16=$(echo $__be32 | cut -f4 -d".")


Then __be16 would be 36

This won't give you the last two bytes but give you the last field.
If __be32 was 192.168.12.136 then __be16 would be 136

I'm just guessing that that is what you really want?

Russ
 
Old 08-19-2011, 09:27 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Even easier:

Code:
__be16="${__be32##*.}"
parameter substitution

But that's again assuming bash or a bash-like shell. We really need to know what programming environment the OP talking about.
 
Old 08-19-2011, 11:41 PM   #4
rustek
Member
 
Registered: Jan 2010
Location: Melbourne, IA, USA
Distribution: Ubuntu
Posts: 93

Rep: Reputation: 8
David's answer gives the last field as well but is much better as it stays within the shell.

Old habits die hard :}

Russ

Last edited by rustek; 08-19-2011 at 11:44 PM.
 
Old 08-21-2011, 07:47 AM   #5
hayf
Member
 
Registered: Aug 2011
Distribution: Debian
Posts: 35

Original Poster
Rep: Reputation: Disabled
Hi, what i really want is to get the last 2 bytes.
e.g. if the __be32 ip address is 192.168.12.36 i need to get 12.36
i am programming using the C language.

Thanks
 
Old 08-21-2011, 11:39 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
For example,
Code:
__be32  addr;
__be16  low;

low = (__be16)( addr & (__be32)0xFFFFU );
    or, on a big-endian architecture
low = ((__be16 *)&addr)[1];
    or, on a little-endian architecture
low = ((__be16 *)&addr)[1];

Last edited by Nominal Animal; 08-22-2011 at 03:47 PM.
 
Old 08-22-2011, 09:06 AM   #7
hayf
Member
 
Registered: Aug 2011
Distribution: Debian
Posts: 35

Original Poster
Rep: Reputation: Disabled
OK that really works
but to get the last 2 bytes, it should be: low = ((__be16 *)&addr)[0];
one last question:
Any idea about how can print a __be16 using printk function? (i need it as 2 bytes ip address e.g. 12.36)
thanks in advance

Last edited by hayf; 01-16-2012 at 08:18 AM.
 
Old 08-22-2011, 03:52 PM   #8
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by hayf View Post
OK that is really works
but to get the last 2 bytes, it should be: low = ((__be16 *)&addr)[0];
Only on a little-endian architecture like x86.

(I had a thinko; I thought you meant the data was stored in big-endian order. It is not; only the interpretation of the value is big-endian, i.e. network byte order. Fixed my post.)

Quote:
Originally Posted by hayf View Post
Any idea about how can print a __be16 using printk function? (i need it as 2 bytes ip address e.g. 12.36)
Code:
/* Last two bytes of an IPv4 address */
printk("%d.%d", (addr >> 8U) & 255, addr & 255);

/* A full IPv4 address */
printk("%d.%d.%d.%d",
       (addr >> 24U) & 255,
       (addr >> 16U) & 255,
       (addr >>  8U) & 255,
        addr         & 255);
 
Old 08-23-2011, 03:10 AM   #9
hayf
Member
 
Registered: Aug 2011
Distribution: Debian
Posts: 35

Original Poster
Rep: Reputation: Disabled
Thank you for the answers ..

Last edited by hayf; 08-23-2011 at 03:45 AM.
 
  


Reply



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] How to pass sed address by shell variable? kaz2100 Programming 3 08-02-2011 08:21 PM
How many bytes are in a mac address? having trouble doing the math... hidegrl Linux - Networking 3 09-27-2010 06:27 AM
function returns address of local variable (void) rubadub Programming 11 10-28-2007 12:59 PM
LAN address in an environment variable somewhere? bcfriesen Linux - Networking 1 11-02-2005 02:40 PM
a question about obtaining address of variable ....... purpleburple Programming 3 07-06-2002 05:28 AM

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

All times are GMT -5. The time now is 03:25 AM.

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