Ich habe diese Funktion in C#:Konvertieren von C# Hash-Funktion auf C++
public static unsafe int GetMonoHash(string str) {
unsafe {
fixed (char* src = str) {
char* chPtr2 = src; // + offset;
char* chPtr3 = (chPtr2 + str.Length) - 1;
int num = 0;
while (chPtr2 < chPtr3) {
num = ((num << 5) - num) + chPtr2[0];
num = ((num << 5) - num) + chPtr2[1];
chPtr2 += 2;
}
chPtr3++;
if (chPtr2 < chPtr3) {
num = ((num << 5) - num) + chPtr2[0];
}
return num;
}
}
}
Ich versuchte es in C neu zu schreiben ++ und ich habe so weit:
int Utils::getMonoHash(std::string str)
{
char* src = &str[0];
char* chPtr2 = src; // + offset;
-> char* chPtr3 = (chPtr2 + str.length) - 1;
int num = 0;
while (chPtr2 < chPtr3) {
num = ((num << 5) - num) + chPtr2[0];
num = ((num << 5) - num) + chPtr2[1];
chPtr2 += 2;
}
chPtr3++;
if (chPtr2 < chPtr3) {
num = ((num << 5) - num) + chPtr2[0];
}
return num;
}
Diese eine Kompilierungsfehlern wirft:
C3867 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::length': non-standard syntax; use '&' to create a pointer to member
C2297 '+': illegal, right operand has type 'unsigned __int64 (__cdecl std::basic_string<char,std::char_traits<char>,std::allocator<char>>::*)(void) noexcept const'
Also änderte ich die angegebene (->
) Linie zu
char* chPtr3 = (chPtr2 + (char *)str.length) - 1;
, die mir einen neuen Übersetzungsfehler gab (nur eine diesmal, yay!):
C2440 'type cast': cannot convert from 'unsigned __int64 (__cdecl std::basic_string<char,std::char_traits<char>,std::allocator<char>>::*)(void) noexcept const' to 'char *'
Ich habe keine Ahnung, was jetzt zu tun. Ich brauche die C++ - Funktion, um die gleiche Ausgabe wie die C# eine zu haben, wenn dieselbe Eingabe wie die C# eine gegeben wird.
Was mache ich hier?
die kurze Antwort ist: C++ ist nicht C#. –