2, 8, and 16 hexadecimal to 10 hexadecimal output

Source: Internet
Author: User
# Include <iostream. h>

Int Len (0 );
// Check the number of hexadecimal values
Int check (char B [100])
{
For (INT I = 0; I <10; I ++)
{
If (B [I] = '\ 0 ')
{
Len = I;
Break;
}
}
If (B [0] = '0 ')
{
If (B [1] = 'X ')
{
If (B [len-1] = 'B ')
Return 2;
Else if (B [len-1] = 'H ')
Return 16;
}
Else
Return 8;
}
Else
{
Return 10;
}
}
// Convert hexadecimal letters into numbers
Int hexcode (char shex)
{

If (shex> = '0' & shex <= '9 ')
Return shex-'0 ';
Else if (shex> = 'A' & shex <= 'F ')
Return shex-'A' + 10;
Else if (shex> = 'A' & shex <= 'F ')
Return shex-'A' + 10;
Else
Return 0;
}
// Conversion Function
Void conver (char STR [10], int step)
{
Int sum (0), S (1), Max, min;
Bool hex = false;

Switch (STEP)
{
Case 16:
Hex = true;
Case 2:
Max = len-2;
Min = 2;
Break;
Case 8:
Max = len-1;
Min = 1;
Break;
Case 10:
For (Int J = 0; j <Len; j ++)
Cout <STR [J];
Cout <Endl;
Return;
}
For (INT I = max; I> = min; I --)
{
If (HEX)
Sum + = hexcode (STR [I]) * s;
Else
Sum + = (STR [I]-48) * s;
S * = step;
}

Cout <sum <Endl;
}
Void main ()
{
Char STR [100];

Cin. Getline (STR, 100 );

Switch (check (STR ))
{
Case 2:
Conver (STR, 2 );
Break;
Case 8:
Conver (STR, 8 );
Break;
Case 10:
Conver (STR, 10 );
Break;
Case 16:
Conver (STR, 16 );
Break;
}
}

Rules

123 // decimal
0xab2h // hexadecimal
0x1110b // binary
04234 // octal

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.