440,036 Members | 1,963 Online Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

# Way to know bytes of an int in Python [with bits class thrown in]

 P: 7 Hi, I need to know the byte which a number is made of. For example, let's suppose I have i = 3000 I would like to know if there is a way to get the single byte of the number (0x0B and 0xB8). Thanks in advance, Daniele May 18 '07 #1
15 Replies

 Expert 5K+ P: 6,596 Hi, I need to know the byte which a number is made of. For example, let's suppose I have i = 3000 I would like to know if there is a way to get the single byte of the number (0x0B and 0xB8). Thanks in advance, Daniele Expand|Select|Wrap|Line Numbers >>> aHex = hex(3000) # call built-in function to convert to hex >>> print aHex 0xbb8 >>> bytes = aHex[2:].zfill(4) # slice off the '0x' >>> print bytes 0bb8 >>> highByte = '0x%s' % bytes[0:2] #reformat with '0x' added to just the high byte >>> print highByte 0x0b >>>  Let's see if you can get the low byte. We're here to help. May 18 '07 #2

 Expert 100+ P: 136 Just a curiosity but is there any built-in that would convert to binary? For example: 3000='101110111000' May 18 '07 #3

 P: 7 First of all thanks, the lowbyte comes from lowByte = '0x%s' % bytes[2:4] I've found also another solution: Expand|Select|Wrap|Line Numbers # mod edit # added for completeness # import struct ################################ def getByteLength(self, length):         temp = struct.pack('!h', length)         byteList = [firstLength, secondLength] = struct.unpack('BB', temp)         return list(byteList)   to obtain a list in which the elements are the needed bytes May 18 '07 #4

 Expert 5K+ P: 6,596 Just a curiosity but is there any built-in that would convert to binary? For example: 3000='101110111000' While it's true that >>> int('101', 2) 5 >>> There is no bit or byte type to call for conversion to base 2. May 18 '07 #5

 Expert 5K+ P: 6,596 While it's true that >>> int('101', 2) 5 >>> There is no bit or byte type to call for conversion to base 2. How 'bout this little goodie: Expand|Select|Wrap|Line Numbers def Byte2Bits(value):     # Create a list of bits to convert big endian wise     data = []     outStr = ""     for i in range(8):         value, rem = divmod(value, 2)         data.insert(0, rem)       for pos, i in enumerate(data):         if not (pos % 4):             outStr += " "         outStr += str(i)     return outStr     def Base2Repr(value):     # Create a list of bytes to convert big endian wise     data = []     outStr = ""     while value:         value, rem = divmod(value, 256)         data.insert(0, rem)     for i in data:         outStr += Byte2Bits(i)     return outStr[1:]     class bits(object):     def __init__(self, value):         self.value = value       def __repr__(self):         return Base2Repr(self.value)       if __name__ == "__main__":     b = bits(0xf5a5)     print b   1111 0101 1010 0101 May 18 '07 #6

 Expert Mod 2.5K+ P: 2,851 Just a curiosity but is there any built-in that would convert to binary? For example: 3000='101110111000' I don't think so, but I made this up: Expand|Select|Wrap|Line Numbers def ConvDecToBaseVar(num, base):     if base > 10 or base < 2:         raise ValueError, 'The base number must be between 2 and 10.'     if num == 0: return 0     ans = ''     while num != 0:         num, rem = divmod(num, base)         ans =  str(rem)+ans     return int(ans)   ''' >>> ConvDecToBaseVar(3000,2) 101110111000L >>> ''' May 18 '07 #7

 Expert 100+ P: 136 I was just curious about a built-in but that is absolutely Beautiful! May 18 '07 #8

 Expert 5K+ P: 6,596 I was just curious about a built-in but that is absolutely Beautiful! It's beautiful, all right (ain't BV good). But what about nibble formatting? May 18 '07 #9

 Expert Mod 2.5K+ P: 2,851 I was intrigued by a problem Motoma tackled and posted to Python Articles. http://www.thescripts.com/forum/thread648799.html May 18 '07 #10

 Expert 100+ P: 511 Just a curiosity but is there any built-in that would convert to binary? For example: 3000='101110111000' Expand|Select|Wrap|Line Numbers def dectobin(number):     if number < 1: return ""     else:return dectobin(number/2) + str(number & 1)    May 19 '07 #11

 Expert 5K+ P: 6,596 Expand|Select|Wrap|Line Numbers def dectobin(number):     if number < 1: return ""     else:return dectobin(number/2) + str(number & 1)    VERY nice, GD. So, let's see: If number is odd, then the next [L]MSb is a one... Am I getting that right? And where/how did you dream up that algorithm? May 19 '07 #12

 Expert 100+ P: 511 VERY nice, GD. So, let's see: If number is odd, then the next [L]MSb is a one... Am I getting that right? And where/how did you dream up that algorithm? the & operator is just to test the LSB of binary division. say for example 14 divide by 2. in binary division of these 2 numbers, 1110 divide by 10 , as you slowly progress, you will reach the LSB and it will always be 0 or 1. In & operations, a true and true is a true. So if the LSB is 0, & with 1 will be 0. (tested no remainder) . if LSB is 1, & with 1 is a 1, so there's remainder. ( in this case, 14/2 is remainder 0, so the bit tested is 0 ) and then we do 7/2 , 3/2 and so on.....the rest of the code is just to chain together all these 0's and 1's tested. hope i didn't miss anything... May 19 '07 #13

 Expert 2.5K+ P: 3,235 I don't think so, but I made this up: Expand|Select|Wrap|Line Numbers def ConvDecToBaseVar(num, base):     if base > 10 or base < 2:         raise ValueError, 'The base number must be between 2 and 10.'     if num == 0: return 0     ans = ''     while num != 0:         num, rem = divmod(num, base)         ans =  str(rem)+ans     return int(ans)   ''' >>> ConvDecToBaseVar(3000,2) 101110111000L >>> ''' Gotta love reusable code, eh? May 19 '07 #14

 Expert Mod 2.5K+ P: 2,851 Gotta love reusable code, eh? Yes. Here's some more recycling - Expand|Select|Wrap|Line Numbers def ConvDecToBaseVar3(num, base):     if base > 16 or base < 2:         raise ValueError, 'The base number must be between 2 and 16.'     dd = dict(zip(range(16), [hex(i).split('x') for i in range(16)]))     if num == 0: return ''     num, rem = divmod(num, base)     return ConvDecToBaseVar3(num, base)+dd[rem] Expand|Select|Wrap|Line Numbers >>> ConvDecToBaseVar3(123456789, 16) '75bcd15' >>> ConvDecToBaseVar3(2**200, 16) '100000000000000000000000000000000000000000000000000' >>> ConvDecToBaseVar3(2**200, 7) '141246066533632643213232344050606053061443446006544361632102630555343054' >>>  It's not suitable for very long numbers due to the recursion limit. May 19 '07 #15

 Expert Mod 2.5K+ P: 2,851 This is considerably more efficient than the previous version: Expand|Select|Wrap|Line Numbers def ConvDecToBaseVar3(num, base, dd=False):     if base > 16 or base < 2:         raise ValueError, 'The base number must be between 2 and 16.'     if not dd:         dd = dict(zip(range(16), [hex(i).split('x') for i in range(16)]))     if num == 0: return ''     num, rem = divmod(num, base)     return ConvDecToBaseVar3(num, base, dd)+dd[rem] This avoids the recursion limit: Expand|Select|Wrap|Line Numbers def ConvDecToBaseVar4(num, base):     if num < 2: return str(num)     if base > 16 or base < 2:         raise ValueError, 'The base number must be between 2 and 16.'     dd = dict(zip(range(16), [hex(i).split('x') for i in range(16)]))     ans = ''     while num != 0:         num, rem = divmod(num, base)         ans =  str(rem)+ans     return ans May 19 '07 #16 