Convert hexadecimal to octal and octal

Source: Internet
Author: User
Tags binary to decimal

Adapted from 8 to 16

/// <Summary>
/// Convert the hexadecimal number to the hexadecimal number
/// </Summary>
/// <Param name = "convertfrom"> hexadecimal number </param>
/// <Returns> eight records </returns>
Private string convertnumber8to16 (string convertfrom ){
Try
{
String check = convertfrom. replace ("0", String. empty ). replace ("1", String. empty ). replace ("2", String. empty ). replace ("3", String. empty ). replace ("4", String. empty ). replace ("5", String. empty ). replace ("6", String. empty ). replace ("7", String. empty ). replace ("8", String. empty ). replace ("9", String. empty ). replace ("A", String. empty ). replace ("B", String. empty ). replace ("C", String. empty ). replace ("D", String. empty ). replace ("e", String. empty ). replace ("F", String. empty );

If (check. length> 0)
{
// Errorhandle
Return string. empty;
}
String convertor = string. Empty, convertto2; // number of targets convertor: Convert octal from,

Convertto2 = convertnumber16to2 (convertfrom); // convert to binary first
Convertid = convertnumber2to8 (convertto2 );
/// Convert binary to decimal
// Int64 intten = convert. toint32 (convertto2.trim (), 2 );
/// Convert the decimal number to the octal number
// Convertize = convert. tostring (intten, 8 );
Return convertings;
}
Catch (exception ex) {return "";}
}

/// <Summary>
/// Convert hexadecimal to binary
/// </Summary>
/// <Param name = "convertfrom"> hexadecimal number </param>
/// <Returns> binary number </returns>
Private string convertnumber16to2 (string convertfrom)
{
String check = convertfrom. replace ("0", String. empty ). replace ("1", String. empty ). replace ("2", String. empty ). replace ("3", String. empty ). replace ("4", String. empty ). replace ("5", String. empty ). replace ("6", String. empty ). replace ("7", String. empty ). replace ("8", String. empty ). replace ("9", String. empty ). replace ("A", String. empty ). replace ("B", String. empty ). replace ("C", String. empty ). replace ("D", String. empty ). replace ("e", String. empty ). replace ("F", String. empty );
If (check. length> 0)
{
// Errorhandle
Return string. empty;
}
String convertor = string. Empty, bit;
For (INT I = convertfrom. Length-1; I> = 0; I --)
{
Bit = convertfrom. substring (I, 1 );
Switch (BIT)
{
Case "0 ":
Convertings = "0000" + convertings;
Break;
Case "1 ":
Convertings = "0001" + convertings;
Break;

Case "2 ":
Convertings = "0010" + convertings;
Break;
Case "3 ":
Convertings = "0011" + convertings;
Break;
Case "4 ":
Convertings = "0100" + convertings;
Break;
Case "5 ":
Convertings = "0101" + convertings;
Break;
Case "6 ":
Convertings = "0110" + convertings;
Break;
Case "7 ":
Convertings = "0111" + convertings;
Break;
Case "8 ":
Convertings = "1000" + convertings;
Break;
Case "9 ":
Convertings = "1001" + convertings;
Break;
Case "":
Convertings = "1010" + convertings;
Break;
Case "B ":
Convertings = "1011" + convertings;
Break;
Case "C ":
Convertings = "1100" + convertings;
Break;
Case "D ":
Convertings = "1101" + convertings;
Break;
Case "E ":
Convertings = "1110" + convertings;
Break;
Case "F ":
Convertings = "1111" + convertings;
Break;
}
}
Return convertings;
}

/// <Summary>
/// Convert binary to octal
/// </Summary>
/// <Param name = "convertfrom"> binary number </param>
/// <Returns> eight records </returns>
Private string convertnumber2to8 (string convertfrom)
{
String check = convertfrom. Replace ("0", String. Empty). Replace ("1", String. Empty );
If (check. length> 0)
{
// Errorhandle
Return string. empty;
}
String convertor = string. Empty, bit;
For (INT I = convertfrom. Length-3; I> = 0; I-= 3)
{
Bit = convertfrom. substring (I, 3); // subtract 3 each time
Switch (BIT)
{
Case "000 ":
Convertor = "0" + convertor;
Break;
Case "001 ":
Convertor = "1" + convertor;
Break;
Case "010 ":
Convertor = "2" + convertor;
Break;
Case "011 ":
Convertor = "3" + convertor;
Break;
Case "100 ":
Convertor = "4" + convertor;
Break;
Case "101 ":
Convertor = "5" + convertor;
Break;
Case "110 ":
Convertor = "6" + convertor;
Break;
Case "111 ":
Convertor = "7" + convertor;
Break;
}
}
Int residue, temp = 0;
Residue = convertfrom. Length % 3; // returns the remainder.
String S = "";
If (residue! = 0)
{
If (residue = 1)
{
S = convertfrom. substring (0, 1 );
Temp = 1;
}

If (residue = 2)
{
S = convertfrom. substring (0, 2 );
Switch (s)
{
Case "01": temp = 1; break;
Case "10": temp = 2; break;
Case "11": temp = 3; break;

}
}
}
If (temp! = 0)
{
Convertings = temp. tostring () + convertings;
}
Return convertings;
}

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.