def file2c(fn, array_name):
    """ Read a file and return a C-compatible array with its binary data """

    # Open file and generate a list with hex-encoded byte contents
    with open(fn, 'rb') as f:
        rawdata = f.read()
    hexdata = [hex(x) for x in rawdata]

    # Generate C-compatible array

    # Header
    c_str = 'const char ' + array_name + '[' + str(len(hexdata)) + '] = {'

    # Data contents
    for x in hexdata:
        c_str += x + ', '

    # Remove last ', ' elements
    c_str = c_str[:-2]

    # Tail
    c_str += '};'
    return c_str