src/openSourceC.DotNetLibrary.Core/HexConvert.cs
/// <summary>
///		Converts a byte array to a hexadecimal string.
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
Converts a byte array to a hexadecimal string.
Converts a byte array to a hexadecimal string .
[return: NotNullIfNotNull("byteArray")]
		public static string? DumpByteArray(byte[]? byteArray)
		{
			const int lineBlockSize = 8;
			const int bytesPerLine = 16;
			const int linesPerPage = 16;
			const int pageSize = linesPerPage * bytesPerLine;
			if (byteArray == null)
			{
				return null;
			}
			StringBuilder returnValue = new StringBuilder();
			for (int page = 0; page * pageSize < byteArray.Length; page++)
			{
				if (page != 0)
				{
					returnValue.AppendLine();
				}
				for (int line = 0; line < linesPerPage && page * pageSize + line * bytesPerLine < byteArray.Length; line++)
				{
					returnValue.Append($"{(page * linesPerPage + line) * bytesPerLine:X4} :");
					for (int index = 0; index < bytesPerLine; index++)
					{
						if (index % lineBlockSize == 0)
						{
							returnValue.Append("  ");
						}
						int byteIndex = (page * linesPerPage + line) * bytesPerLine + index;
						if (byteIndex < byteArray.Length)
						{
							returnValue.Append($"  {byteArray[byteIndex]:X2}");
						}
						else
						{
							returnValue.Append("    ");
						}
					}
					returnValue.Append("    ");
					for (int index = 0; index < bytesPerLine; index++)
					{
						int byteIndex = (page * linesPerPage + line) * bytesPerLine + index;
						if (byteIndex < byteArray.Length)
						{
							byte arrayByte = byteArray[byteIndex];
							if (arrayByte < 0x20 || arrayByte > 0x7F)
							{
								returnValue.Append(".");
							}
							else
							{
								returnValue.Append(Convert.ToChar(arrayByte));
							}
						}
						else
						{
							returnValue.Append(" ");
						}
					}
					returnValue.AppendLine();
				}
			}
			return returnValue.ToString();
		}
[ return : NotNullIfNotNull ( " " ) ] public static string ? DumpByteArray ( byte [ ] ? byteArray ) { const int lineBlockSize = 8 ; const int bytesPerLine = 16 ; const int linesPerPage = 16 ; const int pageSize = linesPerPage * bytesPerLine ; if ( byteArray == null ) { return null ; } StringBuilder returnValue = new StringBuilder ( ) ; for ( int page = 0 ; page * pageSize < byteArray . Length ; page ++ ) { if ( page != 0 ) { returnValue . AppendLine ( ) ; } for ( int line = 0 ; line < linesPerPage && page * pageSize + line * bytesPerLine < byteArray . Length ; line ++ ) { returnValue . Append ( $" { ( page * linesPerPage + line ) * bytesPerLine : }  : " ) ; for ( int index = 0 ; index < bytesPerLine ; index ++ ) { if ( index % lineBlockSize == 0 ) { returnValue . Append ( " " ) ; } int byteIndex = ( page * linesPerPage + line ) * bytesPerLine + index ; if ( byteIndex < byteArray . Length ) { returnValue . Append ( $" { byteArray [ byteIndex ] : } " ) ; } else { returnValue . Append ( " " ) ; } } returnValue . Append ( " " ) ; for ( int index = 0 ; index < bytesPerLine ; index ++ ) { int byteIndex = ( page * linesPerPage + line ) * bytesPerLine + index ; if ( byteIndex < byteArray . Length ) { byte arrayByte = byteArray [ byteIndex ] ; if ( arrayByte < 0x20 || arrayByte > 0x7F ) { returnValue . Append ( " " ) ; } else { returnValue . Append ( Convert . ToChar ( arrayByte ) ) ; } } else { returnValue . Append ( " " ) ; } } returnValue . AppendLine ( ) ; } } return returnValue . ToString ( ) ; }
Converts a byte array to a hexadecimal string.
Converts a byte array to a hexadecimal string .
None
byteArray
byte[]?
None