mob41 / broadlink-java-api

A clean Broadlink API for Java
MIT License
22 stars 25 forks source link

macStrToBytes assumes numbers only #13

Closed kaminskypavel closed 6 years ago

kaminskypavel commented 6 years ago

maybe im missing something out, but it seems like macStrToBytes(String macStr) assumes that macStr contains numbers only, and following loop throws an exception

 byte[] bout = new byte[6];
         for (int i = 0; i < macs.length; i++) {
             try {
                 bout[i] = Byte.parseByte(macs[i]);
             } catch (NumberFormatException e) {
                 throw new MacFormatException(macStr, e);
             }
         }
kaminskypavel commented 6 years ago

Update

I was able to make this work by fixing fixing bytesToMacStr method in Mac.java just copy paste the following code.

    public static byte[] macStrToBytes(String macStr) throws MacFormatException {
        if (macStr == null) {
            throw new MacFormatException(macStr);
        }

        String[] macs = macStr.split(":");

        if (macs.length != 6) {
            throw new MacFormatException(macStr);
        }

        byte[] bout = new byte[6];
        for (int i = 0; i < macs.length; i++) {
            try {
                _Integer hex = Integer.parseInt(macs[i], 16);
                bout[i] = hex.byteValue();_
            } catch (NumberFormatException e) {
                throw new MacFormatException(macStr, e);
            }
        }

        return bout;
    }

@mob41 , mind merging?

mob41 commented 6 years ago

Yea, thank you for reporting this. It is appreciated that you can modify it and push to this repository. Thanks!

kaminskypavel commented 6 years ago

sure there you go, #14.