Skip to content

Commit

Permalink
Added ByteExtension、CharEncodingConverter class,update VisualTreeHelp…
Browse files Browse the repository at this point in the history
…erExtensions class.
  • Loading branch information
LeoYang06 committed Mar 8, 2024
1 parent 461735e commit a1a8021
Show file tree
Hide file tree
Showing 3 changed files with 470 additions and 168 deletions.
82 changes: 82 additions & 0 deletions Source/Wif.Utilities/Extensions/ByteExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**************************************************************************
* File Name:SerializationHelper.cs
* Description:SerializationHelper.cs class description...
* Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved.
* Creator:Leo Yang
* Create Time:2024/03/08
*Project Address:https://github.com/LeoYang-Chuese/wif
**************************************************************************/


using System;
using System.Collections;
using System.Linq;

namespace Frontier.Wif.Utilities.Extensions
{
/// <summary>
/// 表示bit、byte和数据类型转换的扩展方法。
/// </summary>
public static class ByteExtension
{
#region BitArray Extension

/// <summary>
/// Convert bits to bytes.
/// </summary>
/// <param name="bitArray"></param>
/// <returns></returns>
public static byte[] ToBytes(this BitArray bitArray)
{
int length = bitArray.Length / 8 + (bitArray.Length % 8 == 0 ? 0 : 1);
var array = new byte[length];
bitArray.CopyTo(array, 0);
return array;
}

/// <summary>
/// Convert bits to short.
/// </summary>
/// <param name="bitArray"></param>
/// <returns></returns>
public static short ToShort(this BitArray bitArray)
{
byte[] bytes = bitArray.ToBytes();
return bytes.ToShort();
}

#endregion BitArray Extension

#region ValueType Extension

/// <summary>
/// Convert short to bits.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static BitArray ToBitArray(this short value)
{
byte[] bytes = BitConverter.GetBytes(value);
var bits = new BitArray(bytes);
return bits;
}

/// <summary>
/// Convert bytes to bits.
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static short ToShort(this byte[] bytes)
{
if (BitConverter.IsLittleEndian)
{
return BitConverter.ToInt16(bytes, 0);
}

byte[] revValue = bytes.Reverse().ToArray();
return BitConverter.ToInt16(revValue, 0);
}

#endregion ValueType Extension
}
}
84 changes: 84 additions & 0 deletions Source/Wif.Utilities/Extensions/CharEncodingConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**************************************************************************
* File Name:SerializationHelper.cs
* Description:SerializationHelper.cs class description...
* Copyright:Copyright © 2020 LeoYang-Chuese. All rights reserved.
* Creator:Leo Yang
* Create Time:2024/03/08
*Project Address:https://github.com/LeoYang-Chuese/wif
**************************************************************************/


using System;
using System.Text;

namespace Frontier.Wif.Utilities.Extensions
{
/// <summary>
/// 表示字符编码转换器。
/// </summary>
public class CharEncodingConverter
{
#region 字符串转16进制编解码

/// <summary>
/// 字符串转十六进制编码。
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StringToHex(string str)
{
var sb = new StringBuilder();
foreach (char c in str)
{
sb.Append(((int)c).ToString("X4"));
}

return sb.ToString();
}

/// <summary>
/// 十六进制编码转字符串。
/// </summary>
/// <param name="hex"></param>
/// <param name="str">出现不符合十六进制字符则返回空字符。</param>
/// <returns>true:转换成功;false:转换失败</returns>
public static bool HexToString(string hex, out string str)
{
str = string.Empty;
var sb = new StringBuilder();
for (var i = 0; i < hex.Length; i += 4)
{
string hexChar = hex.Substring(i, 4);
if (!IsHex(hexChar))
{
return false;
}

sb.Append((char)Convert.ToInt32(hexChar, 16));
}

str = sb.ToString();
return true;
}

/// <summary>
/// 判断字符串是否符合十六进制数。
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsHex(string str)
{
foreach (char c in str)
{
if (!Uri.IsHexDigit(c))
{
return false;
}
}

return true;
}

#endregion
}
}
Loading

0 comments on commit a1a8021

Please sign in to comment.