# Convert utf8 to binary

**URL:** <https://discourse.julialang.org/t/convert-utf8-to-binary/42315>\
**Category:** New to Julia\
**Created:** [June 30, 2020, 5:39pm UTC](https://discourse.julialang.org/t/convert-utf8-to-binary/42315 "2020-06-30T17:39:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pihive](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@pihive](https://discourse.julialang.org/u/pihive)\
**Post date:** [June 30, 2020, 5:39pm UTC](https://discourse.julialang.org/t/convert-utf8-to-binary/42315/1 "2020-06-30T17:39:18Z")

</div>

Hi,

I’m new to Julia and trying to solve the cryptopals challenge: [Challenge 6 Set 1 - The Cryptopals Crypto Challenges](https://cryptopals.com/sets/1/challenges/6)

This challenge requires to compute the hamming distance between two strings:

```julia
str1 = "this is a test"

str2 = "wokka wokka!!!"

```

The Hamming distance is the number of differing bits. In the above case the distance must be 37.

My approach was the following:

`Vector{UInt8}(str1) .!= Vector{UInt8}(str2)`

but this is wrong (results in a distance of 14)  
I get the same results using the `hammond` function in the `Distances` package.

To confirm that 37 is correct I converted the strings to their bit values using an ASCII table (by hand)

```julia
s1 = [0,1,1,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,0,0,0,1,1,0,0,1,0,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1,0,0]

s2 = [0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,1,1,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,1,1,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1]

```

now the distance is indeed 37:

```julia
julia> sum(abs.(s1 .- s2))
37

```

What is a more elegant solution to this problem?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 30, 2020, 5:50pm UTC](https://discourse.julialang.org/t/convert-utf8-to-binary/42315/2 "2020-06-30T17:50:12Z")

</div>

> [@pihive](#):
>
> The Hamming distance is the number of differing bits.

The Hamming distance is the number of differing _characters_, no? Anyway, you could sum the bits in the xor between the elements of the UInt8 vector you created. I think there is a `popcnt` in Julia.

Edit: it is called `count_ones`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 30, 2020, 5:57pm UTC](https://discourse.julialang.org/t/convert-utf8-to-binary/42315/3 "2020-06-30T17:57:36Z")

</div>

@pihive You’re getting a different answer because you’re measuring the number of _bytes_ which are different, not the number of _bits_.

You can find bitwise difference between two bytes with xor:

```julia

julia> a = 0xab
0xab

julia> b = 0x04
0x04

julia> bitstring(a)
"10101011"

julia> bitstring(b)
"00000100"

julia> bitstring(xor(a, b))
"10101111"

```

so now you just need a way to count all the 1s in a byte. Here’s an overly complicated but compact one:

```julia
julia> function count_ones(x::UInt8)
         Int(sum((x >> i) & 0x01 for i in 0:7))
       end
count_ones (generic function with 1 method)

```

Now you just need to xor each byte of the two inputs and sum up the total number of ones. You can do that with broadcasting and summing, just like in your hand-written binary example.

Edit: It turns out that Julia already provides `count_ones` (with the same name). So you don’t need my overly complicated implementation at all!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 30, 2020, 5:57pm UTC](https://discourse.julialang.org/t/convert-utf8-to-binary/42315/4 "2020-06-30T17:57:46Z")

</div>

> [@kristoffer.carlsson](#):
>
> Anyway, you could sum the bits in the xor between the elements of the UInt8 vector you created.

In particular:

```julia
julia> sum(count_ones.(xor.(codeunits(str1), codeunits(str2))))
37

```

(I used `codeunits` rather than `Vector{UInt8}` to avoid making a copy — `codeunits` is just a byte-based view of the string contents.)
