Last active
November 15, 2019 14:55
-
-
Save oropesa/a0ecc284f2db67846b09b2cd2ce660b6 to your computer and use it in GitHub Desktop.
Simple function to cast ASCII-string-text to barcode128-text for font. That text can be used with Barcode 128 Font and it's recognized by Barcode readers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BARCODE 128 FONT (with or without text): | |
* https://fonts.google.com/specimen/Libre+Barcode+128+Text | |
* https://fonts.google.com/specimen/Libre+Barcode+128 | |
* | |
* EXAMPLES (with checksum < 126 or checksum > 126 ): | |
* - textToBarcode128B( 'Hello World!' ); // "ÌHello World!WÎ" | |
* - textToBarcode128B( 'Hello W4rld!' ); // "ÌHello W4rld!ÆÎ" | |
* | |
* NOTE: | |
* The font takes miliseconds to refresh the barcode-text without checksum char. | |
*/ | |
function textToBarcode128B( str ) { | |
/* Type B start */ | |
var sumValue = 104; | |
/* Multiply char position with decimal value of character */ | |
for( var i = 0, len = str.length; i < len; i++ ) { | |
sumValue += ( ( i + 1 ) * ( str.charCodeAt( i ) - 32 ) ); | |
} | |
/* Use Modulus to find checksum */ | |
var modValue = ( sumValue % 103 ) + 32; | |
/* Checksum with shift control */ | |
var checksum = String.fromCharCode( modValue > 126 ? modValue + 68 : modValue ); | |
return String.fromCharCode( 204 ) + str + checksum + String.fromCharCode( 206 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment