Hi there, i need to use this function but it is write in Python and if is possible i dont want to start learning Python just for one function so i wanted to ask if someoen can direct me to Pythong to C# convertor or help me convert this function.

from ctypes import c_uint32, c_int32

unid = "|HItem:2,1565456761:-146123031:1273837477,-2012337763,1951515674,-334812892:-1:0:130756:9:8:8:523:523:0:0:6:0:-648496672:|h[Galraki]|h"

def hashString(s):
h = 0
for c in s:
    h = c_uint32((h * 0x21)  + ord(c)).value
return h

def IDLink(s):
parts = s.split(":")

affixes = parts[3].split(',')
affixes.reverse()
parts[3] = ','.join(affixes)

parts[9] = str(int(parts[9]) | 0x1)
hash_input = ':'.join(parts[1:-2]) + ':'
link_hash = c_int32(hashString(hash_input)).value
parts[-2] = str(link_hash)

id = ':'.join(parts)
return id   

print IDLink(unid)

Thanks for all the help in advance!

Recommended Answers

All 3 Replies

Iron Python is Python for .Net. I'm not sure if it's what you're looking for.

I don't know any C# (or any other variant of C for that matter.) This has me intrigued, so I might take a stab at writing it in C# if I have time later today.

But, for now, here's the original code with each line commented. Hopefully it'll explain the Python code well enough that it'll make it easier for you to write these two functions in C#. I've explained to the best of my knowledge. Though there could very well be errors in my explanation and/or understanding. If so, hopefully someone can correct me.

# imports the c_unit32 and c_int32 modules from ctypes
# ctypes: http://docs.python.org/library/ctypes.html
from ctypes import c_uint32, c_int32

# Declares variable unid as a string
unid = "|HItem:2,1565456761:-146123031:1273837477,-2012337763,1951515674,-334812892:-1:0:130756:9:8:8:523:523:0:0:6:0:-648496672:|h[Galraki]|h"

# def function(args): declares a function and it's input in Python
def hashString(s):

    # Declares variable h equals 0
    h = 0

    # For each character in s
    for c in s:

        # Sets h to equal the value of a C unsigned integer of h * 0x21(33) + ord 
        # Below is how it would look for the first character passed by IDLink:
        # h = c_uint32( (h * 0x21) + ord('2') ).value
        # h = c_uint32( (0 * 0x21) + 50 ).value
        # h = c_uint32( (0) + 50) ).value
        # h = 50L
        h = c_uint32((h * 0x21) + ord(c)).value

    # Return the final value of h
    return h


def IDLink(s):

    # unid was passed as s

    # Splits the string into a list with ":" as the delimiter
    #parts = ['|HItem', '2,1565456761', '-146123031', '1273837477,-2012337763,1951515674,-334812892', '-1', '0', '130756', '9', '8', '8', '523', '523', '0', '0', '6', '0', '-648496672', '|h[Galraki]|h']
    parts = s.split(":")

    # Splits 3rd index of parts into a list with "," as the delimiter
    # affixes = ['1273837477', '-2012337763', '1951515674', '-334812892']
    affixes = parts[3].split(',')

    # Reversed the list 'affixes'
    # affixes = ['-334812892', '1951515674', '-2012337763', '1273837477']
    affixes.reverse()

    # "','.join(affixes)" combines the affixes list (now reversed from it's original form) into a string with ',' in between each of the indices.
    # "parts[3] =" sets the 3rd index of the list 'parts'
    # parts = ['|HItem', '2,1565456761', '-146123031', '-334812892,1951515674,-2012337763,1273837477', '-1', '0', '130756', '9', '8', '8', '523', '523', '0', '0', '6', '0', '-648496672', '|h[Galraki]|h']
    parts[3] = ','.join(affixes)

    # I'm a bit fuzzy on this next bit, I'm pretty sure I understand it now.
    # Sets 9th index of parts; str converts it to a string; int(parts[9]) converts the 9th index to an integer (it's an 8 in this case)
    # 8 | 0x1 is a bitwise operator; 8 | 0x1 Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1. 
    # Bitwise Operators: http://wiki.python.org/moin/BitwiseOperators
    # 8 = 1000 0x1 = 0001; the output would be 1001 == 9; parts[9] now equals '9'
    parts[9] = str(int(parts[9]) | 0x1)

    # Joins the 1st index through to the 3rd from last index with ':' in between and adds a ':' at the end
    # hash_input = '2,1565456761:-146123031:-334812892,1951515674,-2012337763,1273837477:-1:0:130756:9:8:9:523:523:0:0:6:0:'
    hash_input = ':'.join(parts[1:-2]) + ':'

    # Sets link_hash to the value of a C signed int data type of the output of the function hashString with hash_input as an argument.
    # link_hash = 1941137377
    link_hash = c_int32(hashString(hash_input)).value

    # sets 2nd to last index in parts to a string of link_hash
    # parts[-2] = '1941137377'
    parts[-2] = str(link_hash)

    # sets id as a string of all the indices of parts with ':' in between them
    # id = '|HItem:2,1565456761:-146123031:-334812892,1951515674,-2012337763,1273837477:-1:0:130756:9:8:9:523:523:0:0:6:0:1941137377:|h[Galraki]|h'
    id = ':'.join(parts)

    # Returns id variable
    return id

# Prints the output of IDLink with variable unid as the argument
print IDLink(unid)
commented: nice help +13
> # "parts[3] =" sets the 3rd index of the list 'parts'

4th index parts[3] ie parts at index 3.

> # 8 = 1000 0x1 = 0001; the output would be 1001 == 9; parts[9] now equals '9'

No int by itself turns number in decimal not binary

commented: Thanks for the feedback. +1

Thanks for the feedback pyTony.

I'm pretty sure I've got this figured out in C#. This produces the same output as the Python script. It compiles and runs without error. I used the gmcs compiler that's part of mono. I don't know if adjustments need to be made for it to work with Visual Studio or any other compiler. I welcome any feedback, especially given my inexperience in C#.

using System;

class linkHash {

    // Declare unid variable as a string
    static string unid = "|HItem:2,1565456761:-146123031:1273837477,-2012337763,1951515674,-334812892:-1:0:130756:9:8:8:523:523:0:0:6:0:-648496672:|h[Galraki]|h";

    // Main function to run IDLink
    static void Main() {

        Console.WriteLine(IDLink(unid));

    }


    static uint hashString(string hashInput) {

        // Declare hashOut as long int = 0
        long hashTmp = 0;

        // Loop through all charaters of hashInput
        foreach (char c in hashInput) {

            // get Unicode codepoint for c
            int codePoint = c;

            hashTmp = ( (hashTmp * 0x21) + codePoint);

        }   

        // Convert output from long to uint
        uint hashOutput = (uint)hashTmp;

        // Return hash of string
        return hashOutput;

    }

    static string IDLink(string inputString) {

        // Split inputString into a list with ':' as the delimiter.
        string[] parts = inputString.Split(':');

        // Split 4th index of parts into a list with ',' as delimiter.
        string[] affixes = parts[3].Split(',');

        // Reverse affixes
        Array.Reverse(affixes);

        // Rejoin the 4th index into a single string with ',' between the indices.
        parts[3] = String.Join(",", affixes);

        // Convert parts[9] to an int
        int intPart;
        int.TryParse(parts[9], out intPart);

        // Perform bitwise operation parts[9] | 0x1
        int tmpBitwise = intPart | 0x1;

        // Convert the result of the bitwise operation to a string and set parts[9] to equal it.
        parts[9] = tmpBitwise.ToString();

        // Join the 2nd through 3rd to last index together with ":" in between and add ":" at the end.
        // I couldn't seem to find anything similar to Pythons parts[1:-2].  So this loop may very well
        // be quite ugly.  I get the feeling that there's a better way to do this bit, and I'd really like
        // to hear it if anyone knows.  
        string hash_Input = "";
        int loopEnd = parts.Length - 2;
        for (int i = 1; i < loopEnd; i++) {
            hash_Input += parts[i] + ":";
        }

        // Call hashString with hash_Input as the argument.
        // convert output to signed integer.
        int linkHash = (int)hashString(hash_Input);

        // Set 2nd to last index of parts to equal a string of linkHash
        parts[parts.Length -2] = linkHash.ToString();

        // Declare string id and set it to equal the elements of parts
        // Joined with a ":" in between them
        string id = String.Join(":", parts);

        // Return id
        return id;

    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.