Hex converter help - Python

I have this code that converts binary to hex

## The Input part l = raw_input('Input 8-bit number: ') j = list(l) ## Value Error part t = [int(x) for x in j] if len(t) != 8 or len([n for n in t if n not in (1, 0)]) != 0: raise ValueError('NOT 1 OR 0, or NOT CORRECT LENGTH') ## The Conversion part a = l[0] b = l[1] c = l[2] d = l[3] e = l[4] f = l[5] g = l[6] h = l[7] p = a+b+c+d q = e+f+g+h y = hex(int(p, 2))[2:] z = hex(int(q, 2))[2:] ## The Results part print print 'Results' print 'Binary: ',l print 'Hex: ',y+z

my whole conversion part is insanely long because whenever i try to convert a number that has 0000 in front of it it doesnt print the 0 and i need it to print the 0 is there a way to do this without doing the long process

-------------Problems Reply------------

If I understand you correctly, you want to print out a 2-digit hex number that is gotten from the binary input l. You can do that as follows:

print 'Hex: ', hex(int(l, 2))[2:].zfill(2)

Skip all that coding.

First off, try hex(): http://docs.python.org/library/functions.html#hex

Then, check out format(): http://docs.python.org/library/functions.html#format

print "%02x" % int(raw_input("Enter a number in binary: "), 2)

Category:python Views:0 Time:2011-08-18
Tags: python

Related post

  • How do I convert a python string to ucs2 hex? 2010-07-19

    I've been searching for this one and couldn't find it, although it seems simple. I need to send in a ucs2 hex string in the url, and I don't know how to convert a python string to be ucs2 hex. Any thoughts? --------------Solutions------------- >

  • What's the correct way to convert bytes to a hex string in Python 3? 2011-07-08

    What's the correct way to convert bytes to a hex string in Python 3? I see claims of a bytes.hex method, bytes.decode codecs, and have tried other possible functions of least astonishment without avail. I just want my bytes as hex! --------------Solu

  • perl hex() analog in python 2012-02-13

    perl hex() analog in python how to? I have next perl code: my $Lon = substr($Hexline,16,8); say $output_f "Lon: " . hex($Lon) . ""; where $Hexline has "6a48f82d8e828ce82b82..." format I try it on python Lon = int(Hexline[16:24], 16) f.write('lon = %s

  • How do I code a source code converter from python to ruby? 2009-02-23

    My teacher told me that if I wanted to get the best grade in our programming class, I should code a Simple Source Code Converter. Python to Ruby (the simplest he said) Now my question to you: how hard is it to code a simple source code converter for

  • how to convert a python dict object to a java equivalent object? 2009-07-01

    This question already has an answer here: Java Equivalent to Python Dictionaries 3 answers I need to convert a python code into an equivalent java code. Python makes life very easy for the developers by providing lots of shortcut functionalities. But

  • How do you convert a python time.struct_time object into a datetime object? 2009-11-08

    How do you convert a python time.struct_time object into a datetime.datetime object? I have a library that provides the first one and a second library that wants the second one... --------------Solutions------------- Use time.mktime() to convert the

  • Help in Converting Small Python Code to PHP 2010-01-15

    please i need some help in converting a python code to a php syntax the code is for generating an alphanumeric code using alpha encoding the code : def mkcpl(x): x = ord(x) set="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" for c in

  • Convert between python array and .NET Array 2010-06-11

    I have a python method that returns a Python byte array.array('c'). Now, I want to copy this array using System.Runtime.InteropServices.Marshal.Copy. This method however expects a .NET array. import array from System.Runtime.InteropServices import Ma

  • Convert a python 'type' object to a string 2011-02-15

    I'm wondering how to convert a python 'type' object into a string using python's reflective capabilities. For example, I'd like to print the type of an object print "My type is " + type(someObject) # (which obviously doesn't work like this) EDIT: Btw

  • How to generate checksum from hex byte using python 2011-08-02

    I am creating a Hex file using python and at the end I need to add a checksum that consists of sum of all hex values so that checksum = Byte 0x000000 + Byte 0x000001 +…+ Byte 0x27DAFF (not including this 4 bytes). This checksum shall then be written

  • Converting a Python List into a Numpy Structured array? 2015-03-23

    I need to convert a Python list with mixed type (integers and floats) to a Numpy structured array with certain column names. I have tried the following code but for some reason I cant see it doesn't work. import numpy as np lmtype = [('el','intp',1),

  • Convert a Python int into a big-endian string of bytes 2009-05-10

    I have a non-negative int and I would like to efficiently convert it to a big-endian string containing the same data. For example, the int 1245427 (which is 0x1300F3) should result in a string of length 3 containing three characters whose byte values

  • How do I convert a Python float to a hexadecimal string in python 2.5? Nonworking solution attached 2009-06-27

    What I really need to do is to export a floating point number to C with no precision loss. I did this in python: import math import struct x = math.sqrt(2) print struct.unpack('ii', struct.pack('d', x)) # prints (1719614413, 1073127582) And in C I tr

  • Decode Hex String in Python 3 2010-07-19

    In Python 2, converting the hexadecimal form of a string into the corresponding unicode was straightforward: comments.decode("hex") where the variable 'comments' is a part of a line in a file (the rest of the line does not need to be converted, as it

  • Converting a Python Float to a String without losing precision 2010-08-13

    I am maintaining a Python script that uses xlrd to retrieve values from Excel spreadsheets, and then do various things with them. Some of the cells in the spreadsheet are high-precision numbers, and they must remain as such. When retrieving the value

  • refactor this dictionary-to-xml converter in python 2009-01-30

    It's a small thing, really: I have this function that converts dict objects to xml. Here's the function: def dictToXml(d): from xml.sax.saxutils import escape def unicodify(o): if o is None: return u''; return unicode(o) lines = [] def addDict(node,

  • How can I convert a Python dictionary to a list of tuples? 2009-03-23

    If I have a dictionary like: { 'a': 1, 'b': 2, 'c': 3 } How can I convert it to this? [ ('a', 1), ('b', 2), ('c', 3) ] And how can I convert it to this? [ (1, 'a'), (2, 'b'), (3, 'c') ] --------------Solutions------------- >>> d = { 'a': 1,

  • Convert a Python list with strings all to lowercase or uppercase 2009-11-26

    I have a python list variable that contains strings. Is there a python function that can convert all the strings in one pass to lowercase and vice versa, uppercase? --------------Solutions------------- It can be done with list comprehensions >>

  • Process to convert simple Python script into Windows executable 2010-01-26

    I wrote a script that will help a Windows user in her daily life. I want to simply send her the .exe and not ask her to install python, dlls or have to deal with any additional files. I've read plenty of the stackoverflow entries regarding compiling

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.163 (s). 11 q(s)