如何解决如何将整数大小和指针声明从C ++转换为Python?
我正在尝试将此C ++函数转换为Python代码,但是在执行此操作时遇到麻烦,无法将python变量分别转换为uint32_t和uint8_t。我不确定如何声明函数以使其返回uint32_t,以及如何处理指针uint8_t * buf。请帮我弄清楚如何将函数从C ++转换为python。
这是我的C ++代码:
uint32_t Functions::Do_calc(uint8_t *buf,uint32_t len){
return Do_calc(25,buf,len);
}
uint32_t Functions::Do_calc(uint32_t val,uint8_t *buf,uint32_t len){
uint32_t temp_int,c = val;
uint32_t ip_buf[128];
uint32_t j,rem = 0,tf = 0,p = 0;
rem = len;
while(rem > 0){
if(rem <= 512){
tf = rem;
}
else{
tf = 512;
}
for(j = 0; j < 128; j++){
ip_buf[j]=0;
}
for(j = 0; j < tf; j += 2){
temp = ((buf[p * 512 + (j + 3)]<<24) +
(buf[p * 512 + (j + 2)]<<16) +
(buf[p * 512 + (j + 1)]<<8) +
buf[p * 512 + j]);
ip_buf[j / 4] = temp;
}
c = c_cal(ip_buf,tf,c,0x04C22AB9,2,true);
p++
}
return c;
}
uint32_t Functions::c_cal(uint32_t *d_base,uint32_t d_size,uint32_t c,uint32_t poly,uint8_t c_size,bool b_r_ip)
{
unsigned long d_offset;
unsigned long d,d_temp;
unsigned char c_bit;
d = 0;
for(d_offset = 0; d_offset < d_size; (d_offset += c_size))
{
u32_d_temp = 0;
d_temp = d_base[d_offset/c_size];
if(FALSE == b_r_ip)
{
d = d_temp;
}
else
{
d = 0;
for(c_bit = 0; c_bit < (c_size << 3); c_bit++)
{
d <<= 1;
d |= (d_temp & 1);
d_temp >>= 1;
}
}
for(c_bit = 0; c_bit < (c_size << 3); c_bit++)
{
if(((c >> ((c_size << 3) - 1)) ^ d) & 1)
{
c <<= 1;
d >>= 1;
c ^= poly;
}
else
{
c <<= 1;
d >>= 1;
}
}
}
return (c & (0xFFFFFFFF >> (32 - (c_size << 3))));
}
这是我尝试的Python实现。如您所见,我只是做了一个基本的实现,因为我不必担心非常需要的指针和整数的大小:
def Do_calc(buf,len):
return Do_calc(0,len)
def Do_calc(val,len):
ip_buf = []
c = val
p = 0
rem = len
while rem > 0:
if rem <= 512:
tf = rem
else:
tf = 512
for j in range(128):
ip_buf[j].append = 0
for j in xrange(0,2):
temp_int = ((buffer[packet * 512 + (i + 3)] << 24) +
(buffer[packet * 512 + (i + 2)] << 16) +
(buffer[packet * 512 + (i + 1)] << 8) +
buffer[packet * 512 + i])
ip_buf[j/4] = temp
c = c_cal(ip_buf,true)
p += 1
return c
照顾完所有方面后,如何正确进行转换?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。