0

I am working on Client Server application where I need to send 6 byte mac id (FFF000 - FFF1FF) to the server from the client. I wanted to know the how best to represent in c++.

I thought of declaring it as unsigned long and then covert it in 6 bytes and send it to the server.

Is this correct do this. Any Other alternative please suggest.

Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
  • Why would it have to be "converted to hexadecimal" to send to the server? Hexadecimal, decimal, octal, base-64, etc. are just different "views" (representations with different symbols) of the same data. –  Apr 27 '11 at 05:55
  • Sorry it should 6 bytes instead of hexadecimal – Vikram Ranabhatt Apr 27 '11 at 05:57
  • 2
    Just use a 64-bit variable (an unsigned long is not necessarily it -- perhaps "long long" or "int64_t"? See [size of int, long, etc](http://stackoverflow.com/questions/589575/c-size-of-int-long-etc)) and only send/use the low-6 bytes? –  Apr 27 '11 at 06:05

1 Answers1

1

I don't think I'd use an integral type for this, since we would never treat the value as a number. I would represent it as a sequence of 6 bytes. Some possibilities are:

unsigned char macid[6];
typedef unsigned char macid[6];
struct macid {
    unsigned char data[6];
};

But in the end, I'd probably opt for:

std::tr1::array<unsigned char, 6> macid;
send(serverFd, &macid[0], macid.size());
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • `std::tr1::array<>` is cool for a short-term solution (or even internal implementation for `macid` class). If, however, there are named operations to perform on `macid` values, I would definitely opt for the class solution. – André Caron Apr 27 '11 at 18:43