[C++-sig] problem about conertting c++ method to python
sam wang
samwzm at yahoo.com
Mon Jul 17 23:41:05 CEST 2006
hi, there,
I want to convert a little c++ method to python. it just retrieves the length of some unsigned
values from a vector according to some rules.
one of them seems work while another one didn't work, could you please let me know what's wrong
and how I should correct it?
any hint will great appreciate!
Sammy
the following is the source code of the method:
*****************c++ function and converted python which seems work****************
c++:
int getint(uchar *&p)
{
int c = *((char *)p);
p++;
if(c==-128) { int n = *p++; n |= *((char *)p)<<8; p++; return n;}
else if(c==-127) { int n = *p++; n |= *p++<<8; n |= *p++<<16; return n|(*p++<<24); }
else return c;
};
python:
def __getint(this):
if this.p[0]==-128:
return this.p[1] | (this.p[1]<<8), 3
elif this.p[0] == -127:
return this.p[1] | (this.p[2] <<8) | (this.p[3] << 16) | (this.p[4]), 5
else:
return this.p[0], 1
************another c++ method and converted python which didn't work*******************
c++:
int getuint(uchar *&p)
{
int n = *p++;
if(n & 0x80)
{
n += (*p++ << 7) - 0x80;
if(n & (1<<14)) n += (*p++ << 14) - (1<<14);
if(n & (1<<21)) n += (*p++ << 21) - (1<<21);
if(n & (1<<28)) n |= 0xF0000000;
};
return n;
};
python:
def __getuint(this):
n = this.p[0]
s = 1
if (n & 0x80):
n += (this.p[0] << 7) - 0x80;
s += 1
if (n & (1<<14)):
n += (this.p[1] << 14) - (1<<14);
s += 1
if (n & (1<<21)):
n += (this.p[2] << 21) - (1<<21);
s += 1
if (n & (1<<28)): n |= 0xF0000000;
return n, s
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the Cplusplus-sig
mailing list