public int convert_msgtobin(String msg)
    {
        int m = msg.length();
        if (m < 14)
        {
            return -1;  // Too short for a valid CAN msg
        }
        if (m > 30)
        {
            return -2;  // Longer than the longest CAN msg
        }
        if ((m & 0x1) != 0)
        {
            return -3; // Not even: asci1-hex must be in pairs
        }
        try{
            pb = DatatypeConverter.parseHexBinary(msg); // Convert ascii/hex to byte array
        }
        catch(IllegalArgumentException e){
            System.err.println("Caught IOException: " + e.getMessage());
            return -6;
        }

        /* Check computed checksum versus recieved checksum.  */
        byte chkx = checksum((m / 2) - 1);
        if (chkx != pb[((m / 2) - 1)])
        {
// System.out.println(msg);    // Display for debugging
// for (int j = 0; j < (m / 2); j++)
// {
//     System.out.format("%02X ", pb[j]);
// }
// System.out.format("chkx: %02X" + " pb[((m/2) -1)]: %02X\n", chkx,
//         pb[((m / 2) - 1)]);
            return -5; // Return error code
        }

        /* Check that the payload count is within bounds */
        if (pb[5] < 0)
        {
            return -4;    // This should not be possible
        }
        if (pb[5] > 8)
        {
            return -4;    // Too large means something wrong.
        }
        /* Extract some items that are of general use */
        seq = (pb[0] & 0xff);     // Sequence number byte->unsigned
        dlc = (pb[5] & 0xff);     // Save payload ct in an easy to remember variable
        id = ((((((pb[4] << 8) | (pb[3] & 0xff)) << 8)
                | (pb[2] & 0xff)) << 8) | (pb[1] & 0xff));
        return 0;
    }