def Hex2Bin(h, N): h = h.strip() # Remove leading and trailing whitespaces h = h.upper() # Convert to uppercase if len(h) == 0: return '' # Return empty string if input is empty

# Check for out of range values
for char in h:
    if not (char.isdigit() or (char >= 'A' and char <= 'F')):
        raise ValueError('Input string found with characters other than 0-9, a-f, or A-F.')

# Convert hexadecimal to decimal
d = int(h, 16)

# Convert decimal to binary
n = int(N)
if n < 0:
    raise ValueError('N must be a positive scalar numeric.')

binary = bin(d)[2:]  # Convert decimal to binary string
binary = binary.zfill(n)  # Pad with leading zeros if necessary

return binar
function s= Hex2BinhN h=h; Make sure h is a column vector if iscellstrh h = charh; end if isemptyh s = ; return end Work in upper case h = upperh; mn=sizeh; Right justify stri

原文地址: http://www.cveoy.top/t/topic/iZHp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录