开发者

Issues with non-printable characters

开发者 https://www.devze.com 2023-02-01 04:24 出处:网络
I\'m using a bar code scanner to read 2-D data matrix, UID barcodes into a web application.UIDs contain the non-printable ASCII characters RS, GS and EOT.My problem is that the browser automatically a

I'm using a bar code scanner to read 2-D data matrix, UID barcodes into a web application. UIDs contain the non-printable ASCII characters RS, GS and EOT. My problem is that the browser automatically appends the HTML Code to the front of each ASCII char and since there are no HTML codes defined for RS, GS and EOT it throws a fit.

I'm using Javascript to dynamically build a string from the scanned in data, here is a sample of what I get back

091[041)062>030RS

As you can see, the RS char is given a value(030) that doesn't exist as a standard HTML Code. Another issue seems to be when a regular alphabetic character is hit the browser seems to treat it as a carriage return(CR), so the values that precede them are shown 开发者_如何学Cbriefly then disappear.

I want to be able to show the whole string in the browser textbox. I have a javascript function that looks for these non-printable chars and swaps them for letters, i.e. RS will be replaced with a concatenation of 'R' and 'S'. But I still have the issue of the browser going crazy as explained above. Any insight is appreciated.

UPDATE:

Hi Mathew, sorry for not being more detailed. I'm tried the replacement function as follows with the replace all flag:

String.replace(/(030)|(029)|(004)/g,"");

Problem is that it replaces the entire string, so

091[041)062>030RS

becomes

/(030)|(029)|(004)/g


I'm not sure if I understand your problem exactly, but why not just run the replacement function on the string before writing it to the DOM? If the non-printable characters are never printed, there won't be any issues.


Response to Update

You can match non-printable characters with hex values using regular expressions. Check this out for details. Here's a quick example:

uid.replace(/[\x30\x29\x04]/g,"")

(Note that the replace() function is called on the string instance, not on String itself.)

To make this more effective, consider using a range of non-printable characters (i.e. [\x00-\x1F\x80-\xFF]).

0

精彩评论

暂无评论...
验证码 换一张
取 消