S
S
Sash0k_k2014-04-09 11:03:36
Android
Sash0k_k, 2014-04-09 11:03:36

What is the best way to store the MAC address in the database?

In particular, it is necessary to store BD_ADDR in Sqlite, but the question is also interesting for the general case.
I understand that you can convert it to INTEGER as you like, or store it in TEXT just as it is.
Since the address formats are standardized (EUI-48, EUI-64), maybe there are generic storage solutions? It would suit the conversion by some general convention to INTEGER, since it is logical to make addresses the keys of the table. But it was not possible to google something like that.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sash0k_k, 2014-04-09
@Sash0k_k

Resolved, MAC address to be stored will be stored in INTEGER Sqlite (long in Java)
made its own conversion "readable MAC address" <=> long , for android:

BluetoothDevice device;
final String mac = device.getAddress();
final long id = Long.parseLong(mac.replaceAll(":", ""), 16); 
final String back_mac = idToBD_ADDR(id);

private static String idToBD_ADDR(long id) {
    final byte LAST_BYTE = 5;
    StringBuilder address = new StringBuilder();
    for (byte i = 0; i <= LAST_BYTE; i++) {
        byte b = (byte) (id >> 8*i);
        address.append(new StringBuilder(String.format("%02x", b)).reverse());
        if (i != LAST_BYTE) address.append(":");
    }
    return address.reverse().toString();
}

the idToBD_ADDR method is scary, of course, I will be glad to see a better version

M
Mercury13, 2014-04-09
@Mercury13

Because the MAC address is a 6-byte number, it's easiest to convert it to an 8-byte INTEGER. For example, 12:34:56:78:9A:BC becomes 0x1234'5678'9ABC. It is effective and a question only in manual editing of a DB.

M
Maxim Moseychuk, 2014-04-09
@fshp

It was not possible to google because the MAC address is an Int, in which the first two bytes are not significant, and the rest are written in hex. 28:d3:44:16:69:54 => 0x000028d344166954.

S
Sash0k_k, 2014-04-09
@Sash0k_k

@Mercury13 , @fshp thanks, that's basically what I wanted to do.
I will clarify that in Java it will be long, but in sqlite yes - you can store integers up to 8 bytes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question