///
/// 十六进制字符串转换成字节数组
///
///
public
static
byte
[] HexStringToByteArray(
string
hexString)
{
hexString = hexString.Replace(
" "
,
""
).Replace(
"\n"
,
""
);
if
((hexString.Length % 2) != 0)
hexString +=
" "
;
byte
[] returnBytes =
new
byte
[hexString.Length / 2];
for
(
int
i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return
returnBytes;
}
///
/// 将内容全部为16进制数字的字符串按照一定规律对齐
///
///
public
static
string
TileHexString(
string
hexStr)
{
hexStr = hexStr.Replace(
" "
,
""
).Trim();
if
(hexStr.Length % 2 == 1) hexStr = hexStr +
" "
;
StringBuilder sb =
new
StringBuilder();
for
(
int
i = 0; i < hexStr.Length; i++)
{
if
(i % 2 == 0) sb.Append(hexStr.Substring(i, 2) +
" "
);
}
return
sb.ToString().Trim().ToUpper();
}
public
static
string
TileHexString(
byte
[] Byte,
int
Offset,
int
Length)
{
StringBuilder builder =
new
StringBuilder();
for
(
int
i = Offset; i < (Offset + Length); i++)
{
builder.AppendFormat(
"{0:x2} "
, Byte[i]);
}
return
builder.ToString().ToUpper();
}
public
static
string
TileHexString(
byte
[] Byte)
{
return
TileHexString(Byte,0,Byte.Length);
}
/// 一个随机产生的密钥字节数组
///
///
public
static
byte
[] RandomKey()
{
byte
[] key =
new
byte
[16];
Random.NextBytes(key);
return
key;
}
/// 一个随机产生的密钥字节数组
///
///
public
static
byte
[] RandomKey(
int
length)
{
byte
[] key =
new
byte
[length];
Random.NextBytes(key);
return
key;
}
#region GetString
///
/// 根据某种编码方式将字节数组转换成字符串
///
/// 如果encoding不支持,返回一个缺省编码的字符串
public
static
string
GetString(
byte
[] b,
string
encoding)
{
try
{
return
Encoding.GetEncoding(encoding).GetString(b);
}
catch
{
return
Encoding.Default.GetString(b);
}
}
///
/// 根据缺省编码将字节数组转换成字符串
///
/// 字符串
public
static
string
GetString(
byte
[] b)
{
return
GetString(b,
"UTF-8"
);
}
///
/// * 从buf的当前位置解析出一个字符串,直到碰到了buf的结尾
/// * 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
/// * 后,buf当前位置将位于buf最后之后
/// *
/// * 返回的字符串将使用QQ缺省编码,一般来说就是GBK编码
///
///
public
static
string
GetString(Lingchen.Common.Utils.ByteBuffer buf)
{
ByteBuffer temp =
new
ByteBuffer();
while
(buf.HasRemaining())
{
temp.Put(buf.Get());
}
return
GetString(temp.ToByteArray());
}
/// 从buf的当前位置解析出一个字符串,直到碰到了buf的结尾或者读取了len个byte之后停止
/// 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
/// * 后,buf当前位置将位于len字节之后或者最后之后
///
///
public
static
string
GetString(ByteBuffer buf,
int
len)
{
ByteBuffer temp =
new
ByteBuffer();
while
(buf.HasRemaining() && len-- > 0)
{
temp.Put(buf.Get());
}
return
GetString(temp.ToByteArray());
}
///
/// * 从buf的当前位置解析出一个字符串,直到碰到了delimit或者读取了maxLen个byte或者
/// * 碰到结尾之后停止
/// *此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
/// *后,buf当前位置将位于maxLen之后
///
///
public
static
String GetString(ByteBuffer buf,
byte
delimit,
int
maxLen)
{
ByteBuffer temp =
new
ByteBuffer();
while
(buf.HasRemaining() && maxLen-- > 0)
{
byte
b = buf.Get();
if
(b == delimit)
break
;
else
temp.Put(b);
}
while
(buf.HasRemaining() && maxLen-- > 0)
buf.Get();
return
GetString(temp.ToByteArray());
}
/// 根据某种编码方式将字节数组转换成字符串
///
///
public
static
string
GetString(
byte
[] b,
int
offset,
int
len)
{
byte
[] temp =
new
byte
[len];
Array.Copy(b, offset, temp, 0, len);
return
GetString(temp);
}
///
/// 从buf的当前位置解析出一个字符串,直到碰到一个分隔符为止,或者到了buf的结尾
/// 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
/// * 后,buf当前位置将位于分隔符之后
///
///
public
static
string
GetString(ByteBuffer buf,
byte
delimit)
{
ByteBuffer temp =
new
ByteBuffer();
while
(buf.HasRemaining())
{
byte
b = buf.Get();
if
(b == delimit)
return
GetString(temp.ToByteArray());
else
buf.Put(b);
}
return
GetString(temp.ToByteArray());
}
#endregion
#region GetSequence
public
static
byte
[] GetSequence(
int
lenth)
{
byte
[] bs =
new
byte
[lenth];
new
Random().NextBytes(bs);
return
bs;
}
///
/// 生成一个2字节的随机Sequence
///
///
public
static
byte
[] GetSequence()
{
return
GetSequence(2);
}
#endregion
/// 判断IP是否全0
///
///
public
static
bool
IsIPZero(
byte
[] ip)
{
for
(
int
i = 0; i < ip.Length; i++)
{
if
(ip[i] != 0)
return
false
;
}
return
true
;
}
/// ip的字节数组形式转为字符串形式的ip
///
///
public
static
String IPStringFromByteArray(
byte
[] ip)
{
StringBuilder sb =
new
StringBuilder();
sb.Append(ip[0] & 0xFF);
sb.Append(
'.'
);
sb.Append(ip[1] & 0xFF);
sb.Append(
'.'
);
sb.Append(ip[2] & 0xFF);
sb.Append(
'.'
);
sb.Append(ip[3] & 0xFF);
return
sb.ToString();
}
///
/// 将IP字符串转换成字节
///
///
public
static
byte
[] IPStringToByteArray(
string
ip)
{
byte
[] bytes =
new
byte
[4];
string
[] s = ip.Split(
'.'
);
if
(s.Length == 4)
{
for
(
int
i = 0; i < 4; i++)
{
bytes[i] = (
byte
)
int
.Parse(s[i]);
}
}
return
bytes;
}
///
/// 将昵称的16进制字符串转换成普通文本
///
///
public
static
string
HexNicktoString(
string
hexNick)
{
byte
[] data = HexStringToByteArray(hexNick);
return
Encoding.UTF8.GetString(data);
}
///
/// 把字节数组从offset开始的len个字节转换成一个unsigned int
/// 【此函数摘抄自网络】
///
/// 转换长度, 如果len超过8则忽略后面的.
///
public
static
uint
GetUInt(
byte
[] inData,
int
offset,
int
len)
{
uint
ret = 0;
int
end = 0;
if
(len > 8)
end = offset + 8;
else
end = offset + len;
for
(
int
i = 0; i < end; i++)
{
ret <<= 8;
ret |= (
uint
)inData[i];
}
return
ret;
}
///
/// 将QQ号码字符串转换成十六进制显示
///
///
public
static
string
QQToHexString(
long
qq)
{
string
hexstr = Convert.ToString(qq, 16);
if
(hexstr.Length == 8)
{
return
hexstr;
}
if
(hexstr.Length > 8)
{
return
null
;
}
int
a = 8 - hexstr.Length;
string
b =
""
;
for
(
int
i = 0; i < a; i++)
{
b +=
"0"
;
}
hexstr = (b + hexstr).ToUpper();
return
hexstr;
}
///
/// 将IP的字节流转换成文本格式
///
///
public
static
string
HexIPToString(
string
hexIP)
{
string
ip = ((Convert.ToInt32(hexIP.Substring(0, 2), 16)).ToString()) +
"."
+ ((Convert.ToInt32(hexIP.Substring(2, 2), 16)).ToString()) +
"."
+ ((Convert.ToInt32(hexIP.Substring(4, 2), 16)).ToString()) +
"."
+ ((Convert.ToInt32(hexIP.Substring(6, 2), 16)).ToString());
return
ip;
}
///
/// 把字符串转换成int
///
///
public
static
int
GetInt(
string
s,
int
defaultValue)
{
int
value;
if
(
int
.TryParse(s,
out
value))
{
return
value;
}
else
{
return
defaultValue;
}
}
///
///
///
///
public
static
byte
[] ConvertImageToByteArray(Image image)
{
byte
[] bImage;
try
{
using
(MemoryStream ms =
new
MemoryStream())
{
Bitmap b =
new
Bitmap(image);
b.Save(ms, image.RawFormat);
bImage = ms.ToArray();
ms.Close();
}
}
catch
(Exception) {
throw
; }
return
bImage;
}
///
/// 返回16位小数
///
///
public
static
double
getRadomNum()
{
Random rd =
new
Random();
double
result = rd.NextDouble();
return
result;
}
public
static
long
ByteArrayToLong(
byte
[] arr,
int
Offset,
int
VarLength)
{
long
num = 0L;
try
{
if
(VarLength == 4)
{
num += Convert.ToInt64(arr[Offset + 3]);
num += (
long
)(Convert.ToInt64(arr[Offset + 2]) * Math.Pow(2.0, 8.0));
num += (
long
)(Convert.ToInt64(arr[Offset + 1]) * Math.Pow(2.0, 16.0));
return
(num + ((
long
)(Convert.ToInt64(arr[Offset]) * Math.Pow(2.0, 24.0))));
}
if
(VarLength == 2)
{
num += Convert.ToInt64(arr[Offset + 1]);
num += Convert.ToInt64((
double
)(arr[Offset] * Math.Pow(2.0, 8.0)));
}
}
catch
(Exception exception)
{
throw
new
Exception(
"Error in Converting Bytes to Long()"
, exception);
}
return
num;
}
///
/// QQ登录时POST的参数clientid
///
///
public
static
uint
GetClientID()
{
uint
clientid=
uint
.Parse(
new
Random(Guid.NewGuid().GetHashCode()).Next(0, 99) +
""
+ GetTimestamp(DateTime.Now) / 1000000);
return
clientid;
}
public
static
int
GetGTK(
string
skey)
{
int
hash = 5381;
for
(
int
i = 0; i <= skey.Length - 1; i++)
{
hash += (hash << 5) + Convert.ToInt32(
char
.Parse(skey.Substring(i, 1)));
}
hash = hash & 0x7fffffff;
return
hash;
}
public
static
string
UnicodeToGb(
string
hex)
{
Regex regex =
new
Regex(
@"\\u(?'hex'[0-9A-F]{4})"
);
MatchCollection matches = regex.Matches(hex);
foreach
(Match macth
in
matches)
{
string
str =
string
.Empty;
string
hex_item = macth.Groups[
"hex"
].Value;
for
(
int
i = 0; i < hex_item.Length / 4; i++)
{
str += (
char
)
short
.Parse(hex_item, global::System.Globalization.NumberStyles.HexNumber);
}
hex = hex.Replace(
"\\u"
+ hex_item, str);
}
return
hex;
}
///
/// 将汉字转换为Unicode
///
///
public
static
string
ToUnicode(
string
text)
{
byte
[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
string
lowCode =
""
, temp =
""
;
for
(
int
i = 0; i < bytes.Length; i++)
{
if
(i % 2 == 0)
{
temp = System.Convert.ToString(bytes[i], 16);
if
(temp.Length < 2) temp =
"0"
+ temp;
}
else
{
string
mytemp = Convert.ToString(bytes[i], 16);
if
(mytemp.Length < 2) mytemp =
"0"
+ mytemp; lowCode = lowCode +
@"\u"
+ mytemp + temp;
}
}
return
lowCode;
}
public
static
string
LongToHexString(
long
num)
{
return
string
.Format(
"{0:X8}"
, num);
}
public
static
byte
[] LongToByteArray(
long
num)
{
byte
[] bytes =
new
byte
[4];
bytes[0] = (
byte
)(num >> 0x18);
bytes[1] = (
byte
)(num >> 0x10);
bytes[2] = (
byte
)(num >> 8);
bytes[3] = (
byte
)(num);
return
bytes;
}
///
/// 将QQ号码字符串转换成十六进制显示
///
///
public
static
string
HexQQToString(
long
qq)
{
string
hexstr = Convert.ToString(qq, 16);
if
(hexstr.Length == 8)
{
return
hexstr;
}
if
(hexstr.Length > 8)
{
return
null
;
}
int
a = 8 - hexstr.Length;
string
b =
""
;
for
(
int
i = 0; i < a; i++)
{
b +=
"0"
;
}
hexstr = (b + hexstr).ToUpper();
return
hexstr;
}