122

QR codes I have seen are mostly image files. But can you create QR codes using plain text?

For example ASCII and UTF-8 have black boxes as characters. Can I use those together with spaces to create a QR code?

3
  • 7
    Is there a specific project this is for? I'm just curious – Ben Leggiero Apr 2 '19 at 3:35
  • 6
    @BenLeggiero Thank you for your question. I don't have a certain project but I thought it would be handy to know the existence of such QR codes. For example, you can put those in a bio on a forum if the forum doesn't support profile pictures, and many more advantages. – MCK Apr 2 '19 at 10:19
  • At a small enough font size, you don't even need the black boxes! – Paul D. Waite Apr 3 '19 at 16:27
177

Yes! There is a utility called qrencode that can render these for you.

The only really important factor for a QR code is that the 2D array has "darker" and "ligher" pixels / segments. It can be colored too, though contrast can start to be an issue.

ASCII

Your ability to read this QR code will likely depend on the camera's resolution, distance, and the software you're using.

qrencode -t ASCIIi 'https://superuser.com/questions/1420001/is-it-possible-to-write-a-qr-code'

Note: I used -t ASCIIi (Inverted ASCII) because my terminal is White-on-Black.

ASCII QR Code

ANSI

This mode works by setting the background color to black or white, and printing a number of space characters.

qrencode -t ANSI 'https://superuser.com/questions/1420001/is-it-possible-to-write-a-qr-code'

ANSI QR Code

Some of the raw characters written to the terminal are shown below, these are ANSI escape codes. An "escape" character has a value of 0x1b and can often be written as \e.

  • \e[40m sets the background color to black
  • \e[47m sets the background color to white
  • 0x20 is an ASCII space

ANSI QR Code Raw

UTF-8

There is also a UTF-8 mode (-t UTF8). This mode uses the "half block" characters to increase the density, and cut the line count by half.

  • ▀ - U+2580 / Upper Half Block
  • ▄ - U+2584 / Lower Half Block
  • █ - U+2588 / Full Block

Screenshot from @grawity (thanks)

qrencode -t UTF8 'https://superuser.com/questions/1420001/is-it-possible-to-write-a-qr-code'
qrencode -t ANSIUTF8 'https://superuser.com/questions/1420001/is-it-possible-to-write-a-qr-code'

UTF-8 QR Code

12
  • 4
    Don't forget -t UTF-8 mode, which uses the "box drawings" characters that OP mentions? – user1686 Apr 1 '19 at 15:00
  • 9
    i.imgur.com/WQYkxYm.png i.imgur.com/KeDVJ16.png – this uses "half block" characters , with optional colors to increase contrast only. (They're from Unicode 1.1 and date all the way to IBM DOS; surprised there's still any terminal that cannot render those...) – user1686 Apr 1 '19 at 15:04
  • 7
    @MuratKaçiran: Powershell and CMD are just command interpreters (shells), they both use the same "Windows Console" as the terminal. It too has received significant improvements in Win10.18xx. In the past, it did not support UTF-8 at all. It could do Unicode via UTF-16 with suitably written programs, but those programs had to deliberately use the special Unicode mode. If the program didn't do that, all you get is the MS-DOS era cp437 codepages... In that situation, qrencode -t UTF8 | iconv -f utf8 -t cp437 may still work, as the same box drawings also existed back then. – user1686 Apr 2 '19 at 11:46
  • 7
    To my surprise, the QR reader app on my iphone successfully read all these off my screen, including the ASCII # one. +1. – Digital Trauma Apr 3 '19 at 17:10
  • 3
    @Attie: Oh, I probably had accidentally selected some text so it shows part of the line in reverse-text. – user1686 Apr 4 '19 at 10:30
0

Attie's answer with qrencode is great, but for some reason it always outputs a qr code with inverted colors that my Barcode Scanner app can't read.

Inverting the UTF-8 output is hardly trivial, so I thought I'd leave it here for others ;)

qrencode=`qrencode -t UTF8  "https://superuser.com/q/1420001/551559"`
echo "${qrencode}"

# replace black
qrencode=$(echo "${qrencode}" | sed s/`echo -e '\xe2\x96\x88'`/A/g)
echo "${qrencode}"

# replace white
qrencode=$(echo "${qrencode}" | sed s/\ /B/g)
echo "${qrencode}"

# swap black for white
qrencode=$(echo "${qrencode}" | sed s/A/\ /g)
echo "${qrencode}"

# swap white for black
qrencode=$(echo "${qrencode}" | sed s/B/`echo -e '\xe2\x96\x88'`/g)
echo "${qrencode}"

# replace "Upper Half Block"
qrencode=$(echo "${qrencode}" | sed s/`echo -e '\xe2\x96\x80'`/A/g)
echo "${qrencode}"

# replace "Lower Half Block"
qrencode=$(echo "${qrencode}" | sed s/`echo -e '\xe2\x96\x84'`/B/g)
echo "${qrencode}"

# swap upper for lower
qrencode=$(echo "${qrencode}" | sed s/A/`echo -e '\xe2\x96\x84'`/g)
echo "${qrencode}"

# swap lower for upper
qrencode=$(echo "${qrencode}" | sed s/B/`echo -e '\xe2\x96\x80'`/g)
echo "${qrencode}"

I use this so that I can cryptographically sign a utf-8 plaintext message with a qr-code and display it on my website in html.

To achieve this without html breaking the qr code readability with line spacing, I put the qr code inside of a <pre> block with css styles as follows:

<pre style="line-height:1em; letter-spacing: -1px; font-size: x-large;">
...
</pre>

For a live example, see

Not the answer you're looking for? Browse other questions tagged or ask your own question.