473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

14 byte array (MAC address) to 48 bits (6 bytes)...

First,

Sorry for the cross-post :p OK, in Linux I can obtain the MAC address
using socket(), ioctl() etc. and a few data structures. No problem.
However, the resulting MAC is stored in a 14 byte char array
(sockaddr.sa_data). However, a MAC address is 48-bits! The MAC (as in
the char array) is obviously the printable form of:

00efc8346b72

or whatever. So that last '7' and '2' is in fact meant to be the 1 byte
72 right? How is this array shifted down to a 6 byte array or even
better, what I really want is the least significant 32-bits to stick in
an unsigned int!

Any help appreciated!

Jim

Jul 28 '06 #1
9 10932
James Vanns wrote:
First,

Sorry for the cross-post :p OK, in Linux I can obtain the MAC address
using socket(), ioctl() etc. and a few data structures. No problem.
However, the resulting MAC is stored in a 14 byte char array
(sockaddr.sa_data). However, a MAC address is 48-bits! The MAC (as in
the char array) is obviously the printable form of:

00efc8346b72

or whatever. So that last '7' and '2' is in fact meant to be the 1 byte
72 right? How is this array shifted down to a 6 byte array or even
better, what I really want is the least significant 32-bits to stick in
an unsigned int!

Any help appreciated!
I'd love to help but I have no idea what you're talking about !
Could you express in abstract terms what you're trying to achieve
without mentioning MAC's , Linux , ioctl etc. ?

Jul 28 '06 #2

James Vanns wrote:
First,

Sorry for the cross-post :p OK, in Linux I can obtain the MAC address
using socket(), ioctl() etc. and a few data structures. No problem.
However, the resulting MAC is stored in a 14 byte char array
(sockaddr.sa_data). However, a MAC address is 48-bits! The MAC (as in
the char array) is obviously the printable form of:

00efc8346b72

or whatever. So that last '7' and '2' is in fact meant to be the 1 byte
72 right? How is this array shifted down to a 6 byte array or even
better, what I really want is the least significant 32-bits to stick in
an unsigned int!

Any help appreciated!

Jim
Hi,

I think you probably want code like this, probably with some added
error
checking...

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *pMac = "00efc8346b72";
unsigned long iMac = strtoul(&pMac[4], NULL, 16);

printf("pMac '%s' -iMac '%lu' (=%lx)\n",
pMac, iMac, iMac);

return 0;
}

-David

Jul 28 '06 #3
OK, I've come up with this code. I know it's C++ (sorry wrong news
group) but I *was* going to use just C. However, do you think I'm on
the right track?

static const uint32_t mac2int (const string &p_nic, const string
&p_mac) {
uint32_t imac = 0;
string mac (p_mac); // 00:02:B3:14:03:D7
mac.erase (remove (mac.begin (), mac.end (), ':'), mac.end ()); //
0002B31403D7

for (uint i = 0 ; i < sizeof (uint) * 2 ; i += 2) {
char b[3];
b[2] = '\0';
b[1] = *(mac.end () - (i + 1)); // 7
b[0] = *(mac.end () - (i + 2)); // D
imac = (imac << 8) | strtoul (b, NULL, 16); // The important bit!
}

return imac;
}

Ta,

Jim

David Resnick wrote:
James Vanns wrote:
First,

Sorry for the cross-post :p OK, in Linux I can obtain the MAC address
using socket(), ioctl() etc. and a few data structures. No problem.
However, the resulting MAC is stored in a 14 byte char array
(sockaddr.sa_data). However, a MAC address is 48-bits! The MAC (as in
the char array) is obviously the printable form of:

00efc8346b72

or whatever. So that last '7' and '2' is in fact meant to be the 1 byte
72 right? How is this array shifted down to a 6 byte array or even
better, what I really want is the least significant 32-bits to stick in
an unsigned int!

Any help appreciated!

Jim

Hi,

I think you probably want code like this, probably with some added
error
checking...

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *pMac = "00efc8346b72";
unsigned long iMac = strtoul(&pMac[4], NULL, 16);

printf("pMac '%s' -iMac '%lu' (=%lx)\n",
pMac, iMac, iMac);

return 0;
}

-David
Jul 28 '06 #4
In article <11**********************@75g2000cwc.googlegroups. com>,
James Vanns <ja*********@gmail.comwrote:
>OK, I've come up with this code. I know it's C++ (sorry wrong news
group) but I *was* going to use just C. However, do you think I'm on
the right track?
>static const uint32_t mac2int (const string &p_nic, const string
&p_mac) {
uint32_t imac = 0;
string mac (p_mac); // 00:02:B3:14:03:D7
mac.erase (remove (mac.begin (), mac.end (), ':'), mac.end ()); //
0002B31403D7
That code violates the initialy stated condition that the MAC
was stored in a 14 byte array and therefore is not suitable
for the original purpose.

If you are going to work with the human-readable textual form of MACs,
then you need to be concerned with the possibility that leading
zeroes have been supressed, such as 0:2:B3:14:3:D7 .
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jul 28 '06 #5
James Vanns wrote:
OK, I've come up with this code. I know it's C++ (sorry wrong news
group) but I *was* going to use just C. However, do you think I'm on
the right track?

static const uint32_t mac2int (const string &p_nic, const string
&p_mac) {
uint32_t imac = 0;
string mac (p_mac); // 00:02:B3:14:03:D7
mac.erase (remove (mac.begin (), mac.end (), ':'), mac.end ()); //
0002B31403D7

for (uint i = 0 ; i < sizeof (uint) * 2 ; i += 2) {
char b[3];
b[2] = '\0';
b[1] = *(mac.end () - (i + 1)); // 7
b[0] = *(mac.end () - (i + 2)); // D
imac = (imac << 8) | strtoul (b, NULL, 16); // The important bit!
}

return imac;
}

Ta,

Jim
People frown on C++ code (off topic) and on top-posting (bad practice)
in comp.lang.c, just FYI. Anyway, I won't comment specifically on C++
aspects of the code in comp.lang.c (followups set to comp.lang.c++ and
comp.unix.programmer).

It seems to me that you might as well convert the whole thing (starting
at the 5th character) at once after removing the ':' characters rather
than do the convert two characters/shift thing unless you somehow think
the format of a MAC address will change.

-David

Jul 28 '06 #6
<sp****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
James Vanns wrote:
>Sorry for the cross-post :p OK, in Linux I can obtain the MAC
address using socket(), ioctl() etc. and a few data structures.
No problem. However, the resulting MAC is stored in a 14 byte
char array (sockaddr.sa_data). However, a MAC address is
48-bits! The MAC (as in the char array) is obviously the printable
form of:

00efc8346b72

or whatever. So that last '7' and '2' is in fact meant to be the 1
byte
72 right? How is this array shifted down to a 6 byte array or even
better, what I really want is the least significant 32-bits to
stick in
an unsigned int!

Any help appreciated!

I'd love to help but I have no idea what you're talking about !
Could you express in abstract terms what you're trying to achieve
without mentioning MAC's , Linux , ioctl etc. ?
He wants to turn this:

char printablemac[14] = "00efc8346b72";

into this:

unsigned char binarymac[6] = { 0x00, 0xef, 0xc8, 0x34, 0x6b, 0x72 };

or this:

unsigned long longmac = 0xc8346b72;

or this:

unsigned long long longlongmac = 0x00efc8346b72;

The answers for the first and the latter two are rather different, but
David Resnick provided a reasonable answer to the middle option.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Jul 28 '06 #7
I thought that someone may have noticed this. The array is defined in
the linux socket.h header file as being 14 bytes in size. However, you
only need 12 for a hex rep of a 48-bit number which is what the MAC
address is. I'm only working with what I've got ;) Basically I built
the std::string from the 14 byte char array taking only bytes 12 -0.
This seems to work fine on the ~1000 linux machines I've tried. I'm not
bothered with portability at the moment. The colons are never use at
this level of the api.

Have anything else to add?

Jim

Walter Roberson wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
James Vanns <ja*********@gmail.comwrote:
OK, I've come up with this code. I know it's C++ (sorry wrong news
group) but I *was* going to use just C. However, do you think I'm on
the right track?
static const uint32_t mac2int (const string &p_nic, const string
&p_mac) {
uint32_t imac = 0;
string mac (p_mac); // 00:02:B3:14:03:D7
mac.erase (remove (mac.begin (), mac.end (), ':'), mac.end ()); //
0002B31403D7

That code violates the initialy stated condition that the MAC
was stored in a 14 byte array and therefore is not suitable
for the original purpose.

If you are going to work with the human-readable textual form of MACs,
then you need to be concerned with the possibility that leading
zeroes have been supressed, such as 0:2:B3:14:3:D7 .
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jul 28 '06 #8
I think Stephen and David have hit the nail on the head. I'm close to
getting what I want so I'm sure I'll crack it now.

Ta,

Jim

James Vanns wrote:
I thought that someone may have noticed this. The array is defined in
the linux socket.h header file as being 14 bytes in size. However, you
only need 12 for a hex rep of a 48-bit number which is what the MAC
address is. I'm only working with what I've got ;) Basically I built
the std::string from the 14 byte char array taking only bytes 12 -0.
This seems to work fine on the ~1000 linux machines I've tried. I'm not
bothered with portability at the moment. The colons are never use at
this level of the api.

Have anything else to add?

Jim

Walter Roberson wrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
James Vanns <ja*********@gmail.comwrote:
>OK, I've come up with this code. I know it's C++ (sorry wrong news
>group) but I *was* going to use just C. However, do you think I'm on
>the right track?
>static const uint32_t mac2int (const string &p_nic, const string
>&p_mac) {
uint32_t imac = 0;
string mac (p_mac); // 00:02:B3:14:03:D7
mac.erase (remove (mac.begin (), mac.end (), ':'), mac.end ()); //
>0002B31403D7
That code violates the initialy stated condition that the MAC
was stored in a 14 byte array and therefore is not suitable
for the original purpose.

If you are going to work with the human-readable textual form of MACs,
then you need to be concerned with the possibility that leading
zeroes have been supressed, such as 0:2:B3:14:3:D7 .
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jul 28 '06 #9
Stephen Sprunk wrote:
<sp****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
James Vanns wrote:
Sorry for the cross-post :p OK, in Linux I can obtain the MAC
address using socket(), ioctl() etc. and a few data structures.
No problem. However, the resulting MAC is stored in a 14 byte
char array (sockaddr.sa_data). However, a MAC address is
48-bits! The MAC (as in the char array) is obviously the printable
form of:

00efc8346b72

or whatever. So that last '7' and '2' is in fact meant to be the 1
byte
72 right? How is this array shifted down to a 6 byte array or even
better, what I really want is the least significant 32-bits to
stick in
an unsigned int!

Any help appreciated!
I'd love to help but I have no idea what you're talking about !
Could you express in abstract terms what you're trying to achieve
without mentioning MAC's , Linux , ioctl etc. ?

He wants to turn this:

char printablemac[14] = "00efc8346b72";

into this:

unsigned char binarymac[6] = { 0x00, 0xef, 0xc8, 0x34, 0x6b, 0x72 };

or this:

unsigned long longmac = 0xc8346b72;

or this:

unsigned long long longlongmac = 0x00efc8346b72;

The answers for the first and the latter two are rather different, but
David Resnick provided a reasonable answer to the middle option.
Thank you. This not only makes sense but is actually on topic.

But my charitable mood has left me plus the opening poster says he
has solved the problem anyway.

Spiros Bousbouras

Jul 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
5
by: Gidraz | last post by:
Hello, i can't figure out how to retrieve clients MAC address using C#.NET. I can get IP but not MAC. Thanks in advance. Gidraz
3
by: Lee Crabtree | last post by:
I have a managed DLL that I've used to expose a C++ class. One of the functions in this class reads a line out of a file into a buffer of type "unsigned char*". Since all the data is just...
4
by: Lance | last post by:
I have an array of bytes that I need to convert into an array of Integers. But, the number of bits per value in the Byte array is not necessarily divisible by 8 (although it will never exceed...
3
by: simonc | last post by:
Can you define a property as type Byte array of a specific length? I am trying to pass a byte array which is 3200 bytes in length from one form (in which the bytes are read from a file) to...
5
by: RThaden | last post by:
Hi all, I have a text file with a list of MAC addresses. Each time, my program is called it reads the last MAC address entry from the file, increases it by one, writes this new address into a 6...
4
by: ankitabhatia | last post by:
i ave a byte array..ave 2 left shift it by 25 bits.. to be able 2 reuse de elements of de array.. ave to do this for encryption algo.. ne help will b gr8..thnx a lot:):) btw i need it in...
0
by: kuning | last post by:
sorry i cant speak english with perfect. i want to question, how to get ip address from mac address? if get mac address from ip address i have done, this is source code : need text1, text2,...
1
by: Chintan Shah | last post by:
I have a byte array which looks something like 01 00 02 00 73 45 69 A5 So i have to read first 2 bytes and convert it into integer to get its value. Same for next 2 bytes and then next 4...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.