Skip to content

Commit be0d6a1

Browse files
committed
feat: add method convert string to Hex string
1 parent dca9e3b commit be0d6a1

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/Helpers/Converters.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,28 @@ public static byte[] HexadecimalStringToByteArray(this string input)
3737
}
3838
return output;
3939
}
40+
/// <summary>
41+
/// Converts a string to its hexadecimal representation.
42+
/// </summary>
43+
/// <param name="input">The input string to convert.</param>
44+
/// <returns>The hexadecimal representation of the input string.</returns>
45+
46+
public static string ConvertToHexString(this string input)
47+
{
48+
byte[] byteArray = Encoding.UTF8.GetBytes(input);
49+
StringBuilder hexBuilder = new StringBuilder(byteArray.Length * 2);
50+
51+
foreach (byte b in byteArray)
52+
{
53+
hexBuilder.AppendFormat("{0:X2}", b);
54+
}
55+
56+
return hexBuilder.ToString();
57+
}
4058

4159
public static byte[] ConvertKeysToBytes(this string data)
4260
{
4361
return Encoding.UTF8.GetBytes(data);
44-
}
62+
}
4563
}
4664
}

0 commit comments

Comments
 (0)