C # string and byte [] conversion help class-. NET tutorial, C # Language

Source: Internet
Author: User


Mainly implements the following functions:

The sidle in the Code is my network name.

/**//*
* @ Author wuerping
* @ Version 1.0
* @ Date 2004/11/30
* @ Description:
*/
Using system;
Using system. text;
Namespace sidlehelper
{
/** // <Summary>
/// Summary description for strhelper.
/// Abbreviation:
// Str: unicode string
/// Arr: unicode array
/// Hex: binary data
/// Hexbin: the binary data uses ascii characters to indicate that hex of Case 1 is 0x31, and hexbin is 31.
/// Asc: ascii
/// Uni: unicode
/// </Summary>
Public sealed class strhelper
{
Conversion between hex and hexbin # conversion between region hex and hexbin
Public static void hexbin2hex (byte [] bhexbin, byte [] bhex, int nlen)
{
For (int I = 0; I <nlen/2; I ++)
{
If (bhexbin [2 * I] <0x41)
{
Bhex [I] = convert. tobyte (bhexbin [2 * I]-0x30) <4) & 0xf0 );
}
Else
{
Bhex [I] = convert. tobyte (bhexbin [2 * I]-0x37) <4) & 0xf0 );
}

If (bhexbin [2 * I + 1] <0x41)
{
Bhex [I] | = convert. tobyte (bhexbin [2 * I + 1]-0x30) & 0x0f );
}
Else
{
Bhex [I] | = convert. tobyte (bhexbin [2 * I + 1]-0x37) & 0x0f );
}
}
}
Public static byte [] hexbin2hex (byte [] bhexbin, int nlen)
{
If (nlen % 2! = 0)
Return null;
Byte [] bhex = new byte [nlen/2];
Hexbin2hex (bhexbin, bhex, nlen );
Return bhex;
}
Public static void hex2hexbin (byte [] bhex, byte [] bhexbin, int nlen)
{
Byte c;
For (int I = 0; I <nlen; I ++)
{
C = convert. tobyte (bhex [I]> 4) & 0x0f );
If (c <0x0a)
{
Bhexbin [2 * I] = convert. tobyte (c + 0x30 );
}
Else
{
Bhexbin [2 * I] = convert. tobyte (c + 0x37 );
}
C = convert. tobyte (bhex [I] & 0x0f );
If (c <0x0a)
{
Bhexbin [2 * I + 1] = convert. tobyte (c + 0x30 );
}
Else
{
Bhexbin [2 * I + 1] = convert. tobyte (c + 0x37 );
}
}
}
Public static byte [] hex2hexbin (byte [] bhex, int nlen)
{
Byte [] bhexbin = new byte [nlen * 2];
Hex2hexbin (bhex, bhexbin, nlen );
Return bhexbin;
}
# Endregion

Conversion between arrays and strings # conversion between region arrays and strings
Public static byte [] str2arr (string s)
{
Return (new unicodeencoding (). getbytes (s );
}
Public static string arr2str (byte [] buffer)
{
Return (new unicodeencoding (). getstring (buffer, 0, buffer. length );
}

Public static byte [] str2ascarr (string s)
{
Return system. text. unicodeencoding. convert (system. text. encoding. unicode,
System. text. encoding. ascii,
Str2arr (s ));
}

Public static byte [] str2hexascarr (string s)
{
Byte [] hex = str2ascarr (s );
Byte [] hexbin = hex2hexbin (hex, hex. length );
Return hexbin;
}
Public static string ascarr2str (byte [] B)
{
Return system. text. unicodeencoding. unicode. getstring (
System. text. asciiencoding. convert (system. text. encoding. ascii,
System. text. encoding. unicode,
B)
);
}

Public static string hexascarr2str (byte [] buffer)
{
Byte [] B = hex2hexbin (buffer, buffer. length );
Return ascarr2str (B );
}
# Endregion
}
}

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.