Search code examples
barcodebarcode-scannerchecksum

How to generate barcode checksum


I have a barcode with the characters; 34166110860346bNq (also see image below).
I would like to be able to:
1. identify what type of barcode it is and
2. what formula is used to generate it's checksum digits. (the last 3 digits are the checksum, bNq)

image of barcode


Solution

  • I have reproduced the Code 128 barcode above in this snippet and the checksum generated is the integer 98. None of the trailing characters is part of the checksum. They will all be produced as scanned text by the scanner.

    <html>
    <head>
    <title>Plain Vanilla JS Code 128B Barcodes</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
    </head>
    <body>
    <div id="result"></div>
    <script type="text/javascript">
    // The MIT License (MIT)
    // Copyright (c) 2017, Notionovus, LLC.
    
    var arrayCode128Bin = [ '11011001100', '11001101100', '11001100110', '10010011000', '10010001100', '10001001100', '10011001000', '10011000100', '10001100100', '11001001000', '11001000100', '11000100100', '10110011100', '10011011100', '10011001110', '10111001100', '10011101100', '10011100110', '11001110010', '11001011100', '11001001110', '11011100100', '11001110100', '11101101110', '11101001100', '11100101100', '11100100110', '11101100100', '11100110100', '11100110010', '11011011000', '11011000110', '11000110110', '10100011000', '10001011000', '10001000110', '10110001000', '10001101000', '10001100010', '11010001000', '11000101000', '11000100010', '10110111000', '10110001110', '10001101110', '10111011000', '10111000110', '10001110110', '11101110110', '11010001110', '11000101110', '11011101000', '11011100010', '11011101110', '11101011000', '11101000110', '11100010110', '11101101000', '11101100010', '11100011010', '11101111010', '11001000010', '11110001010', '10100110000', '10100001100', '10010110000', '10010000110', '10000101100', '10000100110', '10110010000', '10110000100', '10011010000', '10011000010', '10000110100', '10000110010', '11000010010', '11001010000', '11110111010', '11000010100', '10001111010', '10100111100', '10010111100', '10010011110', '10111100100', '10011110100', '10011110010', '11110100100', '11110010100', '11110010010', '11011011110', '11011110110', '11110110110', '10101111000', '10100011110', '10001011110', '10111101000', '10111100010', '11110101000', '11110100010', '10111011110', '10111101110', '11101011110', '11110101110', '11010000100', '11010010000', '11010011100', '1100011101011', '11010111000'];
    var array5bit_A = [ 'f//A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I', 'TTECAq7UnEM', 'TUoCA+Jw8kA', 'ZjUDAmZGozo', 'TU4CA7CME0s', 'ajUDAvnk9E4', 'ak4DA7VAmk0', 'ZjkDAtle3bI', 'TVICAxOyzrM', 'STUCAqHeHtM', 'SU4CA+16cNA', 'h6QEAZKdo54', 'SVICA62zYxM', 'RTkCAqx1lb4', 'RVICA/z3WM0', 'QT0CAkdoxRU', 'KFYBA46vJCA'];
    var stringStart = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAACCAQAAADLaIVbAAAANUlEQVQIHQEqANX/A';
    var stringMid = 'AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAA';
    var stringEnd = 'AAAAASUVORK5CYII=" style="width:';
    
    function genBarcode(inputString, intWidth, intHeight) {
    var arraySeq = [], i, intChunks, resultString;
    var intRawmod = inputString.length % 5;
      for (i = 0; i < 5 - intRawmod; i += 1) {
        inputString += "0";
      }
      intChunks = inputString.length / 5;
      for (i = 0; i < intChunks; i += 1) {
        arraySeq[i] = parseInt(inputString.substr(i * 5, 5), 2);
      }
      resultString = "";
      for (i = 0; i < arraySeq.length; i += 1) {
        resultString += stringStart + array5bit_A[arraySeq[i]] + stringMid + array5bit_B[arraySeq[i]] + stringEnd + intWidth + 'px;height:' + intHeight + 'px;">';
      }
      return resultString;
    }
    
    function funcCode128C(strText) {
      'use strict';
    let i, j, intWeight, intLength, intWtProd = 0;
    let arrayData = [];
    let strRaw = [];
    
    	intLength = strText.length;
    	intWtProd = arrayData[0] = 105; // Start with Code 128C
    	console.log(`Value ${arrayData[0]}, Multiplier 1, Cumulative ${intWtProd}`);
    	for (i = 0, j = 0; i < intLength; i += 2, j += 1) {
    			arrayData[j + 1] = parseInt(strText.substr(i, 2), 10);
    			intWeight = j + 1;
    			intWtProd += intWeight * arrayData[j + 1];
        	console.log(`Value ${arrayData[j + 1]}, Multiplier ${j + 1}, Cumulative ${intWtProd}`);
    	}
    	arrayData[++j] = 100; // Switch to Code 128B
    			intWtProd += j * arrayData[j];
        	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
    	arrayData[++j] = 66; // Lowercase 'b'
    			intWtProd += j * arrayData[j];
        	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
    	arrayData[++j] = 46; // Uppercase 'N'
    			intWtProd += j * arrayData[j];
        	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
    	arrayData[++j] = 81; // Lowercase 'q'
    			intWtProd += j * arrayData[j];
        	console.log(`Value ${arrayData[j]}, Multiplier ${j}, Cumulative ${intWtProd}`);
    	arrayData[++j] = intWtProd % 103; // Checksum will be the integer 98 for this example
    	console.log("Checksum = ", arrayData[j]);
    	arrayData[++j] = 106;
    	strRaw = "";
    	for (i = 0; i < arrayData.length; i += 1) {
    		strRaw += arrayCode128Bin[arrayData[i]];
    	}
    	return strRaw;
    } // End Code 128C
    
    document.getElementById("result").innerHTML = genBarcode(funcCode128C("34166110860346"), 18.3, 50);
    </script>
    </body>
    </html>

    If you are curious about how to calculate the checksum, add up the "characters" multiplied by their weighted sum as they appear in the Code 128C/B chart here and then get the remainder of the number divided by 103. I have logged the process to the console.