code: def hexstr_if_str(to_type, hexstr_or_primitive):
      
      if isinstance(hexstr_or_primitive, str):
          (primitive, hexstr) = (None, hexstr_or_primitive)
          if remove_0x_prefix(hexstr) and not is_hex(hexstr):
              raise ValueError(
                  "when sending a str, it must be a hex string. Got: {0!r}".format(
                      hexstr_or_primitive,
                  )
              )
      else:
          (primitive, hexstr) = (hexstr_or_primitive, None)
      return to_type(primitive, hexstr=hexstr)
docs: Convert to a type, assuming that strings can be only hexstr (not unicode text)
  
      @param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
          eg~ to_bytes, to_text, to_hex, to_int, etc
      @param text_or_primitive in bytes, str, or int.