Question

"Create a program in Python that converts a decimal to a hexadecimal, then from hexadecimal to decimal." I have completed the decimal-to-hexadecimal conversion, but how do I get the hexadecimal back to a decimal? Code please!

def decToHexa (n): # char array to store # hexadecimal number hexadeciNum = [0] * 100; # counter for hexadecimal # number a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment..

def decToHexa(n): 1. hexaDeciNum while(n#0): temp = 0 %3D temp = n % 16 if(temp<10): hexaDeciNum = chr(temp+48) + hexaDeciNum

15 16 def HexaToDec(hex): 17 18 hex[ :: -1].upper() hex = 19 20 for c in hex: 21 if c in ABCDEF: 22 n += (ord(c) - ord(A)

nagarajuanagaraju-Vostro-3550:13012020$ python3 conversion.py 9F1 2545

Code:

def decToHexa(n):

hexaDeciNum = ""

while(n!=0):

temp = 0

temp = n % 16

if(temp<10):

hexaDeciNum = chr(temp+48) + hexaDeciNum

else:

hexaDeciNum = chr(temp+55) + hexaDeciNum

n = int(n/16)

print(hexaDeciNum)

n = 2545

decToHexa(n)


def HexaToDec(hex):

n = 0

hex = hex[::-1].upper()

p = 1

for c in hex:

if c in "ABCDEF":

n += (ord(c) - ord('A')+10)*p

else:

n += (ord(c) - ord('0'))*p

p *= 16

print(n)

HexaToDec("9F1")

Add a comment
Know the answer?
Add Answer to:
"Create a program in Python that converts a decimal to a hexadecimal, then from hexadecimal to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

  • The code is to convert the fractional part of a decimal number to its hexadecimal form....

    The code is to convert the fractional part of a decimal number to its hexadecimal form. Can you explain the code, for example ,why the fractional part need to multiply 16 instead of devise 16? FracNum DecNum-num int count, IntPart float FracPart; float temp temp-FracNum *16; FracPartstemp- (int) temp; //getting Fractional part from temp IntPart (int) temp; count-e ise while (FracPart >e && count<=6) //while fractional part e and count Less than or equal to 6 then //Now taking fractional...

  • Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...

    Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to 100 digits (plus a sign). The calculator has a simple user interface, and 10 \variables" (n0, n1, ..., n9) into which hexadecimal integers can be stored. For example a session with your calculator might look like: mac: ./assmt1 > n0=0x2147483647 > n0+0x3 > n0? 0x214748364A > n1=0x1000000000000000000 > n1+n0 > n1? 0x100000000214748364A > n0? 0x214748364A > exit mac: Note: \mac: " is...

  • Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The...

    Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The maximum length of any operand or a solution is 40 digits. The input will be in the following format: Op1 op op2 = There is no space between operands and operator. Note 5/2 = quotient 2, remainder 1 2$3 = 8 The output should be of the form 2*3=6. Read date from a file. TEST DATA (input.txt): AAAA+BBF= BFD+2DE= 100*AA= 100$5= 100/F= 10000000000000-1= AAAAABBBBBCCCCCDDDDDEEEEEFFFFF-ABCDEF0123456789ABCDEF=...

  • Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we...

    Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we considered converting a byte string to decimal. What about converting a binary string of arbitrary length to decimal? Given a binary string of an arbitrarily length k, bk-1....bi .box the decimal number can be computed by the formula 20 .bo +21.b, + ... + 2k-1. bx-1- In mathematics, we use the summation notation to write the above formula: k- 2.b; i=0) In a program,...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Simplify for loop condition in getline. This program reads lines from the terminal, converts them to...

    Simplify for loop condition in getline. This program reads lines from the terminal, converts them to C strings by appending \0, and prints the longest one found. We need the ability to read in a line and the ability to save a line. We write two function getline and copy for these task. Using C language #include <stdio.h> #def i ne MAXL INE 1 000 int getL ine(char ne], int maxl ine); void copy( char to[], char from]); int main)...

  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal...

    [Using Python] Write a program to convert a hexadecimal number to its decimal value. (Reminder: hexadecimal numbers are 0 through 9, A,B,C,D,E,F. hex(A) = 10, hex(F) = 15). example outputs: 1. `Enter a hex number: f` `The decimal value for hex number f is 15` 2. `Enter a hex number: g` `Incorrect hex number` 3. `Enter a hex number: 091c` `The decimal value for hex number 091c is 2332` 4. `Enter a hex number: 091g` `Incorrect hex number` Hints: you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT