Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a text string to hex string?

Tags:

string

ios

hex

Lets say I have a text string : "Say Hello to My Little Friend"

A function should return hex value as:

5361792048656c6c6f20746f204d79204c6974746c6520467269656e64

like image 490
Ameet Dhas Avatar asked Jun 09 '16 08:06

Ameet Dhas


People also ask

How do I encode a hex string?

Hex encoding is performed by converting the 8 bit data to 2 hex characters. The hex characters are then stored as the two byte string representation of the characters. Often, some kind of separator is used to make the encoded data easier for human reading.

Can we convert string to hex in python?

To convert Python String to hex, use the inbuilt hex() method. The hex() is a built-in method that converts the integer to a corresponding hexadecimal string. For example, use the int(x, base) function with 16 to convert a string to an integer.

What are hex strings?

Hexadecimal Number String. The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system and are a popular choice for representing long binary values because their format is quite compact and much easier to understand compared to the long binary strings of 1's and 0's.


Video Answer


1 Answers

Swift 4, Swift 3

let hex: String = "ABC".unicodeScalars.filter { $0.isASCII } .map { String(format: "%X", $0.value) } .joined() print(hex) // correctly yields 414243

like image 174
smat88dd Avatar answered Sep 28 '22 19:09

smat88dd