Convert Character to Ascii Code in JavaScript

Convert character to ASCII code in JavaScript

"\n".charCodeAt(0);

How to get the ASCII value in JavaScript for the characters

Here is the example:

var charCode = "a".charCodeAt(0);console.log(charCode);

Converting Characters to ASCII in JavaScript

A string input is technically already an array of characters.

You can do the following

asciiKeys = [];
for (var i = 0; i < string.length; i ++)
asciiKeys.push(string[i].charCodeAt(0));

Math behind obtaining a charaters Ascii code

About ASCII

Well, as you know, computers don't store characters as 'a', 'b', 'z' or '.', they only accept binary bits (0 or 1). These binary bits can be used to form integers and numbers, but not strings and characters.

So how does the computer store characters? That's where ASCII (American Standard Code for Information Interchange) comes in.

So the idea is that each character needs to be stored as a number, and ASCII code is then the standardization for what number corresponds to what character and vice versa.

So to answer your first question: String.fromCharCode(x) is just returning you the character in the standardized ASCII table corresponding to the number x. For eg, String.fromCharCode(65) will return you 'A' because the ASCII code for 'A' is standardized by the standard as 65.

Why -18

It's a clever (but confusing) hack to force all the lowercase letters to the character following it.

Because str has been made to be lowercase by the line str = str.trim().toLowerCase();, str[i] will now only contain values from 97 ('a') to 97+25 = 122 ('z').
Now 18 = 122 (mod 26) or 122%26.

This is necessary to accommodate for the fact that 'z' needs to go to 'a', and since we only have 26 characters from 'a' to 'z' that we need to "wrap around" (modulus/%)

Note that:

(97('a')-18)%26 = 1
(98('b')-18)%26 = 2
...
(122('z')-18)%26 = 0

So when you add 97 ('a') to each of the numbers you get a function mapping of 'a'->'b', 'b'->'c', ... , 'z'->'a'

Javascript hexadecimal to ASCII with latin extended symbols

First an example solution:

let demoHex = `0053007400720069006E006700200068006100730020006C0065007400740065007200730020007700690074006800200064006900610063007200690074006900630073003A0020010D002C00200161002C00200159002C0020002E002E002E`;

function hexToString(hex) {
let str="";
for( var i = 0; i < hex.length; i +=4) {
str += String.fromCharCode( Number("0x" + hex.substr(i,4)));
}
return str;
}
console.log("Decoded string: %s", hexToString(demoHex) );

How to convert ASCII characters to readable value in Javascript

This is not a javascript problem. You have the string:

+RESP:GTFRI,EF8019,866425032153324,ZK105,,,,,0,0000000000000000,1,0.0,21,29.5,120.101247,30.344809,20190710013252,,0460,0000,580C,8500,31&0,1,42,0,36638,0,0,0,0,1,,0.0&0.0&0.0&0.0&0&0&0&030015&D50052&0&0&00000000000000000000,49,20190710013252,6D1F$

.. and literally that is the human readable value. That is to say, the string that humans can read literally starts with +RESP .. and ends with $.

Your problem is you don't understand what this string means, not figuring out how to display it on screen.

My personal debugging process when faced with something like this is to use http://www.google.com. Googling the string +RESP:GTFRI gives me a PDF document as the first result. It is the documentation of the Track Air Interface Protocol which is a protocol used by a GPS device called Enduro Pro.

This link may not last forever but for now the document can be found at: http://www.trackingtheworld.com/wt_products/wtenduropro/Documents/Enduro_Pro_Tracker_Air_Interface_Protocol_1.04.pdf.

The documentation of the +RESP:GTFRI packet can be found on page 39.

It looks like it uses AT commands (a format for serial protocols) where commands sent to the device starts with AT+<command> and responses starts with +RESP:<command>. So the packet encodes the reply to the GTFRI command (datatype) which is the command to configure scheduled report (page 19).

Whenever you encounter a new kind of AT command it is worth googling the first few characters (the command words). I have never worked with this device and don't know anything about it but I got all the above from googling +RESP:GTFRI.

Ideally, since you presumably actually have access to the device you should also have copies of the documentation. But I've been in the industry long enough to know that that's not always the case.

how can i convert ascii code to character in javascript

String.fromCharCode(ascii_code)

How to convert a char to its keycode?

You can use the charCodeAt function to achieve this.

Working example:

function showKeyCode () {    var character = document.getElementById("character").value.substring(0, 1);    var code = document.getElementById("character").value.charCodeAt(0);    var msg = "The Key Code for the \"" + character + "\" character is " + code + ".";    alert(msg);}
<input type="text" id="character" size="15"><input type="button" value="Show Key Code" onclick="showKeyCode();">


Related Topics



Leave a reply



Submit