434,795 Members | 1,235 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

what is this mean: if( Hex.length &lt; 5 )

 P: 4 Hi There, I need help to understand the script here what's meant? I'm not a programmer but ran into machine issue with machine not recognized a 1D 128 barcode with a space in there such as "ABC 6" it looked like that the script was cut off the 6 due to the length limits to 4 characters. Thanks function HexToWord( Hex ) { var sFullHex = ""; if( Hex.length < 5 ) sFullHex = "0000".substr(0, 4 - Hex.length) + Hex; else sFullHex = Hex.substr(0,4); var val = 0; val += hex_array.indexOf(sFullHex.substr(0,1)) * 4096; val += hex_array.indexOf(sFullHex.substr(1,1)) * 256; val += hex_array.indexOf(sFullHex.substr(2,1)) * 16; val += hex_array.indexOf(sFullHex.substr(3,1)) return val; } Mar 28 '19 #1
6 Replies

 100+ P: 115 < means less-than. length, as it says, returns the length of the string. The condition of 'if' becomes true when the length of Hex is less than 5. Mar 28 '19 #2

 P: 4 Hi dev7060, If I set the length of Hex to less than 6. What I need to do all the lines below that: function HexToWord( Hex ) { var sFullHex = ""; if( Hex.length < 6 ) sFullHex = "0000".substr(0, 4 - Hex.length) + Hex; else sFullHex = Hex.substr(0,4); var val = 0; val += hex_array.indexOf(sFullHex.substr(0,1)) * 4096; val += hex_array.indexOf(sFullHex.substr(1,1)) * 256; val += hex_array.indexOf(sFullHex.substr(2,1)) * 16; val += hex_array.indexOf(sFullHex.substr(3,1)) return val; } Thanks, CH Mar 29 '19 #3

 100+ P: 115 Hi there, I am not sure what you mean exactly. "the script was cut off the 6 due to the length limits to 4 characters" You can have a read on how substr() function works here. You can change it according to the needs like, Hex.substr(0,5) to avoid the cut off, of the character 6 from string "ABC 6" Mar 30 '19 #4

 P: 4 Hi dev7060, I meant do I need to make any changes after set the Hex.substr to(0.5) to those lines below: var val = 0; val += hex_array.indexOf(sFullHex.substr(0,1)) * 4096; val += hex_array.indexOf(sFullHex.substr(1,1)) * 256; val += hex_array.indexOf(sFullHex.substr(2,1)) * 16; val += hex_array.indexOf(sFullHex.substr(3,1)) return val; } Apr 1 '19 #5

 100+ P: 115 Here's what I have observed after going through your program: Your program is converting the hexadecimal values (present in the string) to an integer number. Here's the flow: Reading barcode -> obtaining a string having hexadecimal values -> converting the string to a number (this number might be a product number such as 54673 etc.) Hexadecimal values have a base 16 and they range from 0 to 15. The values after 9 are named as: 10->A , 11->B, 12->C, 13->D, 14->E, 15->F. According to me, there should not be any space in the string if every character is treated as a hexadecimal value. For solving this, you can have another code snippet before the algorithm, to remove all the spaces in the string and obtain the correct string which can be processed further. In that case, manipulation in Hex.substr(0,4) is not even required since the correct string would be spaces free. If there's a standard way your barcodes are numbered (like 5 hex valued strings), then you need to manipulate a 4 char string by adding a 0 before that to make it ready to be processed further. Like "ABC 6" to "0ABC6" Apr 2 '19 #6

 P: 4 HI dev7060, Thanks for all your help here! I will try 0ABC6 once I have a chance. CH Apr 2 '19 #7 