Home > front end >  How Do I Convert Integer To String Without Using int()
How Do I Convert Integer To String Without Using int()

Time:11-07

Basically what the title says. For my assignment I need to create a function that takes in a binary in string and I have to convert it to decimal in integer but I can't use the int() function. However, I don't know any other way to convert string into integer.

CodePudding user response:

This is a long code that works:

def string_to_int(s):
    rtr=0
    for c in s:
        rtr=rtr*10   ord(c) - ord('0')

    return rtr

print(string_to_int("12445"))

output:

12445

CodePudding user response:

I found this at https://www.geeksforgeeks.org/how-to-convert-string-to-integer-in-python/

# User-defined function to 
# convert a string into integer
def string_to_int(input_string):
    
  output_int = 0
    
  # Check if the number contains
  # any minus sign or not, 
  # i.e. is it a negative number or not. 
  # If it contains in the first
  # position in a minus sign,
  # we start our conversion 
  # from the second position which
  # contains numbers.
  if input_string[0] == '-' :
    starting_idx = 1
    check_negative = True
      
  else:
    starting_idx = 0
    check_negative = False
      
  for i in range(starting_idx, len(input_string)):
      
    # calculate the place value for 
    # the respective digit
    place_value = 10**(len(input_string) - (i 1))
      
    # calculate digit value
    # ord() function gives Ascii value
    digit_value = ord(input_string[i]) - ord('0')
      
    # calculating the final integer value
    output_int  = place_value * digit_value
      
  # if check_negative is true 
  # then final integer value 
  # is multiplied by -1
  if check_negative :
    return -1 * output_int
  else:
    return output_int
  
# Driver code
if __name__ == "__main__" : 
    
  string = "554"
  
  # function call
  x = string_to_int(string)
  
  # Show the Data type
  print(type(x))
  
  string = "123"
  
  # Show the Data type
  print(type(string_to_int(string))) 
  
  string = "-123"
  
  # Show the Data type
  print(type(string_to_int(string)))

CodePudding user response:

def binary_to_int(in_string):
    result = 0
    # the 'power' of the current binary digit
    magnitude = 1
    # look at each digit, starting from the right
    for c in s[::-1]:
        # only the 1 digits have value here
        if c == "1":
             result  = factor
        # after every digit, we go up a power
        factor *= 2
    return result

Remember to cite your sources in Chicago format.

  • Related