Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Concatenate int

Tags:

c

I will be the first to admit, I'm a C# guy 100% and C isn't for me. However I have problem. I need to concatenate 7 with HashUrl(HashInt) and then with HashInt Any help would be greatly appreciated.

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;
}

I should have specified the usage of this. It's actually going to be used in a students VB6 project.

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

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

The full source for the C library is

#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;
}

give " http://www.hello.com" should return 783544359868 but its not

like image 294
Art W Avatar asked Oct 10 '10 23:10

Art W


People also ask

Can you append int?

If you want to append any number of digits, multiply the int by pow(10, log10(yourDigits) + 1) .

How do you add an int to a string?

To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.

How do you concatenate numbers?

To combine numbers, use the CONCATENATE or CONCAT, TEXT or TEXTJOIN functions, and the ampersand (&) operator.

How do I concatenate a number to a string in C++?

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.


1 Answers

You can use sprintf function to create formatted strings. To concatenate 3 integers into a string you could use something like

sprintf(string, "%d%d%d", int1, int2, int3)
like image 171
Tumas Avatar answered Sep 29 '22 23:09

Tumas