www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How convert String to Hex?

reply Marcone <marcone email.com> writes:
How convert String to Hex?

Example:

string text = "Hello World"; // Converted to Hex = 
48656c6c6f20576f726c64
Apr 18 2020
parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Saturday, 18 April 2020 at 15:47:38 UTC, Marcone wrote:
 How convert String to Hex?

 Example:

 string text = "Hello World"; // Converted to Hex = 
 48656c6c6f20576f726c64
import std.format : format; string hex = format("%(%2x%)", "Hello World"); import std.stdio : writeln; writeln(hex); A bit of explanation: %( %) is a range formatting specifier, and basically means "format each element of the range with the format specifier between these two symbols". In other words, it's the equivalent of "Hello World".map!(c => format("%2x", c)).joiner. -- Simen
Apr 18 2020