2010-10-10 10 views
5

Sarò il primo ad ammettere, sono un ragazzo C# 100% e C non è per me. Comunque ho un problema. Ho bisogno di concatenare 7 con HashUrl (HashInt) e poi con HashInt Qualsiasi aiuto sarebbe molto apprezzato.C Concatenate int

int main(int argc) 
{ 
unsigned int HashInt; 
HashInt = HashURL(argc); 
// I need to return 7 + CheckHash(HashInt) + HashInt but not ADDING, but concanenating them 
return HOWEVERTOGETTHESTRING; 
} 

Avrei dovuto specificare l'utilizzo di questo. In realtà verrà utilizzato in un progetto VB6 per studenti.

Private Declare Function main Lib "checksum.dll" (ByVal pStr As String) As Long 

Private Sub Command1_Click() 
MsgBox main("http://hello.com") 
End Sub 

La sorgente completo per la libreria C è

#include <stdio.h> 
#include <string.h> 
#include <windows.h> 
#include <string.h> 
#include <winreg.h> 
#include <stdlib.h> 

int StrToInt(char *pStr, int Init, int Factor) 
{ 
while (*pStr) { 
Init *= Factor; 
Init += *pStr++; 
} 
return Init; 
} 

int HashURL(char *pStr) 
{ 
unsigned int C1, C2, T1, T2; 

C1 = StrToInt(pStr, 0x1505, 0x21); 
C2 = StrToInt(pStr, 0, 0x1003F); 
C1 >>= 2; 
C1 = ((C1 >> 4) & 0x3FFFFC0) | (C1 & 0x3F); 
C1 = ((C1 >> 4) & 0x3FFC00) | (C1 & 0x3FF); 
C1 = ((C1 >> 4) & 0x3C000) | (C1 & 0x3FFF); 

T1 = (C1 & 0x3C0) << 4; 
T1 |= C1 & 0x3C; 
T1 = (T1 << 2) | (C2 & 0xF0F); 

T2 = (C1 & 0xFFFFC000) << 4; 
T2 |= C1 & 0x3C00; 
T2 = (T2 << 0xA) | (C2 & 0xF0F0000); 

return (T1 | T2); 
} 

char CheckHash(unsigned int HashInt) 
{ 
int Check = 0, Flag = 0; 
int Remainder; 

do { 
Remainder = HashInt % 10; 
HashInt /= 10; 
if (1 == (Flag % 2)){ 
Remainder += Remainder; 
Remainder = (Remainder/10) + (Remainder % 10); 
} 
Check += Remainder; 
Flag ++; 
} while(0 != HashInt); 

Check %= 10; 
if (0 != Check) { 
Check = 10 - Check; 
if (1 == (Flag % 2)) { 
if (1 == (Check % 2)) { 
Check += 9; 
} 
Check >>= 1; 
} 
} 
Check += 0x30; 
return Check; 
} 

int main(int argc) 
{ 
unsigned int HashInt; 
    int result; 
    HashInt = HashURL(argc); 
    char temp[100]; 
    sprintf(temp, "7%i%j", CheckHash(HashInt), HashInt); 
    result = atoi(temp); 
    return result; 
} 

dare "http://www.hello.com" dovrebbe tornare 783.544.359.868 ma la sua non

+0

Hai diversi problemi qui; uno di questi è che la tua funzione sta prendendo un int, non una stringa. Inoltre, 'main' è una scelta sbagliata per un nome di funzione se lo si sta esportando in una DLL. – You

risposta

5

È possibile utilizzare la funzione sprintf per creare stringhe formattate. Per concatenare 3 interi in una stringa si potrebbe usare qualcosa come

sprintf(string, "%d%d%d", int1, int2, int3) 
1

Io non sono un gran che in C, ma come faccio a risolvere questo problema è quello di trasformarli in array di caratteri, poi concatenare quelli. Poi si potrebbe trasformare il risultato in un int:


int main(int argc) { 
    unsigned int HashInt; 
    int result; 
    HashInt = HashURL(argc); 
    char temp[50]; 
    sprintf(temp, "7%i%i", CheckHash(HashInt), HashInt); 
    result = atoi(temp); 
    return result; 
} 

Nota: Non ci sono garanzie in questo lavoro ...