Data Type Conversion

Source: Internet
Author: User
Tags array to string

/// <Summary>
/// Convert the UInt32 type value of the special column to the specified format
/// </Summary>
/// <Param name = "formatStyle">
/// Format parameters:
/// Yyyy indicates the year of the date (4 digits); yy indicates the year of the date (2 digits );
/// M indicates the month of the date (automatic); mm indicates the month of the date (two digits );
/// D indicates the day of the date (automatic); dd indicates the day of the date (two digits );
/// P1 indicates the first part of the IP address; p2 indicates the second part of the IP address; p3 indicates the third part of the IP address; p4 indicates the fourth part of the IP address;
/// V1 indicates the first part of the version number; v2 indicates Part 2 of the version number; [Version Number Format: Primary version number. Minor version number]
/// Vs1 indicates the first part of the version number; vs2 indicates part 2nd of the version number; vs3 indicates part 3rd of the version number; vs4 indicates part 4th of the version number; [Version Number Format: x. x. x. x]
/// </Param>
/// <Returns> </returns>
Public static string UInt32ToString (UInt32 intValue, string formatStyle)
{
// Convert to the hexadecimal table Value
String valueString = intValue. ToString ("X ");
String strReturn = formatStyle. ToUpper ();
// Format
String strTemp = "00000000 ";
StrTemp = strTemp. Remove (0, valueString. Length) + valueString;
String strPart1 = strTemp. Substring (0, 2 );
String strPart2 = strTemp. Substring (2, 2 );
String strPart3 = strTemp. Substring (4, 2 );
String strPart4 = strTemp. Substring (6, 2 );
// Date-year Processing
If (strReturn. IndexOf ("YYYY ")! =-1)
{
StrReturn = strReturn. Replace ("YYYY", int. Parse (strPart1 + strPart2, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("YY ")! =-1)
{
StrReturn = strReturn. Replace ("YY", (int. Parse (strPart1 + strPart2, System. Globalization. NumberStyles. AllowHexSpecifier) % 100). ToString ());
}
// Processing of the date month
If (strReturn. IndexOf ("MM ")! =-1)
{
Int mm = int. Parse (strPart3, System. Globalization. NumberStyles. AllowHexSpecifier );
StrReturn = strReturn. Replace ("MM", (mm> 10? Mm. ToString (): "0" + mm. ToString ())));
}
If (strReturn. IndexOf ("M ")! =-1)
{
StrReturn = strReturn. Replace ("M", int. Parse (strPart3, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
// Processing date and day
If (strReturn. IndexOf ("DD ")! =-1)
{
Int dd = int. Parse (strPart4, System. Globalization. NumberStyles. AllowHexSpecifier );
StrReturn = strReturn. Replace ("DD", (dd> 10? Dd. ToString (): "0" + dd. ToString ())));
}
If (strReturn. IndexOf ("D ")! =-1)
{
StrReturn = strReturn. Replace ("D", int. Parse (strPart4, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
// Ip address Processing
If (strReturn. IndexOf ("P1 ")! =-1)
{
StrReturn = strReturn. Replace ("P1", int. Parse (strPart1, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("P2 ")! =-1)
{
StrReturn = strReturn. Replace ("P2", int. Parse (strPart2, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("P3 ")! =-1)
{
StrReturn = strReturn. Replace ("P3", int. Parse (strPart1, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("P4 ")! =-1)
{
StrReturn = strReturn. Replace ("P4", int. Parse (strPart1, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
// Process the version number [major version number. Minor version number]
If (strReturn. IndexOf ("V1 ")! =-1)
{
StrReturn = strReturn. Replace ("V1", int. Parse (strPart1 + strPart2, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("V2 ")! =-1)
{
StrReturn = strReturn. Replace ("V2", int. Parse (strPart3 + strPart4, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
// Process the version number [X. X]
If (strReturn. IndexOf ("VS1 ")! =-1)
{
StrReturn = strReturn. Replace ("VS1", int. Parse (strPart1, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("VS2 ")! =-1)
{
StrReturn = strReturn. Replace ("VS2", int. Parse (strPart2, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("VS3 ")! =-1)
{
StrReturn = strReturn. Replace ("VS3", int. Parse (strPart1, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
If (strReturn. IndexOf ("VS4 ")! =-1)
{
StrReturn = strReturn. Replace ("VS4", int. Parse (strPart1, System. Globalization. NumberStyles. AllowHexSpecifier). ToString ());
}
Return strReturn;
}

/// <Summary>
/// Convert character arrays to strings
/// </Summary>
/// <Param name = "charValue"> character array </param>
/// <Returns> string </returns>
Public static string ByteArrayToString (byte [] byteArray)
{
// String result = Encoding. GetEncoding ("gb2312"). GetString (byteArray );
// String result = Encoding. GetEncoding (936). GetString (byteArray );
String result = pEncoding. GetString (byteArray );
Return result;
}

/// <Summary>
/// Character array to string [Only applicable to character array objects with a Data Length of 16]
/// </Summary>
/// <Param name = "charValue"> character array </param>
/// <Param name = "formatStyle"> string format [case insensitive] IP1: The first part of the IP address; IP2: the second part of the IP address; IP3: the third part of the IP address; IP4: part 4 of the IP address </param>
/// <Returns> string </returns>
Public static string ByteArrayToString (byte [] byteArray, string formatStyle)
{
// String result = Encoding. GetEncoding ("gb2312"). GetString (byteArray );
// String result = Encoding. GetEncoding (936). GetString (byteArray );
String result = pEncoding. GetString (byteArray );
If (result. Length = 16)
{
String sPart1 = result. Substring (0, 4 );
String sPart2 = result. Substring (4, 4 );
String sPart3 = result. Substring (8, 4 );
String sPart4 = result. Substring (12, 4 );
String strTemp = formatStyle. ToUpper ();
// Ip address format processing
If (strTemp. IndexOf ("IP1 ")! =-1)
{
StrTemp = strTemp. Replace ("IP1", int. Parse (sPart1). ToString ());
}
If (strTemp. IndexOf ("IP2 ")! =-1)
{
StrTemp = strTemp. Replace ("IP2", int. Parse (sPart2). ToString ());
}
If (strTemp. IndexOf ("IP3 ")! =-1)
{
StrTemp = strTemp. Replace ("IP3", int. Parse (sPart3). ToString ());
}
If (strTemp. IndexOf ("IP4 ")! =-1)
{
StrTemp = strTemp. Replace ("IP4", int. Parse (sPart4). ToString ());
}
Result = strTemp;
}
Return result;
}


/// <Summary>
/// Convert a string to a character array
/// </Summary>
/// <Param name = "strValue"> </param>
/// <Returns> </returns>
Public static bool StringToByteArry (string strValue, ref byte [] byteArry)
{
Try
{
// Byte [] byteTemp = Encoding. GetEncoding ("gb2312"). GetBytes (strValue );
Byte [] byteTemp = pEncoding. GetBytes (strValue );
Int maxLen = (byteTemp. Length> byteArry. Length? ByteArry. Length: byteTemp. Length );
For (int I = 0; I <byteArry. Length; I ++)
{
If (I <maxLen)
{
ByteArry [I] = byteTemp [I];
}
Else
{
ByteArry [I] = new byte ();
}
}
Return true;
}
Catch
{
Return false;
}
}

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.