You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							59 lines
						
					
					
						
							1.8 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							59 lines
						
					
					
						
							1.8 KiB
						
					
					
				| const Mode = require('./mode') | |
| 
 | |
| /** | |
|  * Array of characters available in alphanumeric mode | |
|  * | |
|  * As per QR Code specification, to each character | |
|  * is assigned a value from 0 to 44 which in this case coincides | |
|  * with the array index | |
|  * | |
|  * @type {Array} | |
|  */ | |
| const ALPHA_NUM_CHARS = [ | |
|   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | |
|   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
|   'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | |
|   ' ', '$', '%', '*', '+', '-', '.', '/', ':' | |
| ] | |
| 
 | |
| function AlphanumericData (data) { | |
|   this.mode = Mode.ALPHANUMERIC | |
|   this.data = data | |
| } | |
| 
 | |
| AlphanumericData.getBitsLength = function getBitsLength (length) { | |
|   return 11 * Math.floor(length / 2) + 6 * (length % 2) | |
| } | |
| 
 | |
| AlphanumericData.prototype.getLength = function getLength () { | |
|   return this.data.length | |
| } | |
| 
 | |
| AlphanumericData.prototype.getBitsLength = function getBitsLength () { | |
|   return AlphanumericData.getBitsLength(this.data.length) | |
| } | |
| 
 | |
| AlphanumericData.prototype.write = function write (bitBuffer) { | |
|   let i | |
| 
 | |
|   // Input data characters are divided into groups of two characters | |
|   // and encoded as 11-bit binary codes. | |
|   for (i = 0; i + 2 <= this.data.length; i += 2) { | |
|     // The character value of the first character is multiplied by 45 | |
|     let value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45 | |
| 
 | |
|     // The character value of the second digit is added to the product | |
|     value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1]) | |
| 
 | |
|     // The sum is then stored as 11-bit binary number | |
|     bitBuffer.put(value, 11) | |
|   } | |
| 
 | |
|   // If the number of input data characters is not a multiple of two, | |
|   // the character value of the final character is encoded as a 6-bit binary number. | |
|   if (this.data.length % 2) { | |
|     bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6) | |
|   } | |
| } | |
| 
 | |
| module.exports = AlphanumericData
 | |
| 
 |