Output result of string conversion from HEX to ASCII is not expected via function fromCharCode

339 Views Asked by At

I am new to Javascript, I am trying to write an app with React Native to accept keyboard like input from external RFID device. by using react-native-keyevent we can capture the input from RFID device, the keycode captured by react-native keyevent seems not same as the ascii code table i just found by Google, however the hex_to_ascii still can interpret these characters while the result is embedded with strange characters, have no idea of which step cause the issue ? below are the codes:

import QRCodeScanner from 'react-native-qrcode-scanner';
import KeyEvent from 'react-native-keyevent';

const HomeScreen = (props) => {
     ...
     const hex_to_ascii = (str) => {
        var hex = str.toString();
        let strFinal = '';
        for (var n = 0; n < hex.length; n += 2) {
            strFinal += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
        }
        return strFinal;
    };
     ...
    useEffect( () => {
    if (isSwEnabled) {
                console.log('addListener');
                let charArray = [];
                let currEPC = '';
                let currHex = '';

                KeyEvent.onKeyUpListener((keyEvent) => {
                    if (keyEvent.pressedKey === '/') {
                        console.log(charArray);
                        charArray.forEach((value, index, array) => {
                            currHex = currHex.concat(value);
                        });
                        console.log(charArray.join().replace(/,/g, '').substr(8));
                        const aa = hex_to_ascii(currHex);
                        const bb = 'PB01G4E9';
                        console.log(aa === bb); // output: false
                        console.log(aa.length); // output: 13
                        console.log(bb.length); // output: 8
                        
                        charArray = [];
                        console.log(`cardEPC: ${aa}`);
                        // below code for test/compare

                        const array = [50, 42, 30, 31, 47, 34, 45, 39];
                        const a = hex_to_ascii(array.join().replace(/,/g, ''));
                        // const a = hex_to_ascii('5042303147344539');
                        const b = 'PB01G4E9';
                        console.log(a);  // output: PB01G4E9
                        console.log(a === b); // output: true
                    } else if (keyEvent.pressedKey.trim() !== '') {
                        charArray.push(keyEvent.pressedKey.trim());
                    } 
                });
            } else {
                KeyEvent.removeKeyUpListener();
                console.log('removeListener');
            }
        },
        [dispatch, isSwEnabled]
}
);

return ( ... );

}
1

There are 1 best solutions below

0
On

I fixed the issue after using below regex to remove all non-ascii characters, thank you all anyway.

str.replace(/[^ -~]+/g, '')