【C#】BCD转字符串

/// <summary>
/// BCD转字符串
/// </summary>
/// <param name="bcdNum"></param>
/// <param name="offset">偏移量</param>
/// <param name="numlen">数字字符串位数</param>
/// <returns></returns>
/// <remarks></remarks>
public static string BCDToString(byte[] bcdNum, int offset, int numlen)
{
    string retString = "";
    int res;
    var len = Math.DivRem(numlen, 2, out res);
    if (res != 0)
    {
        len++;
    }

    for (int i = 0; i < len; i++)
    {
        retString += ((bcdNum[i + offset] & 0xf0) >> 4).ToString("x");
        retString += (bcdNum[i + offset] & 0xf).ToString("x");
    }
    return retString.ToLower().Replace("f", "");
}
posted @ 2024-03-19 11:36  qiutian-hao  阅读(82)  评论(0编辑  收藏  举报