# UTF8 to Ascii and Reverse

**URL:** <https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650>\
**Category:** General Usage\
**Created:** [March 11, 2018, 10:23pm UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650 "2018-03-11T22:23:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [March 11, 2018, 10:23pm UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650/1 "2018-03-11T22:23:07Z")

</div>

I could program this, but I am wondering if this is already in j: an encoder/decoder of UTF8 to ASCII. Something like

```julia
julia> x= "\u00a5"
"¥"

julia> encodeutf8toascii(x)
"\\00a5"

julia> encodeascii2utf8(ans)
"¥"

```

PS: and thanks for all the help everyone, here and earlier.

---

<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:** [March 12, 2018, 1:07am UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650/2 "2018-03-12T01:07:51Z")

</div>

[https://docs.julialang.org/en/stable/stdlib/strings/#Base.unescape\_string](https://docs.julialang.org/en/stable/stdlib/strings/#Base.unescape_string)

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [March 12, 2018, 3:20am UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650/3 "2018-03-12T03:20:27Z")

</div>

nope.

```julia
julia> x= "\u00a5"
"¥"

julia> unescape_string(x)
"¥"

julia> escape_string(x)
"¥"

```

---

<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:** [March 12, 2018, 12:47pm UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650/4 "2018-03-12T12:47:01Z")

</div>

`unescape_string` at least goes in one direction:

```julia
julia> "\\u00a5"
"\\u00a5"

julia> unescape_string("\\u00a5")
"¥"

```

To go in the opposite direction, you’re correct that you can’t use `escape_string`. You’d have to write your own function, something like:

```nohighlight
function escape_unicode(s::AbstractString)
    buf = IOBuffer()
    for c in s
        if isascii(c)
            print(buf, c)
        else
            i = UInt32(c)
            if i < 0x10000
                print(buf, "\\u", hex(i, 4))
            else
                print(buf, "\\U", hex(i, 6))
            end
        end
    end
    return String(take!(buf))
end

```

Why do you need this, however? If it is because you want to embed Unicode data into an ASCII (7-bit) stream, base64 encoding is a much more standard way to do this (and is implemented in Base).

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [March 12, 2018, 5:50pm UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650/5 "2018-03-12T17:50:36Z")

</div>

I wanted the unescape string, but then I got curious. the natural inverse, escape\_string, did not work. thanks, steven.

---

<div class="post-metadata">

**Author:** ![jmkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmkuhn/32/2090_2.png) [@jmkuhn](https://discourse.julialang.org/u/jmkuhn)\
**Post date:** [March 12, 2018, 7:40pm UTC](https://discourse.julialang.org/t/utf8-to-ascii-and-reverse/9650/6 "2018-03-12T19:40:27Z")

</div>

In stevengj’s `unescape_unicode` you should change the padding in the second `hex()` from 6 to 8. This will give you:

```julia
julia> s = escape_unicode("𠱸1")
"\\U00020c781"

julia> unescape_string(s)
"𠱸1"

```

With padding of 6, the `unescape_string()` will result in an invalid unicode escape sequence.
