Binary value of a Character in pure Swift -
i need convert character in binary string.
ex : c = 34 = "1000011"
my work in progress this. it's work "c" character :
import foundation func chartobin(c:character) -> string { let binaire = 34 return string(binaire, radix: 2) } let c:character = "c" let b = chartobin(c)
thank :
i solved problem code :
func stringtobinarystring (mystring:string) -> string { // array of characters let characterarray = [character](mystring.characters) // array of asccii value let asciiarray = characterarray.map({string($0).unicodescalars.first!.value}) // array of binary value let binaryarray = asciiarray.map ({ string($0, radix: 2)}) // reduce in string let r = binaryarray.reduce("",combine: {$0 + " " + $1}) return r } // sample use : let r = stringtobinarystring("cc")
Comments
Post a Comment