/// <summary> /// Encode the DecodedData of the instance into string as specified Hex format /// </summary> /// <param name="fmt">Format of Hex string</param> /// <param name="lineLength">Length in byte that is included per a line</param> /// <returns></returns> public string EncodeStoredData(HexFormat fmt, byte lineLength) { var str = ""; Line l = new Line(); l.Valid = true; foreach (var blk in DecodedData.Blocks) { int remains = blk.Length; int addr = blk.StartAddress; if ((addr & 0xffff0000) != 0) { l.Type = LineType.Offset; l.Address = (int)(addr & 0xffff0000); l.Data = new byte[] { }; str += EncodeLineWithFormat(l, fmt); } while (remains > 0) { l.Type = LineType.Data; l.Address = addr; l.Data = DecodedData.GetData(addr, Math.Min(lineLength, remains)); str += EncodeLineWithFormat(l, fmt); remains -= l.Data.Length; addr += l.Data.Length; } } l.Type = LineType.Termination; l.Address = 0x0; l.Data = new byte[] { }; str += EncodeLineWithFormat(l, fmt); return(str); }
/// <summary> /// Converts the specified array of <paramref name="bytes"/> to a corresponding HEX string. /// </summary> /// <param name="bytes">The array of bytes.</param> /// <param name="format">The format to be used for the HEX string.</param> /// <returns>The HEX string.</returns> public static string ToHexString(byte[] bytes, HexFormat format) { return(format switch { HexFormat.LowerCase => BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower(), HexFormat.UpperCase => BitConverter.ToString(bytes).Replace("-", string.Empty), _ => throw new Exception($"Unsupported format: {format}") });
public void TestFewValidEntries(string valid) { // Arrange // Act HexFormat hex = new HexFormat(valid); //Assert Assert.True(hex.IsValidRecord); }
public void TestInvalidLine(string notValid) { // Arrange // Act HexFormat hex = new HexFormat(notValid); //Assert Assert.False(hex.IsValidRecord); }
public void TestExtendedAddress() { // Arrange string extended = ":020000025000AC"; // Act HexFormat hex = new HexFormat(extended); // Assert Assert.True(hex.IsValidRecord); Assert.Equal(HexFieldType.ExtendedAddress, hex.HexFieldType); }
/// <summary> /// byteコレクションを16進数文字列に変換する。 /// </summary> public static string BytesToHexString(IReadOnlyList <byte> bytes, HexFormat format) { Span <char> chars = stackalloc char[bytes.Count * 2]; for (var i = 0; i < bytes.Count; i++) { var t = ByteToHexChar(bytes[i], format); chars[i * 2] = t.high; chars[i * 2 + 1] = t.low; } return(new string(chars)); }
public void TestEndOfFile() { // Arrange string endoffile = ":00000001FF"; // Act HexFormat hex = new HexFormat(endoffile); // Assert Assert.True(hex.IsValidRecord); Assert.Equal(0, hex.NumberOfBytes); Assert.Null(hex.Data); }
/// <summary>Takes a byte array and returns a hex-encoded string.</summary> /// <param name="data">Data to be encoded.</param> /// <param name="format">Output format.</param> /// <param name="hcase">Lowercase or uppercase.</param> /// <returns>Hex-encoded string.</returns> /// <remarks>Bit fiddling by CodeInChaos.</remarks> /// <remarks>This method don`t use libsodium, but it can be useful for generating human readable fingerprints.</remarks> public static string BinaryToHex(byte[] data, HexFormat format, HexCase hcase = HexCase.Lower) { var sb = new StringBuilder(); for (var i = 0; i < data.Length; i++) { if ((i != 0) && (format != HexFormat.None)) { switch (format) { case HexFormat.Colon: sb.Append((char)58); break; case HexFormat.Hyphen: sb.Append((char)45); break; case HexFormat.Space: sb.Append((char)32); break; default: //no formatting break; } } var byteValue = data[i] >> 4; if (hcase == HexCase.Lower) { sb.Append((char)(87 + byteValue + (((byteValue - 10) >> 31) & -39))); //lower } else { sb.Append((char)(55 + byteValue + (((byteValue - 10) >> 31) & -7))); //upper } byteValue = data[i] & 0xF; if (hcase == HexCase.Lower) { sb.Append((char)(87 + byteValue + (((byteValue - 10) >> 31) & -39))); //lower } else { sb.Append((char)(55 + byteValue + (((byteValue - 10) >> 31) & -7))); //upper } } return(sb.ToString()); }
public static string GetString(Span <byte> data, HexFormat hexFormat = HexFormat.UpperCase) { char[] values = hexFormat == HexFormat.LowerCase ? lowerCaseHex : upperCaseHex;; StringBuilder sb = new StringBuilder(data.Length * 2); for (int i = 0; i < data.Length; i++) { sb.Append(values[data[i] >> 4]); sb.Append(values[data[i] & 0xF]); } return(sb.ToString()); }
/// <summary>Takes a byte array and returns a hex-encoded string.</summary> /// <param name="data">Data to be encoded.</param> /// <param name="format">Output format.</param> /// <param name="hcase">Lowercase or uppercase.</param> /// <returns>Hex-encoded string.</returns> /// <remarks>Bit fiddling by CodeInChaos.</remarks> /// <remarks>This method don`t use libsodium, but it can be useful for generating human readable fingerprints.</remarks> public static string BinaryToHex(byte[] data, HexFormat format, HexCase hcase = HexCase.Lower) { var sb = new StringBuilder(); for (var i = 0; i < data.Length; i++) { if ((i != 0) && (format != HexFormat.None)) { switch (format) { case HexFormat.Colon: sb.Append((char)58); break; case HexFormat.Hyphen: sb.Append((char)45); break; case HexFormat.Space: sb.Append((char)32); break; default: //no formatting break; } } var byteValue = data[i] >> 4; if (hcase == HexCase.Lower) { sb.Append((char)(87 + byteValue + (((byteValue - 10) >> 31) & -39))); //lower } else { sb.Append((char)(55 + byteValue + (((byteValue - 10) >> 31) & -7))); //upper } byteValue = data[i] & 0xF; if (hcase == HexCase.Lower) { sb.Append((char)(87 + byteValue + (((byteValue - 10) >> 31) & -39))); //lower } else { sb.Append((char)(55 + byteValue + (((byteValue - 10) >> 31) & -7))); //upper } } return sb.ToString(); }
/******************************************************************************** * input: * dest: 为转换后的结果 * p->addr[0]: 高地址 * p->addr[1]: 低地址 * p->type: 记录类型 * p->data: 为bin格式流有效数据指针 * p->len: 为bin格式流有效数据长度 * output: * 返回有效数据的长度 ********************************************************************************/ public UInt16 BinFormatEncode(ref byte[] dest, HexFormat p) { UInt16 offset = 0; byte check = 0, num = 0; //:(1) + 长度(2) + 地址(4) + 类型(2) StringBuilder each_line_content = new StringBuilder(); each_line_content.Append(string.Format(":{0:X2}", p.len)); each_line_content.Append(string.Format("{0:X2}", p.addr[0])); each_line_content.Append(string.Format("{0:X2}", p.addr[1])); each_line_content.Append(string.Format("{0:X2}", p.type)); offset += 9; check = (byte)(p.len + p.addr[0] + p.addr[1] + p.type); //计算校验和 while (num < p.len) //当数据长度不为0,继续在之前的hex格式流添加数据 { if (0 == p.type) { //sprintf((char*)dest + offset, "%02X", p.data[num]); each_line_content.Append(string.Format("{0:X2}", p.data[num])); check += p.data[num]; //计算校验和 offset += 2; //hex格式数据流数据指针偏移2 num++; //下一个字符 } else if (4 == p.type) { //sprintf((char*)dest + offset, "%02X", p.addr[num + 2]); each_line_content.Append(string.Format("{0:X2}", p.addr[num + 2])); check += p.addr[num + 2]; //计算校验和 offset += 2; //hex格式数据流数据指针偏移2 num++; //下一个字符 } } check = (byte)(~check + 1); //反码+1 //sprintf((char*)dest + offset, "%02X", check); each_line_content.Append(string.Format("{0:X2}", check)); offset += 2; dest = System.Text.Encoding.Default.GetBytes(each_line_content.ToString()); return(offset); //返回hex格式数据流的长度 }
public static bool TryGetString(Span <byte> data, HexFormat hexFormat, Span <char> destination, out int witeChars) { witeChars = data.Length * 2; if (data.Length * 2 > destination.Length) { witeChars = 0; return(false); } char[] values = hexFormat == HexFormat.LowerCase ? lowerCaseHex : upperCaseHex;; for (int i = 0; i < witeChars; i += 2) { byte value = data[i >> 1]; destination[i] = values[value >> 4]; destination[i + 1] = values[value & 0xF]; } return(true); }
internal string EncodeLineWithFormat(Line l, HexFormat fmt) { if (l.Type == LineType.Data) { l.Address = l.Address - _addressOffset; // cuurent offset } else if (l.Type == LineType.Offset) { _addressOffset = l.Address; } char[] a = null; switch (fmt) { case HexFormat.INTEL_HEX: a = _ihex.EncodeLine(l); break; case HexFormat.S_RECORD: a = _srec.EncodeLine(l); break; } return((a == null)? "": new String(a) + "\r\n"); }
/// <summary> /// byteを16進数文字に変換する。 /// </summary> public static (char high, char low) ByteToHexChar(byte value, HexFormat format) { Span <char> chars = stackalloc char[2]; return(value.TryFormat(chars, out _, format.GetStringValue()) ? (chars[0], chars[1]) : throw new InvalidOperationException()); }
/// <summary> /// Converts the specified collection of <paramref name="colors"/> to a corresponding collection of HEX colors, /// according to <paramref name="format"/>. /// </summary> /// <param name="colors">The colors to be converted.</param> /// <param name="format">The HEX format.</param> /// <returns>A collection of the HEX colors.</returns> public static IEnumerable <string> ToHex(this IEnumerable <IColor> colors, HexFormat format) { return(colors.Select(color => color.ToHex(format))); }
/// <summary> /// Returns the MD5 hash of <paramref name="input"/>. The input string is converted using <see cref="Encoding.UTF8"/>. /// </summary> /// <param name="input">The input string</param> /// <param name="format">The format to be used for the HEX string.</param> /// <returns>The hash as a 32-character hexadecimal number.</returns> public static string GetMd5Hash(string input, HexFormat format) { using MD5 md5 = MD5.Create(); return(GetHash(md5, input, format)); }
/// <summary> /// Returns the SHA512 hash of <paramref name="input"/>. The input string is converted using /// <paramref name="encoding"/>. /// </summary> /// <param name="input">The input string</param> /// <param name="format">The format to be used for the HEX string.</param> /// <param name="encoding">The encoding to be used for the conversion.</param> /// <returns>The hash as a 128-character hexadecimal number.</returns> public static string GetSha512Hash(string input, HexFormat format, Encoding encoding) { using SHA512 sha512 = SHA512.Create(); return(GetHash(sha512, input, format, encoding)); }
/// <summary> /// Returns the SHA256 hash of <paramref name="input"/>. The input string is converted using <paramref name="encoding"/>. /// </summary> /// <param name="input">The input string</param> /// <param name="format">The format to be used for the HEX string.</param> /// <param name="encoding">The encoding to be used for the conversion.</param> /// <returns>The hash as a 64-character hexadecimal number.</returns> public static string GetSha256Hash(string input, HexFormat format, Encoding encoding) { using SHA256 sha256 = SHA256.Create(); return(GetHash(sha256, input, encoding)); }
/// <summary> /// Returns the SHA1 hash of <paramref name="input"/>. The input string is converted using <paramref name="encoding"/>. /// </summary> /// <param name="input">The input string</param> /// <param name="format">The format to be used for the HEX string.</param> /// <param name="encoding">The encoding to be used for the conversion.</param> /// <returns>The hash as a 40-character hexadecimal number.</returns> public static string GetSha1Hash(string input, HexFormat format, Encoding encoding) { using SHA1 sha1 = SHA1.Create(); return(GetHash(sha1, input, format, encoding)); }
/// <summary> /// Converts the specified array of <paramref name="bytes"/> to a corresponding HEX string. /// </summary> /// <param name="bytes">The array of bytes.</param> /// <param name="format">The format to be used for the HEX string.</param> /// <returns>The HEX string.</returns> public static string ToHexString(this byte[] bytes, HexFormat format) { return(StringUtils.ToHexString(bytes, format)); }