summaryrefslogtreecommitdiff
path: root/java/src/com/zerotier/sdk/util/StringUtils.java
blob: c7bcc5d9f1a0bc4cfef4554c974cd1251f0e37dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2023  ZeroTier, Inc.  https://www.zerotier.com/
 */

package com.zerotier.sdk.util;

public class StringUtils {

    /**
     * Convert mac address to string.
     *
     * @param mac MAC address
     * @return string in XX:XX:XX:XX:XX:XX format
     */
    public static String macAddressToString(long mac) {

        int[] macChars = new int[6];
        for (int i = 0; i < 6; i++) {
            macChars[i] = (int) (mac % 256);
            mac >>= 8;
        }

        return String.format("%02x:%02x:%02x:%02x:%02x:%02x", macChars[5], macChars[4], macChars[3], macChars[2], macChars[1], macChars[0]);
    }

    /**
     * Convert long to hex string.
     *
     * @param networkId long
     * @return string with 0 padding
     */
    public static String networkIdToString(long networkId) {
        return String.format("%016x", networkId);
    }

    /**
     * Convert node address to string.
     *
     * Node addresses are 40 bits, so print 10 hex characters.
     *
     * @param address Node address
     * @return formatted string
     */
    public static String addressToString(long address) {
        return String.format("%010x", address);
    }

    public static String etherTypeToString(long etherType) {
        return String.format("%04x", etherType);
    }
}