# How to convert bytearray into integer?

Jacky jacky.chao.wang at gmail.com
Mon Aug 16 19:06:57 CEST 2010

```Hi there,

Recently I'm facing a problem to convert 4 bytes on an bytearray into
an 32-bit integer.  So far as I can see, there're 3 ways: a) using
struct module, b) using ctypes module, and c) manually manipulation.

Are there any other ways?

My sample is as following:

-----
import struct
import ctypes

def test_struct(buf, offset):
return struct.unpack_from("I", buf, offset)[0]

def test_ctypes(buf, offset):
return ctypes.c_uint32.from_buffer(buf, offset).value

def test_multi(buf, offset):
return buf[offset] + (buf[offset+1] << 8) + (buf[offset+2] << 16) +
(buf[offset+3] << 24)

buf_w = bytearray(5)
buf_w[1] = 1
buf_r = buffer(buf_w)

if __name__ == '__main__':
import timeit

t1 = timeit.Timer("test_struct(buf_r, 1)",
"from __main__ import test_struct, buf_r")
t2 = timeit.Timer("test_ctypes(buf_w, 1)",
"from __main__ import test_ctypes, buf_w")
t3 = timeit.Timer("test_multi(buf_w, 1)",
"from __main__ import test_multi, buf_w")
print t1.timeit(number=1000)
print t2.timeit(number=1000)
print t3.timeit(number=1000)
-----

Yet the results are bit confusing:

-----
number = 10000
0.0081958770752
0.012549161911
0.0112121105194

number = 1000
0.00087308883667
0.00125789642334
0.00110197067261

number = 100
0.0000917911529541     # 9.17911529541e-05
0.000133991241455
0.00011420249939

number = 10
1.69277191162e-05
2.19345092773e-05
1.69277191162e-05

number = 1
1.00135803223e-05
1.00135803223e-05
5.96046447754e-06
-----

As the number of benchmarking loops decreasing, method c which is
manually manipulating overwhelms the former 2 methods.  However, if
number == 10K, the struct method wins.

Why does it happen?

Thanks,
Jacky (jacky.chao.wang#gmail.com)

```