From HWaddress to long

A

Andrea Crotti

In short I need an hash function that gives me a long from an hardware
address.

What could be an easy solution that still gives me a good distribution?

From
"FF2212A.."
I thought I could simply do

(((16 * 16) % LONG_MAX) * 2) % LONG_MAX...

But probably it's a stupid idea.
A mac address is basically 24bytes, and a long is maybe max 8 bytes, so
of course I have to do something to squeeze it...
 
A

Andrea Crotti

Andrea Crotti said:
In short I need an hash function that gives me a long from an hardware
address.

What could be an easy solution that still gives me a good distribution?

From
"FF2212A.."
I thought I could simply do

(((16 * 16) % LONG_MAX) * 2) % LONG_MAX...

But probably it's a stupid idea.
A mac address is basically 24bytes, and a long is maybe max 8 bytes, so
of course I have to do something to squeeze it...

Of course something already done would be also good.
A generic string hash I'm not sure, since my strings only uses 16
possible characters.

(for example I found this
http://www.cplusplus.com/reference/std/locale/collate/hash/)
But I guess an md5/sha would be also fine, right?
 
F

Fred Zwarts

Andrea Crotti said:
In short I need an hash function that gives me a long from an hardware
address.

What could be an easy solution that still gives me a good
distribution?

From
"FF2212A.."
I thought I could simply do

(((16 * 16) % LONG_MAX) * 2) % LONG_MAX...

But probably it's a stupid idea.
A mac address is basically 24bytes, and a long is maybe max 8 bytes,

An Ethernet MAC address is 6 bytes (48 bits).
What mac address are you talking about?
 
A

Andrea Crotti

Kevin P. Fleming said:
A MAC address is 6 bytes (48 bits). It will fit in a 64-bit unsigned
integer (which might be a 'long' on some platforms and not on others)
just fine.

Ah true I "miscomputed", then it's even easier.
Well then from that long I have to go to a much smaller value (maybe 1
byte only).

How do I map well a long into a uint_8 for example?
For the string I found this which could work just fine then:

long PadNodeID::hash() const
{
long x;
std::stringstream ss;
ss << std::hex << id;
ss >> x;
// output it as a signed type
return static_cast<long>(x);
}
 
J

James Kanze

A MAC address is 6 bytes (48 bits). It will fit in a 64-bit
unsigned integer (which might be a 'long' on some platforms
and not on others) just fine.

And it's guaranteed to fit in a long long. Depending on table
size and how MAC addresses are allocated, however, it's possible
that converting the MAC address to size_t might not result in
a very good hash. If that's the case, using a good string hash
over the six bytes could reduce collisions significantly.
 
A

Andrea Crotti

James Kanze said:
And it's guaranteed to fit in a long long. Depending on table
size and how MAC addresses are allocated, however, it's possible
that converting the MAC address to size_t might not result in
a very good hash. If that's the case, using a good string hash
over the six bytes could reduce collisions significantly.


Well I have 2 choices, or I have a nice hash function over the string
and then I just use the modulo to get the 1 byte I need, OR I have a
stupid string conversion and a better hash.

Depending on what's easier/better I can choose...
 
J

James Kanze

Well I have 2 choices, or I have a nice hash function over the string
and then I just use the modulo to get the 1 byte I need, OR I have a
stupid string conversion and a better hash.
Depending on what's easier/better I can choose...

What's easiest depends on the format in which you have the MAC
address. If it is stored as a unsigned long long (or something
similar), then just manually converting it to a size_t is
easiest; if that turns out to result in a bad hash, there's
always time to change. If it is stored in a character
representation or as a sequence of "bytes" (char or unsigned
char), then using a good quality string hashing algorithm is
simplest. (All of the usual string hashing algorithms will work
on a series of bytes---they don't require "characters" in the
string.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top