# Converting a string into a string of zeroes and ones on basis of some test

**URL:** <https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 17, 2020, 3:07pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243 "2020-07-17T15:07:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 3:07pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/1 "2020-07-17T15:07:58Z")

</div>

Is there a way to convert a string into zeroes and ones on the basis of some test like follows?:

```julia
vowels = ['a', 'e', 'i', 'o', 'u']
string = "string"
bits = my_fun(i -> (i in vowels), string)
#returns "000100"

```

Here I need to know if there is some functions with similar behavior to `my_fun()`

---

<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:** [July 17, 2020, 3:18pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/2 "2020-07-17T15:18:55Z")

</div>

> [@JaydevSR](#):
>
> Is there a way to convert a string into zeroes and ones

Probably you want a comprehension?

```julia
julia> bits = [i in vowels for i in string]
6-element Array{Bool,1}:
 0
 0
 0
 1
 0
 0

```

---

<div class="post-metadata">

**Author:** ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)\
**Post date:** [July 17, 2020, 3:24pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/3 "2020-07-17T15:24:56Z")

</div>

If you want a `String` result:

```julia
julia> map(c -> '0' + in(c, vowels), "string") 
"000100"

```

---

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 3:40pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/4 "2020-07-17T15:40:20Z")

</div>

How can I convert this array to a binary number?

---

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 3:42pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/5 "2020-07-17T15:42:57Z")

</div>

This is a really nice way but I wonder which one will be faster @stevengj’s solution or yours?

---

<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:** [July 17, 2020, 4:02pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/6 "2020-07-17T16:02:05Z")

</div>

> [@JaydevSR](#):
>
> How can I convert this array to a binary number?

I see, what you want is a single integer whose bit patterns match this binary array? Probably the most efficient thing to do is to build up such an integer directly, rather than creating an array first. For example, the following works as long as the number of characters in the string is `< 64`:

```julia
function tobits(predicate::Function, s::String)
    n = UInt64(0)
    len = 0
    for c in Iterators.reverse(s)
        len += 1
        len > 64 && throw(ArgumentError("length of string is > 64"))
        n = (n << 1) + predicate(c)
    end
    return n
end

```

which gives

```julia
julia> n = tobits(c -> c in vowels, string)
0x0000000000000008

julia> bitstring(n)
"0000000000000000000000000000000000000000000000000000000000001000"

```

(I used `Iterators.reverse` so that bit `i` corresponds to character `i+1`, where `i=0` is the least-significant bit.)

---

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 4:04pm UTC](https://discourse.julialang.org/t/converting-a-string-into-a-string-of-zeroes-and-ones-on-basis-of-some-test/43243/7 "2020-07-17T16:04:52Z")

</div>

Yes that’s exactly what I want thanks!
