Divide HEX ASCII by numbers?

I have an H / W device that typically uses a serial port for an interface, sending and receiving binary messages in a PC user interface program. I added an Ethernet port and a small TCP / IP stack with a small web server that I want to use to replace the serial port interface using a web browser interface.

Messages are mainly related to requests and responses, but for some web pages I may need two or more Tx / Rx messages to get all the information I need for this page. I will use AJAX XMLHttpRequest () to send messages and receive responses for the page.

The H / W device has limited resources (CPU and RAM), so for this you just need to make a small CGI interface that accepts outgoing messages and encodes them as HEX ASCII (i.e. two HEX ASCII characters / bytes) to send to the browser, which will use some java script to highlight messages in fields and convert them to numeric vars and display them to the user. The same goes for messages sent from the browser to the H / W device.

Messages contain a mixture of field types, signed and unsigned bytes, shorts, longs, floats and are even more complex, since messages mainly contain low-order byte orders.

I can process the final H / W code, but I try to learn the java script and could use the help using the function to translate the HEX ASCII ↔ numbers to the end of the browser.

Any ideas? Any code example where?

Thanks Paul

+6
javascript parsing ascii hex
source share
3 answers

Looks like you want parseInt . It takes a string and an optional radius (which should always be supplied), and analyzes the number as an integer in this radix (or based on a format-based number if none are supplied, so you should always supply one, people are surprised to find that parseInt("010") returns 8 ).

 > parseInt("ab", 16) 171 

To convert back to hex, you can use toString :

 > var num = 16 > num.toString(16) "10" 

Note that you will need to put it on two characters yourself if it comes out as only one character:

 > num = 5 > num.toString(16) "5" 

I was bored, so I wrote some functions to convert in both directions:

 function parseHexString(str) { var result = []; // Ignore any trailing single digit; I don't know what your needs // are for this case, so you may want to throw an error or convert // the lone digit depending on your needs. while (str.length >= 2) { result.push(parseInt(str.substring(0, 2), 16)); str = str.substring(2, str.length); } return result; } function createHexString(arr) { var result = ""; for (i in arr) { var str = arr[i].toString(16); // Pad to two digits, truncate to last two if too long. Again, // I'm not sure what your needs are for the case, you may want // to handle errors in some other way. str = str.length == 0 ? "00" : str.length == 1 ? "0" + str : str.length == 2 ? str : str.substring(str.length-2, str.length); result += str; } return result; } 

What can be used as follows:

 > parseHexString("abcd100001") [171, 205, 16, 0, 1] > createHexString([0, 1, 2, 10, 20, 100, 200, 255, 1000, 2000]) "0001020a1464c8ffe8d0" > parseHexString(createHexString([0, 1, 2, 10, 20, 100, 200, 255, 1000, 2000])) [0, 1, 2, 10, 20, 100, 200, 255, 232, 208] 
+23
source share

You are looking for the parseInt() function:

 x = "0xff"; y = parseInt(x, 16); alert(y); //255 
+7
source share

Have you been looking for something like these two functions?

 <html> <head> <script type="text/javascript"> function hex2Decimal( hex ) { return parseInt("0x"+hex); } function decimal2Hex( num ) { return num.toString(16); } </script> </head> <body> <script type="text/javascript"> document.write(hex2Decimal("A") + "<br />"); document.write(decimal2Hex(16) + "<br />"); </script> </body> </html> 
+3
source share

All Articles