简体   繁体   English

Javascript Code 128 String Builder - Ascii Value> 127 issue

[英]Javascript Code 128 String Builder - Ascii Value > 127 issue

I've been trying to build a small html/javascript based code 128 type B text builder. 我一直在尝试构建一个基于html / javascript的小型代码128类型B文本构建器。

I have it working for most barcodes, but I'm running into an issue if the value used creates a checksum that is a character that is greater than ascii 127. I'm not sure what I should be using to replace that value in that case. 我有它适用于大多数条形码,但如果使用的值创建一个大于ascii 127的字符的校验和,我遇到了一个问题。我不确定我应该用什么来替换那个值案件。 I've read of adding 'Code 3' and 'FNC X' values in the barcode, but it's not clear in what format, with braces and should FNC be 'FNC4' or 'FNC 4', or if that is relevant to the checksum. 我已经读过在条形码中添加“Code 3”和“FNC X”的值,但不清楚是什么格式,带有大括号,FNC应该是'FNC4'还是'FNC 4',或者是否与校验和。

I'm using the free 128 font from this site, http://jtbarton.com/Barcodes/BarcodeStringBuilderExample.aspx . 我正在使用此站点的免费128字体, http://jtbarton.com/Barcodes/BarcodeStringBuilderExample.aspx I've tried various conditions, such as if the value is >127, take the existing ascii value instead of adding 32, but the barcode is then not readable. 我已经尝试了各种条件,例如,如果值> 127,则取现有的ascii值而不是添加32,但条形码不可读。

I have a jsfiddle here, https://jsfiddle.net/jcqvag5g/ . 我有一个jsfiddle, https ://jsfiddle.net/jcqvag5g/。 If you use a value like 500.77005.YELLO.XXXXX.0160828, the barcode text is invalid. 如果使用类似500.77005.YELLO.XXXXX.0160828的值,则条形码文本无效。

Any insight would be appreciated. 任何见解将不胜感激。 I haven't found a working solution at the moment. 我目前还没有找到可行的解决方案。 It could also be the specific barcode font I'm using, so recommendations for other solid 128 fonts would also be appreciated. 它也可能是我正在使用的特定条形码字体,因此对其他固体128字体的推荐也将受到赞赏。

This is the main js code. 这是主要的js代码。

function textTo128(str) {
    /*
     * Generate 128 Barcode text, suitable for copying and pasting.
     */
        var len         = str.length; //str.length - get length of string, used to generate the checksum.
        var type128     = 104; // 128 Type B start
        var typeClose   = 106;
        var total       = 104; 

        var i; // Counter Variable
        for(i=0;i<len;i++){
            total += ((i+1) * (str.charCodeAt(i)-32)); //Multiply char position with decimal value of character, keep running total
        }

        var modVal      = total % 103; // Use Modulus to find our checksum
        var checksum    = String.fromCharCode(modVal+32);

        if(modVal+32>126){alert(modVal+32);};
        document.getElementById('barcodeTotal').innerHTML = String.fromCharCode(type128+100) + str + checksum + String.fromCharCode(typeClose+100);
    }

Thanks, -David 谢谢, - 大卫

checksum should add 18 if larger than 126 如果大于126,校验和应该加18

var checksum = String.fromCharCode(modVal+32 > 126 ? modVal+32+18 : modVal+32); var checksum = String.fromCharCode(modVal + 32> 126?modVal + 32 + 18:modVal + 32);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM