Em Andamento

Convert 51 lines of C to Python

Convert 51 lines of code from C to python. Results must be bit-for-bit identical with that of the following code and must be fast and efficient as the methods will be called perhaps several hundred times per second. It should use a buffer class of some sort with putInt() and putUint() methods and getInt() and getUint() methods that pop stuff off the buffer. Please see the attached text file which includes the 51 lines of code that need converting, an example of the tests I would like it to pass, and an example of the usage of the resulting class.

## Deliverables

Working code in a single .py file with tests, output, and access methods as described in the description section and attached file. It is important that the actual data be stored in a string that is bit-for-bit identical to the representation of the data in the C code being converted as the resulting code will be incorporated into a game sending the data to the program written in C.

Your code should work more or less as follows:

b = sauerBuffer()

[url removed, login to view](sauerConst['SV_INITS2C'])

[url removed, login to view]([url removed, login to view])

[url removed, login to view](252) # Protocol Version

[url removed, login to view](4)

print repr([url removed, login to view])

## Testing

Your code, complete with an example testing getint and getuint and putint putuint to test all values within their respective ranges.

Test should probably be similar to the following:

def test_int(self):

b = 2 ** 31

lower_bound = -b

upper_bound = b

i = lower_bound

while i < upper_bound:

p = sauerBuffer()

try:

[url removed, login to view](i)

except:

print "could not putint ", i

if([url removed, login to view]() != i):

print "FAILED ", i

i+= 1

def test_uint(self):

b = 2 ** 28

lower_bound = 0

upper_bound = b

i = lower_bound

while i < upper_bound:

p = sauerBuffer()

try:

[url removed, login to view](i)

except:

print "could not putuint ", i

if([url removed, login to view]() != i):

print "FAILED ", i

i+= 1

## The code you are converting From C to python:

void putint(ucharbuf &p, int n)

{

if(n<128 && n>-127) [url removed, login to view](n);

else if(n<0x8000 && n>=-0x8000) { [url removed, login to view](0x80); [url removed, login to view](n); [url removed, login to view](n>>8); }

else { [url removed, login to view](0x81); [url removed, login to view](n); [url removed, login to view](n>>8); [url removed, login to view](n>>16); [url removed, login to view](n>>24); }

}

int getint(ucharbuf &p)

{

int c = (char)[url removed, login to view]();

if(c==-128) { int n = [url removed, login to view](); n |= char([url removed, login to view]())<<8; return n; }

else if(c==-127) { int n = [url removed, login to view](); n |= [url removed, login to view]()<<8; n |= [url removed, login to view]()<<16; return n|([url removed, login to view]()<<24); }

else return c;

}

// much smaller encoding for unsigned integers up to 28 bits, but can handle signed

void putuint(ucharbuf &p, int n)

{

if(n < 0 || n >= (1<<21))

{

[url removed, login to view](0x80 | (n & 0x7F));

[url removed, login to view](0x80 | ((n >> 7) & 0x7F));

[url removed, login to view](0x80 | ((n >> 14) & 0x7F));

[url removed, login to view](n >> 21);

}

else if(n < (1<<7)) [url removed, login to view](n);

else if(n < (1<<14))

{

[url removed, login to view](0x80 | (n & 0x7F));

[url removed, login to view](n >> 7);

}

else

{

[url removed, login to view](0x80 | (n & 0x7F));

[url removed, login to view](0x80 | ((n >> 7) & 0x7F));

[url removed, login to view](n >> 14);

}

}

int getuint(ucharbuf &p)

{

int n = [url removed, login to view]();

if(n & 0x80)

{

n += ([url removed, login to view]() << 7) - 0x80;

if(n & (1<<14)) n += ([url removed, login to view]() << 14) - (1<<14);

if(n & (1<<21)) n += ([url removed, login to view]() << 21) - (1<<21);

if(n & (1<<28)) n |= 0xF0000000;

}

return n;

## Platform

It should run anywhere python does. Making use of Cheese shop modules is AOK.

Habilidades: Engenharia, Consolas de Jogos, Design de jogos, MySQL, PHP, Python, Arquitetura de software, Teste de Software

Ver mais: unsigned bits, representation integers, python shop, need python code, modules anywhere, example php program, get string, bits of, bit bits, bit representation, repr, python work, python program, python data, python 3, protocol testing, php python, print stuff, fast efficient, ent, c to, php, c p, convert python text file, python code game

Acerca do Empregador:
( 1 comentário ) United States

ID do Projeto: #2963039

Premiar a:

ThomasKh

See private message.

$17 USD em 7 dias
(43 Avaliações)
4.4

3 freelancers estão ofertando em média $17 para este trabalho

MuktoSoftware

See private message.

$17 USD in 7 dias
(453 Comentários)
7.5
moxiesolutions

See private message.

$17 USD in 7 dias
(0 Comentários)
0.0