代码之家  ›  专栏  ›  技术社区  ›  Art W

C串联int

c
  •  5
  • Art W  · 技术社区  · 15 年前

    我会第一个承认,我是一个百分之百的男人,C不适合我。但是我有问题。 我需要用HashUrl(HashInt)连接7,然后用HashInt连接 任何帮助都将不胜感激。

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

    我应该说明这个的用法。它实际上将用于学生的VB6项目中。

    Private Declare Function main Lib "checksum.dll" (ByVal pStr As String) As Long
    
    Private Sub Command1_Click()
    MsgBox main("http://hello.com")
    End Sub
    

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

    “给” http://www.hello.com “应返回783544359868,但不是

    3 回复  |  直到 15 年前
        1
  •  5
  •   Tumas    15 年前

    可以使用sprintf函数创建格式化字符串。要将3个整数串联成一个字符串,可以使用

    sprintf(string, "%d%d%d", int1, int2, int3)
    
        2
  •  1
  •   masonsbro    15 年前

    我对C语言不是很在行,但我要解决这个问题的方法是首先将它们转换成字符数组,然后将它们串联起来。然后可以将结果转换回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;
    }
    

    注:此项工作不作任何保证。。。

        3
  •  0
  •   rmk    15 年前

    首先,您可以看看:

    asprintf

    就这么做吧 man asprintf 在linux系统上。

    http://www.gnu.org/s/libc/manual/html_node/Dynamic-Output.html