Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The 'b' Word
#1
Another Project I Am Working On Here The Problem Is:
How To Remove That 'b' Word In Binary
Output:
Please Type The Number: 0x3 The 0x3 In Decimal Number Is = 3 The 0x3 In Binary Number Is = 0b11
Here Is The Source Code
#All Bases Start Here
#All Bases End Here
def IsNumericBase(s, base):
 try:
  int(s, base)
  return True
 except ValueError:
  return False
def isDeci(s):
    return IsNumericBase(s, 10)
# Returns True if <s> is binary number string
def IsBinaryString(s):
 return IsNumericBase(s, 2)

# Returns True if <s> is octal number string
def IsOctalString(s):
 return IsNumericBase(s, 8)

# Returns True if <s> is hexadecimal number string
def IsHexadecimalString(s):
 return IsNumericBase(s, 16)

#Main Def Starting
def maincheck(temp):
    a = isDeci(temp)
    if a == False:
        b = IsBinaryString(temp)
        if b == False:
            oc = IsOctalString(temp)
            if oc == False:
                hexa = IsHexadecimalString(temp)
                if hexa == False:
                    print("Only Hex,Numbers,Binary And Octal Are Supported")
                else:
                    print("The " + str(temp) +" In Decimal Number Is = ",int(temp, 16))
                    print("The " + str(temp) +" In Binary Number Is = ",bin(int(temp,16)))
            else:
                print("Is " + str(temp) +" Octal Number? = ",hexa)
        else:
            print("Is " + str(temp) +" Binary Number? = ",b)
    else:
        print("Is " + str(temp) +" Decimal Number? = ",a)
#Main Def Ending 

if __name__ == "__main__":
    print("\t\tWelcome To Hex_To_Decimal_Binary_Octal\n")
    #for i in range(1,6):
    temp = input("Please Type The Number: ")
    maincheck(temp)
Reply
#2
If you have a string that you know is '0b...' or '0x...' how do you print just the '...' part? Look at slices.
Reply
#3
Use string formatting
spam = 3
print(bin(spam))
print(f'{spam:b}')
Output:
0b11 11
also looking at the docs for bin():
https://docs.python.org/3/library/functions.html#bin

Quote:If prefix “0b” is desired or not, you can use either of the following ways.
>>>format(14, '#b'), format(14, 'b')
('0b1110', '1110')

>>>f'{14:#b}', f'{14:b}'
('0b1110', '1110')
See also format() for more information.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Hey! This Problem Now Another Problem When I Type 0b01 in decimal it is saying 2817 i don't know why!
Reply
#5
0b01 hexadecimal (base 16) is 2817 in decimal (base 10)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Aug-12-2020, 08:41 AM)buran Wrote: 0b01 hexadecimal (base 16) is 2817 in decimal (base 10)

Hey! Thanks For Telling I Found The Wrong It was set int(temp,16) that's Why it tells me wrong
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Speech recognition, word by word AceScottie 6 8,966 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 814 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  difference between word: and word[:] in for loop zowhair 2 1,454 Mar-03-2018, 07:24 AM
Last Post: zowhair

Forum Jump:

User Panel Messages

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