c# - Hexa 출력


network stream으로 수신한 data를 확인하기 위해 hexa로 출력하는 메소드를 작성해 보았습니다.

/// <summary>
/// Byte 수신된 내용을 Hexa로 출력하여 디버그에서 사용
/// </summary>
public static string ByteArrayToString(byte[] byteArray)
{
    var hex = new StringBuilder(byteArray.Length * 2);
    int idx = 0;
    foreach (var b in byteArray)
    {
        hex.AppendFormat("{0:x2} ", b);
        idx++;
        if (idx == 20)
        {
            idx = 0;
            hex.Append(Environment.NewLine);
        }
    }
    return hex.ToString();
}

 

ByteArrayToString 메소드를 사용하여 로그를 출력하면 아래와 같이 출력합니다.

FFD8 FFE0 0010 4A46 4946 0001 0101 0001 
0001 0000 FFDB 0043 0003 0202 0302 0203 
0303 0304 0303 0405 0805 0504 0405 0A07 
0706 080C 0A0C 0C0B 0A0B 0B0D 0E12 100D 
0E11 0E0B 0B10 1610 1113 1415 1515 0C0F 
1718 1614 1812 1415 14FF DB00 4301 0304 
0405 0405 0905 0509 140D 0B0D 1414 1414 
1414 1414 1414 1414 1414 1414 1414 1414 
1414 1414 1414 1414 1414 1414 1414 1414 
1414 1414 1414 1414 1414 1414 1414 FFC0

 

 

광고

댓글 남기기