Python Forum
decimal or hexadecimal digits to int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decimal or hexadecimal digits to int
#1
i want to convert a string of digits to an int like int() does.  the digits will be decimal by default except if prefixed by '0x' or '0X' then they are hexadecimal, or if prefixed by '0o' or '0O' then octal, or if prefixed by '0b' or '0B' then binary.  is there any built-in way or do i need to code my own?  C can do this at least for decimal and  hexadecimal.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
eval will do it. However, if you don't have control over your input stream, eval is a security risk. You would either want a regex to confirm it's just a number, or you would want to do a conditional and just convert with int using the base parameter.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The python int() function takes in a base parameter which converts to object/string to base 10 from the given base.
So int('101',2) = 5.

Here's one way you can write you program

def convertToInt(num_string):
    determine_base ={'0x':16,'0b':2,'0o':8} # dict to detrmine base

    # returns base from dict defaults to None(for base 10)
    base = determine_base.get(num_string[:2].lower(),None) 

    if base != None:
        return int(num_string[2:],base)
    else:
        return int(num_string)
        
# prefix '0x' & '0X' => hexadecimal, '0b' & '0B' => binary, '0O' and '0o' => Octal
strings = ['0xd4','0B10001','0o763','56'] 

for num in strings:
    print(convertToInt(num))
Reply
#4
(Sep-26-2017, 06:58 AM)hbknjr Wrote: The python int() function takes in a base parameter which converts to object/string to base 10 from the given base.
So int('101',2) = 5.

Here's one way you can write you program

def convertToInt(num_string):
determine_base ={'0x':16,'0b':2,'0o':8} # dict to detrmine base

# returns base from dict defaults to None(for base 10)
base = determine_base.get(num_string[:2].lower(),None)

if base != None:
return int(num_string[2:],base)
else:
return int(num_string)

# prefix '0x' & '0X' => hexadecimal, '0b' & '0B' => binary, '0O' and '0o' => Octal
strings = ['0xd4','0B10001','0o763','56']

for num in strings:
print(convertToInt(num))
something like that was what i had in mind.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  method to remove zero digits from fraction in decimal Skaperen 17 2,570 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  checking for valid hexadecimal digits Skaperen 3 6,265 Sep-02-2021, 07:22 AM
Last Post: buran
  Single digits seem to be greater than double digits Frosty_GM 4 3,434 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
Smile send hexadecimal package variable UART santos20 2 2,150 Oct-30-2020, 11:40 AM
Last Post: Larz60+
  Printing digits after the decimal point one by one uberman321 1 1,702 Oct-20-2020, 08:10 AM
Last Post: bowlofred
  testing for Decimal w/o importing decimal every time Skaperen 7 4,362 May-06-2019, 10:23 PM
Last Post: Skaperen
  I'm dividing large numbers of about 25 million digits I need to retain decimal format Pleiades 2 3,141 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  Input a number with any number of digits and output it in reverse order of digits. sarada2099 6 6,572 Mar-12-2017, 05:45 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020