473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Function from C/PHP to Python

Hello everyone :)
I have one problem with that function in C

int calc_passcode(const char* pass, char* code) {
int magic1 = 0x50305735;
int magic2 = 0x12345671;
int sum = 7;
char z;
while ((z = *pass++) != 0) {
if (z == ' ') continue;
if (z == '\t') continue;
magic1 ^= (((magic1 & 0x3f) + sum) * z) + (magic1 << 8);
magic2 += (magic2 << 8) ^ magic1;
sum += z;
}
magic1 &= 0x7fffffff;
magic2 &= 0x7fffffff;
return sprintf(code, "%08x%08x", magic1, magic2);
} // end _calc_passcode();

Can someone help me to rewrite it to python?

There is the same function, in PHP:

function _calc_passcode($pass) {

$magic1 = 0x50305735;
$magic2 = 0x12345671;
$sum = 7;
for ($i = 0; $i < strlen($pass); $i++) {
$z = ord($pass[$i]);
if ($z == 32)
continue;
if ($z == 9)
continue;
$magic1 = $magic1 ^ (((($magic1 & 0x3f) + $sum) * $z) + ($magic1 <<
8));
$magic2 = $magic2 + (($magic2 << 8) ^ $magic1);
$sum += $z;
$magic1 = $magic1 & 0x7fffffff;
$magic2 = $magic2 & 0x7fffffff;
}
return sprintf('%08x%08x', $magic1, $magic2);

} // end _calc_passcode();

Please... its very important to me
Aug 7 '08 #1
2 1156
On Wed, 06 Aug 2008 20:15:11 -0700, Grom wrote:
Hello everyone :)
I have one problem with that function in C
....
Can someone help me to rewrite it to python?

There is the same function, in PHP:
....
Please... its very important to me
I don't know C or PHP. But since it's important, and because you said
please, let me try:
def calc_passcode(password):
magic1 = 0x50305735
magic2 = 0x12345671
sum = 7
for i in range(len(password)):
z = ord(password[i])
if z == 32 or z == 9:
continue
magic1 = magic1 ^ ((((magic1 & 0x3f) + sum) * z) + (magic1 << 8))
magic2 = magic2 + ((magic2 << 8) ^ magic1)
sum += z
magic1 = magic1 & 0x7fffffff
magic2 = magic2 & 0x7fffffff
return '%08x%08x' % (magic1, magic2)

--
Steven
Aug 7 '08 #2
On 7 Sie, 06:01, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Wed, 06 Aug 2008 20:15:11 -0700, Grom wrote:
Hello everyone :)
I have one problem with that function in C
...
Can someone help me to rewrite it to python?
There is the same function, in PHP:
...
Please... its very important to me

I don't know C or PHP. But since it's important, and because you said
please, let me try:

def calc_passcode(password):
* * magic1 = 0x50305735
* * magic2 = 0x12345671
* * sum = 7
* * for i in range(len(password)):
* * * * z = ord(password[i])
* * * * if z == 32 or z == 9:
* * * * * * continue
* * * * magic1 = magic1 ^ ((((magic1 & 0x3f) + sum) * z) + (magic1 << 8))
* * * * magic2 = magic2 + ((magic2 << 8) ^ magic1)
* * * * sum += z
* * * * magic1 = magic1 & 0x7fffffff
* * * * magic2 = magic2 & 0x7fffffff
* * return '%08x%08x' % (magic1, magic2)

--
Steven
It works, thanks :)
Aug 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
3
by: Hugh Macdonald | last post by:
I've got a pure python module that parses a certain type of file. It has a load() function that allows a callback function to be passed for getting progress information. In straight python, this...
2
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
38
by: Lasse Vågsæther Karlsen | last post by:
After working through a fair number of the challenges at www.mathschallenge.net, I noticed that some long-running functions can be helped *a lot* by caching their function results and retrieving...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
5
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.