我有一个很长(22位)的纯数字组成的字符串,有没有办法把它压缩的短一点,也要求是纯数字组成,比如8位,要求压缩后不能改变唯一性,也就是说,如果22位的字符串不相等,那么8位的也一定不相等。
不知道有没有什么办法,请大家指点,谢谢!

解决方案 »

  1.   

    LeeZi(临渊羡鱼,不如退而结网) 
    谢谢你的建议但是这样转换,力度太小,16位的字符串转换以后还有10位,再转换还有9位,我希望转换的力度大一些,比如16可以转为8位甚至更少谢谢!
      

  2.   

    如果你使用ULARGE_INTEGER的话,
    相当于用8位保存19位字符串。
    这可能是最大的限度了。你也可以用三个LONG型来保存22位字符串。
      

  3.   

    #include "stdafx.h"
    #include <Stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <TCHAR.H>#pragma pack ( push, 1 )
    class CNumber
    {
    public:
    CNumber();
    ~CNumber();public:
    BOOL Initialize(TCHAR* sz);
    BOOL operator == (const CNumber& n);private:
    WORD mHigh;
    ULARGE_INTEGER mLow;
    };
    #pragma pack ( pop )CNumber::CNumber()
    {
    mHigh = 0;
    mLow.QuadPart = 0;
    }CNumber::~CNumber()
    {
    }BOOL CNumber::Initialize(TCHAR* sz)
    {
    if (sz != NULL)
    {
    TCHAR* pos = sz;
    LONG Len = 0;
    while (*pos != _T('\0'))
    {
    if (!::isdigit(*pos))
    return FALSE;
    Len++;
    pos++;
    }
    if (Len == 0)
    return TRUE; if (Len > 22)
    return FALSE; if (Len > 19)
    {
    pos = sz + (Len - 20);
    WORD Base = 1;
    while (pos >= sz)
    {
    mHigh += ((::toupper(*pos) - _T('0')) * Base);
    pos--;
    Base *= 10;
    }
    } {
    pos = sz + Len - 1;
    ULONGLONG Base = 1;
    while (pos >= (sz + (Len - 19)))
    {
    mLow.QuadPart += ((::toupper(*pos) - _T('0')) * Base);
    pos--;
    Base *= 10;
    }
    }
    }
    return TRUE;
    }BOOL CNumber::operator ==(const CNumber& n)
    {
    if (mHigh == n.mHigh && mLow.QuadPart == n.mLow.QuadPart)
    return TRUE;
    return FALSE;
    }int main(int argc, char* argv[])
    {
    printf("sizeof(CNumber) = %d\r\n", sizeof(CNumber)); CNumber n1;
    if (!n1.Initialize(_T("8801234567890123456789")))
    {
    } CNumber n2;
    if (!n2.Initialize(_T("1234567890123456789")))
    {
    } if (n1 == n2)
    {
    printf("n1 == n2\r\n");
    }
    else
    {
    printf("n1 != n2\r\n");
    } CNumber n3;
    if (!n3.Initialize(_T("8801234567890123456789")))
    {
    } if (n1 == n3)
    {
    printf("n1 == n3\r\n");
    }
    else
    {
    printf("n1 != n3\r\n");
    }
    return 0;
    }
      

  4.   

    如果是19位,只用ULONGLONG就可以了。8位大小。
      

  5.   

    LeeZi(临渊羡鱼,不如退而结网) 
    虽然你误解了我的要求,还是谢谢你!