SHARE
TWEET

Untitled

Bondok26 Nov 19th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def hex2bin(hex_val, num_bits):  # Returns binary string of num_bits length of hex value (pos or neg)
  2.  
  3.     bit_string = '0' * num_bits
  4.     bin_val = str(bin(int(str(hex_val), 16)))[2:]
  5.     bit_string = bit_string[0: num_bits - len(bin_val)] + bin_val
  6.     return bit_string
  7.  
  8. def dec2bin(dec_value, num_bits):
  9.     bit_string = '0' * num_bits
  10.     bin_val = str(bin(dec_value))[2:]
  11.     bit_string = bit_string[0: num_bits - len(bin_val)] + bin_val
  12.     return bit_string
  13.  
  14.  
  15.  
  16. '''
  17.  
  18.  
  19. # Adjust for negative by performing Two's Complement (tc)  written above the code
  20.     tc = False
  21.     if '-' in hex_val:
  22.         tc = True
  23.         hex_val = hex_val.replace('-', '')
  24.  # Two's complement if negative hex value  written under the code
  25.  if tc:
  26.      tsubstring = bit_string[0:bit_string.rfind('1')]
  27.      rsubstring = bit_string[bit_string.rfind('1'):]
  28.      tsubstring = tsubstring.replace('1', 'X')
  29.      tsubstring = tsubstring.replace('0', '1')
  30.      tsubstring = tsubstring.replace('X', '0')
  31.      bit_string = tsubstring + rsubstring
  32.  
  33.  return bit_stringng[num_bits-1:])
  34. '''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top