-
Is there a way to detect if the QR Code will work properly as the encoding mode will change the max number of characters that can be stored will also change.
Or |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Based on how we're using the encoding library and how it works generally (eg doesn't attempt to break the string into multiple segments for split encoding), the safest thing is to assume byte mode. Version is autodetermined, so assume version=40 as your upper bound, and then you control the error correction level, so that factors in but is in your control. With that… The official website has the table documenting this (select 31-40 to see the max): https://www.qrcode.com/en/about/version.html. It's useful but not necessarily a straightforward answer. Byte mode is how your would handle Unicode but those aren't always the same number of bytes. JS strings are historically UCS-2 and thus take 2 bytes per character by default. Unicode doesn't all fit in that so there are surrogate pairs and so a single character may be 4 bytes. The encoding that happens in the library we use converts that to UTF-8, so instead of 2-4 bytes per character it's now 1-4. https://github.com/zpao/qrcode.react/blob/trunk/src/third-party/qrcodegen/index.ts#L873-L886 shows how that conversion is happening and how you could theoretically do limit checks before attempting to generate the whole QR Code. All that to say, worst case of every character taking 4 bytes, you can fit 738 characters. Best case in byte mode is 2953. In numeric mode 7089 characters and alphanumeric is 4296 characters. |
Beta Was this translation helpful? Give feedback.
Based on how we're using the encoding library and how it works generally (eg doesn't attempt to break the string into multiple segments for split encoding), the safest thing is to assume byte mode. Version is autodetermined, so assume version=40 as your upper bound, and then you control the error correction level, so that factors in but is in your control. With that…
The official website has the table documenting this (select 31-40 to see the max): https://www.qrcode.com/en/about/version.html. It's useful but not necessarily a straightforward answer. Byte mode is how your would handle Unicode but those aren't always the same number of bytes. JS strings are historically UCS-2 and thus take…