C # conversion between hexadecimal data

Source: Internet
Author: User

1. Convert the decimal data into N-base data

Decimal data Value, which can be expressed as N exquisite data using the following formula.

Value = Xn * Math. pow (Item, n) + Xn-1 * Math. pow (Item, n-1) + .. + X2 * Math. pow (Item, 1) + X1 * Math. pow (Itm, 0 );

The value range of Xn is (0 <= Xn <Item), and the value range of n is (n> = 2 );

In the preceding example, we can use the following algorithm to convert the decimal data into the Item format:

Loop
IF (value> = Item)
The value at position I = value % Item;
Value = value/Item;
End IF;
ELSE
The value at position I = value;
End if;
I ++;
End Loop;

Therefore, convert the decimal data to the N-base data. The code is implemented as follows (Note: The Item should not exceed the hexadecimal value unless you modify the method in Func)
/// <Summary>
/// Convert the 10th hexadecimal form to the Item hexadecimal form
/// </Summary>
/// <Param name = "value"> </param>
/// <Param name = "item"> </param>
/// <Returns> </returns>
Protected string Pares10ToItem (double value, double item)
{
Double sub = 0;
Double pos = 0;
Double tmp = 0;
Double I = 1;
String s = "";
Do
{
If (value> = item)
{
Pos = value % item; // the value at the I position
Value = value/item;
}
Else
{
Pos = value;
Value = 0;
}
S = (this. Func (int) pos) + s;
I ++;

} While (value> 0 );

Return s;
}
/// <Summary>
/// Obtain the value at each position of the Item hexadecimal data
/// </Summary>
/// <Param name = "s"> </param>
/// <Returns> </returns>
Protected string Func (int s)
{
String result = "";
Switch (s)
{
Case 0:
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
Case 6:
Case 7:
Case 8:
Case 9:
{
Result = s. ToString ();
Break;
}
Case 10:
{
Result = "";
Break;
}
Case 11:
{
Result = "B ";
Break;
}
Case 12:
{
Result = "C ";
Break;
}
Case 13:
{
Result = "D ";
Break;
}
Case 14:
{
Result = "E ";
Break;
}
Case 15:
Default:
{
Result = "F ";
Break;
}
}
Return result;
}

Ii. Convert N-base data to decimal data

This is too simple. We use a loop to traverse the data Xn at each position of the Value, and use the cumulative sum of Xn * Math (N, n-1. You can.

 

Iii. Conversion between non-decimal data:

We can do this in two steps: the first step is to convert Value_X to the decimal data Velue_10; the second step is to convert Value_10 to the Value_N hexadecimal data.

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.