16-binary CString and int convert to each other

Source: Internet
Author: User

The following code is implemented under VC2008:

one, int type turn 16 into the system CString

CString inttocstringhex (int algorism)//16 Conversion
{

Vector<int> Renum;

CString str;

Reverse output
Todo
{
int ntemp = algorism%16;
ALGORISM=ALGORISM/16;
Renum.push_back (NTEMP);
}while (algorism);

The annotation section is plus 0, and if necessary you can use

while (Renum.size () < 4)
// {
Renum.push_back (0);
// }

Reverse output
for (int i = Renum.size ()-1; I >=0; i.)
{
CString strtemp;
if (renum[i]>=10 && renum[i]<=15)
{
WCHAR ch = ' A ' + renum[i]-10; VC2008 with wchar,vc6.0 should use Char
strtemp = ch;
}
Else
{
Strtemp.format (_t ("%d"), Renum[i]);
}

str = strtemp;
}
return str;
}

two, 16 into the CString int type


int Cstringhextoint (CString str)
{
int nret = 0;
int count = 1;
for (int i = str. GetLength ()-1; I >= 0; I.)
{
int nnum = 0;
Char chtest;
Chtest = str.       GetAt (i); CString generally do not have this use, but this program is not a problem
if (chtest >= ' 0 ' && chtest <= ' 9 ')
{
Nnum = chtest-' 0 ';
}
else if (chtest >= ' Á ' && chtest <= ' F ')
{
Nnum = chtest-' A ' + 10;
}
else if (chtest >= ' a ' && chtest <= ' F ')
{
Nnum = chtest-' a ' + 10;
}
Nret + = Nnum*count;
Count *= 16;

}
return nret;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.