简体   繁体   中英

Issues with non-printable characters

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 briefly 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, ie 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 (ie [\\x00-\\x1F\\x80-\\xFF] ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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