Thread: It is possible to convert a Python function in C?

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    4

    It is possible to convert a Python function in C?

    Hello!

    I have to write a small program in C for work.

    The main block of this program is a function that calculate the modbus checksum, but is written in python.

    Is prossibile to convert it in C?

    Code:
    def crc16(data, bits=8):
        crc = 0xFFFF
        for op, code in zip(data[0::2], data[1::2]):
            crc = crc ^ int(op+code, 16)
            for bit in range(0, bits):
                if (crc&0x0001)  == 0x0001:
                    crc = ((crc >> 1) ^ 0xA001)
                else:
                    crc = crc >> 1
        msb = crc >> 8
        lsb = crc & 0x00FF
        return '{:X}{:X}'.format(lsb, msb)
     
    print crc16('11010003000C')
    There is an online converter or something similar?

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    398
    Of course!

    ...but in general the one getting paid should be doing the work.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    4
    Quote Originally Posted by hamster_nz View Post
    Of course!

    ...but in general the one getting paid should be doing the work.
    Surely. I don't ask to convert it for me, I ask only if there is an online converter.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    398
    Quote Originally Posted by Bokka View Post
    Surely. I don't ask to convert it for me, I ask only if there is an online converter.
    Yes, it can be converted, but no online converter exists, but this does:

    Nuitka Downloads — Nuitka the Python Compiler documentation

    The generated code is usually of very low quality compared to actual C code.

    Here's your python module converted to C:
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    Oct 2022
    Posts
    4
    I'll take a look at it. Thanks a lot!

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    Code:
    #include <stdio.h>
     
    unsigned crc16(const char *data, int bits/*=8*/) {
        unsigned crc = 0xFFFF;
        for (int i = 0; data[i * 2] && data[i * 2 + 1]; ++i) {
            unsigned n;
            sscanf(data + i * 2, "%2x", &n);
            crc ^= n;
            for (int bit = 0; bit < bits; ++bit)
                if (crc & 0x1) crc = (crc >> 1) ^ 0xA001;
                else           crc = (crc >> 1);
        }
        return (crc >> 8) | ((crc & 0xff) << 8);
    }
     
    int main() {
        unsigned crc = crc16("11010003000C", 8);
        printf("%04X\n", crc);
        return 0;
    }
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,435
    Kinda weird thing to ask, since the Python would itself have been the result of a conversion from an older C source to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    The python even has a bug since it concatenates the two hex strings together without zero-padding to two-digits per byte.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    903
    Quote Originally Posted by john.c View Post
    The python even has a bug since it concatenates the two hex strings together without zero-padding to two-digits per byte.
    That's a feature, not a bug! (If both 42 and 402 should compare equal, that is.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-09-2022, 11:09 PM
  2. Difference between Python 2 and Python 3?
    By OMG its Ali in forum Tech Board
    Replies: 3
    Last Post: 03-18-2014, 08:06 PM
  3. seg fault in python c wrapper function
    By Karen Lipa in forum C Programming
    Replies: 1
    Last Post: 06-14-2012, 10:21 AM
  4. Function to convert an int to hex
    By o.fithcheallaig in forum C Programming
    Replies: 2
    Last Post: 12-16-2011, 03:12 PM
  5. need function to convert int to hex
    By sonnichs in forum C Programming
    Replies: 17
    Last Post: 08-11-2011, 07:26 PM

Tags for this Thread