Keep zero character when convert from DEC to HEX in Node JS - javascript

I am currently reading the ID number of the energy meter with Node JS and serialport library. The power meter ID has the following format xx xx xx xx xx xx. When I send the command and receive the data, I get the following DEC numbers: 0 0 24 1 104 115. Following the manufacturer's instructions, I have to convert this sequence to HEX. I have added it in an array and exported to the console as follows:
console.log(
(arrID[0]).toString(16)+
(arrID[1]).toString(16) +
(arrID[2]).toString(16) +
(arrID[3]).toString(16) +
(arrID[4]).toString(16) +
(arrID[5].toString(16)
);
and it returned to me as follows 001816873. This is the wrong ID, The correct ID to show must be 000018016873. I know the reason is the conversion of numbers with the first character is 0. I look forward to advice from you.

I used normal js, hope this helps you.
var arrID =[0, 0, 24, 1, 104, 115];
var arrID2 = ['','','','','',''];
for(var i=0;i<6;i++)
{
arrID2[i]=(arrID[i]).toString(16);
if(arrID2[i].length==1)arrID2[i]='0'+arrID2[i];
}
console.log(
(arrID2[0])+
(arrID2[1])+
(arrID2[2])+
(arrID2[3])+
(arrID2[4])+
(arrID2[5])
)
the output is
000018016873

Related

Trying to understand how to use parseFloat to convert string to number

I'm trying to understand how to user parseFloat to convert a string to a number--which by the way changes based on a users rewards club points.
Here's the code I've written so far:
var ptsBalance = jQuery('.value.large.withCommas').text(); // get current RC points balance
var strParse = parseInt(ptsBalance, 10); // * Output should be current RC balance
alert(strParse); // alert Rewards Club Balance
var bonusPoints = 70000;
var totalPts = jQuery(strParse + bonusPoints); // sum total of Bonus and Current points
jQuery(strParse).appendTo('.currentPts');
jQuery(totalPts).appendTo('.totalPts'); // insert RC points balance after 'Current Points' text
Clearly I'm not using pareFloat, but rather strParse, which is rounding my string. That said, how do I convert a string to a number that could be "10", "100", "1,000", "10,000" etc.?
Here's a link to my Fiddle: http://jsfiddle.net/bkmills1/60vdfykq/
Still learning...
Thanks in advance!
In your fiddle, change line 4 from:
var strParse = parseInt(ptsBalance, 10);
to
var strParse = parseInt(ptsBalance.replace(',',''), 10);
You may also want to remove any other symbols needed, or even use regular expressions to do just that.

Mimic this Erlang code behaviour in Javascript

I'm trying to obtain in Javascript the same value returned by the following generate_hash erlang function
-define(b2l(V), binary_to_list(V)).
-define(l2b(V), list_to_binary(V)).
generate_hash(User, Secret, TimeStamp) ->
SessionData = User ++ ":" ++ erlang:integer_to_list(TimeStamp, 16),
Hash = crypto:sha_mac(Secret, SessionData),
base64:encode(SessionData ++ ":" ++ ?b2l(Hash)).
make_time() ->
{NowMS, NowS, _} = erlang:now(),
NowMS * 1000000 + NowS.
This function is being called in erlang in this way:
Username = "username"
Secret = ?l2b("secret"),
UserSalt = "usersalt",
CurrentTime = make_time(),
Hash = generate_hash( ?b2l(UserName), <<Secret/binary, UserSalt/binary>>, CurrentTime).
I managed to use the google CryptoJS library to calculate the hash, but the base64 returned value does not match the one returned in erlang.
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
function generate_hash(User, Secret, TimeStamp) {
var SessionData = User + ":" + parseInt(TimeStamp,16);
var hash = CryptoJS.HmacSHA1(Secret,SessionData);
return atob(SessionData + ":" + hash.toString())
}
var Hash = generate_hash( "username", "secret"+"usersalt", new Date().getTime())
alert(Hash);
There are three problems in your code.
Firstly: CryptoJS.HmacSHA1(Secret,SessionData); has its arguments reversed. It should be CryptoJS.HmacSHA1(SessionData, Secret);.
You can check it out in JS console:
var hash = CryptoJS.HmacSHA1("b", "a");
0: 1717011798
1: -2038285946
2: -931908057
3: 823367506
4: 21804555
Now, go to Erlang console and type this:
crypto:sha_mac(<<"a">>, <<"b">>).
<<102,87,133,86,134,130,57,134,200,116,54,39,49,19,151,82,1,76,182,11>>
binary:encode_unsigned(1717011798).
<<102,87,133,86>>
binary:encode_unsigned(21804555).
<<1,76,182,11>>
I don't know equivalent method for signed integers, but this proves, that changing the order of arguments gives the same binary value.
Second problem is with hash.toString(), which following my example gives something like:
hash = CryptoJS.HmacSHA1("b", "a");
hash.toString();
"6657855686823986c874362731139752014cb60b"
while Erlang binary to list will result in:
Str = binary_to_list(Hash).
[102,87,133,86,134,130,57,134,200,116,54,39,49,19,151,82,1,76,182,11]
io:format("~s", [Str]).
fWV9Èt6'1^SR^AL¶^K
I am not sure, what toString does with word array, but this messes up the final result.
Third problem is, that new Date().getTime() will return time in milliseconds, while in Erlang, you have microseconds. This shouldn't matter, when you test it with static integer, though.
Two things:
The make_time function in the Erlang code above returns the number of seconds since the Unix epoch, while the Javascript method getTime returns the number of milliseconds.
Besides that, since you're probably not running the two functions in the same second, you'll get different timestamps and therefore different hashes anyway.
The Javascript function parseInt parses a string and returns an integer, while the Erlang function integer_to_list takes an integer and converts it to a string (in Erlang, strings are represented as lists, thus the name). You probably want to use the toString method instead.
This algorithm can generate the same sequence of bytes generated by erlang counterpart:
var ret = [];
var hash = CryptoJS.HmacSHA1("b","a").words;
angular.forEach(hash,function(v){
var pos = v>=0, last=ret.length;
for(v=pos?v:v>>>0; v>0; v=Math.floor(v/256)) {
ret.splice(last, 0, v%256);
}
});
console.info(ret);
console.info(String.fromCharCode.apply(String,ret));
The above outputs:
[102, 87, 133, 86, 134, 130, 57, 134, 200, 116, 54, 39, 49, 19, 151, 82, 1, 76, 182, 11]
fWV9Èt6'1RL¶

Create ip range from start and end point in javascript or jquery

I have start IP address and end IP address. I want to create range between these ip address.
For example :
start = '10.10.1.0'
end = '10.10.10.10'
Output must be like.
10.10.1.0
10.10.1.1
10.10.1.2
...
...
10.10.1.255
10.10.2.0
10.10.2.1
...
...
10.10.10.9
10.10.10.10
Is there any object in JavaScript which give range from start and end IP address?
Looking at IP addresses in hex is really useful here. Each number between the periods is called two "octet" which means grouping of 16 bits. The maximum value of each number is 255, or 0xFF in hex. Your starting address of 10.10.1.0 is represented as 0x0A0A0100 in hex. Your ending address is 0x0A0A0A0A.
I've written a JSFiddle to show you how simple it is to count through IP addresses once you represent them in hex.
http://jsfiddle.net/sL7VM/
Here is the most important part of the code:
for(var i = 0x0A0A0100; i < 0x0A0A0A0A; i++)
{
var oc4 = (i>>24) & 0xff;
var oc3 = (i>>16) & 0xff;
var oc2 = (i>>8) & 0xff;
var oc1 = i & 0xff;
$('#output').append(oc4 + "." + oc3 + "." + oc2 + "." + oc1 + "<br>");
}
There is no way to do this in native javascript other than splitting the start and end on the "." char and then creating a loop. It may be achievable by a library.
Have you had a look at this one? https://code.google.com/p/iplib-js/

Decimal number gets disapper

My code:
var $label = $(ev.currentTarget);
var $price = $label.parent("form").find(".oe_price .oe_currency_value");
if (!$price.data("price")) {
$price.data("price", parseFloat($price.text()); //price is 10.30 but it returns 10.3 as number
}
$price.html($price.data("price")+parseFloat($label.find(".badge span").text() || 0));
/* The value coming in **badge** is **12.00**
* but parseFloat converts that into **12**
* thats why after the addition I got the output as **22.3**
* but i want it as **22.30**
*/
I have a string
'10.30'
Now, if I convert the string to number using parsefloat
parseFloat('10.30')
I got the output as 10.3
And If I do it using the .tofixed(2)
parseFloat('10.30').toFixed(2)
I got the output 10.30 but it is in STRING which is the big problem for me because I want the output as number.
And if i do like this
Number(parseFloat('10.30').toFixed(2))
I got the output 10.3
But i want the output in number and with decimal point
like this 10.30
Plz help...!!!
thankz Passerby for the help.
Finally i got the result from this :
$price.html(($price.data("price")+parseFloat($label.find(".badge span").text() || 0,10)).toFixed(2));
Try this:
parseFloat(Math.round(num3 * 100) / 100).toFixed(2);
See Live Demo
I don't know what are you doing, but I've done the same and got desired result.
My code:
var str = '10.30';
var flo = parseFloat(str);
alert(flo);
flo = flo.toFixed(2)
alert(flo);
See here: Fiddle

Dealing with uint8_t in javascript

G'day peoples,
I'm using MavLink to obtain GPS information. One of the message types is GPS_STATUS which is described by MavLink using a series of uint8_t[20].
If I run the following code:
console.log(' sat prn: ' + message.satellite_prn);
console.log(' sat prn: ' + JSON.stringify(message.satellite_prn));
console.log(' sat prn: ' + JSON.stringify(new Uint8Array(message.satellite_prn)));
I get the following output:
sat prn: <weird charcaters...>
sat prn: "\u0002\u0005\n\f\u000f\u0012\u0015\u0019\u001a\u001b\u001d\u001e\u001f\u0000\u0000\u0000\u0000"
sat prn: {"BYTES_PER_ELEMENT":1,"buffer":{"byteLength":0},"length":0,"byteOffset":0,"byteLength":0}
So obviously it's not working. I need a means to get the int value of each element.
I found this https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays?redirectlocale=en-US&redirectslug=JavaScript_typed_arrays
Which made me think I would be able to do the following:
satellite_prn = Array.apply([], new Uint8Array(message.satellite_prn));
satellite_prn.length === 20;
satellite_prn.constructor === Array;
But when I stringify it via JSON it reports [], I presume this is an empty array.
Anyone know how I can do this? I know that the data is an array of 20 unsigned 8 bit integers. I just need to know how to access or parse them.
Note: I'm using node.js, but that shouldn't affect what I'm doing. This is why I'm using console.log, as it's avail in node.js.
Two issues with your code:
message.satellite_prn is a string not an array
Unit8Array needs to be loaded with .set
To get an array of numbers from message.satellite_prn, do this:
var array = message.satellite_prn.map(function(c) { return c.charCodeAt(0) })
To load an ArrayBuffer, do this:
var buffer = new ArrayBuffer(array.length);
var uint8View = new Uint8Array(buffer);
uint8View.set(array);
Ideally you wouldn't need to go through the string. If you are obtaining the data from an up-to-date implementation of XMLHttpRequest, such as xhr2, you can set:
responseType = "arraybuffer"
Looks like the problem is my understanding on how to deal with binary data within javascript. I found that I can just do the following to get the data as base 10 via charCodeAt().
The following is the code that allowed me to iterate through the 20 unsigned 8 bit integers and get each value as a decimal:
for(var i = 0, l = message.satellite_prn.length; i < l; i++) {
console.log(' Satellite ' + i + ', id: ' +
parseInt(message.satellite_prn.charCodeAt(i), 10)
);
}
I suspect that there may be a function that allows me to convert the binary data into an array, but for an unknown reason Uint8Array didn't appear to be working for me. However the above does.

Categories