1 var Mode = require('./mode')
4 * Array of characters available in alphanumeric mode
6 * As per QR Code specification, to each character
7 * is assigned a value from 0 to 44 which in this case coincides
12 var ALPHA_NUM_CHARS = [
13 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
14 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
15 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
16 ' ', '$', '%', '*', '+', '-', '.', '/', ':'
19 function AlphanumericData (data) {
20 this.mode = Mode.ALPHANUMERIC
24 AlphanumericData.getBitsLength = function getBitsLength (length) {
25 return 11 * Math.floor(length / 2) + 6 * (length % 2)
28 AlphanumericData.prototype.getLength = function getLength () {
29 return this.data.length
32 AlphanumericData.prototype.getBitsLength = function getBitsLength () {
33 return AlphanumericData.getBitsLength(this.data.length)
36 AlphanumericData.prototype.write = function write (bitBuffer) {
39 // Input data characters are divided into groups of two characters
40 // and encoded as 11-bit binary codes.
41 for (i = 0; i + 2 <= this.data.length; i += 2) {
42 // The character value of the first character is multiplied by 45
43 var value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45
45 // The character value of the second digit is added to the product
46 value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])
48 // The sum is then stored as 11-bit binary number
49 bitBuffer.put(value, 11)
52 // If the number of input data characters is not a multiple of two,
53 // the character value of the final character is encoded as a 6-bit binary number.
54 if (this.data.length % 2) {
55 bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
59 module.exports = AlphanumericData