I'm implementing the Keccak-f ι step using javascript, the formula is as follow:
# ι step
A[0,0] = A[0,0] xor RC
RC is an array of round constants in Hex and A[0,0] is a single bit in Binary
The problem occurs on the following line
A[0,0] = 0, RC = 0x0000000000008082
when I convert RC to Binary using the code:
let temp = (parseInt(RC, 16).toString(2)).padStart(8, '0');//110010100010011000
A[0][0] = A[0][0] ^ temp; //16975297
the result is not a single bit, rather is a large number 16975297, but I need a single bit.
I tried to convert the large number 16975297 to binary but it is not a single bit as well.
I need some help, thank you!
Having a problem here. I recieve data from hardware as HEX numbers. I use this to make them into strings(it is intended):
arr.push(data.charCodeAt(0).toString(16))
It works well. For example I recieve 0x00 and this code correctly adds a '0' string to the array.
But there is a problem. JS automatically understands these codes as symbols. And when I try 0xAA or 0x80 I recieve FFFD. As I understand. it means that there is no such symbol in utf-8.
How do I make js understand my data as hex-numbers, but not as symbol codes?
Edit: I figured out that my problem was in my wrong way of using the library which recieved data from hardware.
Just convert the Hex number to decimal
yourNumber = parseInt(hexString, 16);
To convert hexadecimal to decimal you can use the next function:
parseInt(hexValue, 16)
Hope this helps :)
data = '0XAA'
data1 = '0X80'
console.log( parseInt(data, 16))
console.log( parseInt(data1, 16))
Okay, I figured out that my problem was in a library I used to recieve data
I know it's quite low quality of question but I get little bit confused now.
Here is the thing What I want to do.
100000 => 100.000
9997080000 => 9997080.000
I want to cut from the third decimal without rounding.
How can I do this? I used the toFixed() but all I want to do is just cut from third decimal. I think I'm complicated now.
It will be simple. Plz let me know. Thanks
From what I understand, this is what you want:
var number = "9002764000";
var result = number.slice(0, -3) +"."+ number.slice(-3);
console.log(result);
This will add a . after the last three digits i.e 9002764000 -> 9002764.000
https://jsfiddle.net/5yqhr7mo/
Hope it helps!
It sounds like you want a value for display. If so, you want to turn the number into a string (since numbers don't intrinsically have any particular number of digits to the right of the decimal). You can then easily insert the . before the last three digits using substring and substr:
function formatForDisplay(num) {
var str = String(num);
return str.substring(0, str.length - 3) + "." + str.substr(-3);
}
function test(num) {
console.log(num, "=>", formatForDisplay(num));
}
test(100000);
test(9997080000);
Alternatively, you could use String#replace and add a dot.
var number = 9002764000;
console.log(Math.floor(number).toString().replace(/(?=...$)/, '.'));
what about :
function formatNumber(num){
num=num/1000;
return num.toFixed(3);
}
console.log(formatNumber(9997080000));
console.log(formatNumber(100000000));
which will return what you expect if you work with integer
Math.floor() will round down essentially cutting off everything to the right of decimal point.
I want to send the data captured from my microphone (converted to unsigned 16bit integers) in the browser to my server as a binary string, but I'm having a hard time doing that.
I tried using String.fromCodePoint on my array, but it seems that the resulting String is not a valid one. I also tried using DataView, but not sure how to get a binary String out of that either.
Does anyone know how that can be achieved?
EDIT: I am referring to binary data, as is "binary file", and not to "binary representation of an integer".
This did not answer the question.
If only in the browser you can use the btoa function to encode to a base64 string.
Edit:
I am assuming you have an array of integers.
Final Edit:
Maybe try something like:
arr.map(function(item) {
var value = item.toString(2);
var padLength = 16 - value.length;
var binaryVal = "0".repeat(padLength) + value;
return binaryVal;
}).join("");
Firstly - my description ;)
I've got a XmlHttpRequests JSON response from the server.
MySQL driver outputs all data as string and PHP returns it as it is, so any integer is returned as string, therefore:
Is there any fast alternative (hack) for parseInt() function in JS which can parse pure numeric string, e.g.
var foo = {"bar": "123"};
...
foo.bar = parseInt(foo.bar); // (int) 123
To convert to an integer simply use the unary + operator, it should be the fastest way:
var int = +string;
Conversions to other types can be done in a similar manner:
var string = otherType + "";
var bool = !!anything;
More info.
Type casting in JavaScript is done via the constructor functions of the built-in types without new, ie
foo.bar = Number(foo.bar);
This differs from parseInt() in several ways:
leading zeros won't trigger octal mode
floating point values will be parsed as well
the whole string is parsed, i.e. if it contains additional non-numeric characters, the return value will be NaN
First off, have you actually documented that it's slow and is causing problems? Otherwise, I wouldn't bother looking for a solution, because there really isn't a problem.
Secondly, I would guess that since parseInt is a native JS-method, it would be implemented in a way that is very fast, and probably in the native language of the VM (probably C, depending on the browser/VM). I think you could have some trouble making a faster method out of pure JS. =)
Of course, I'm not a JS guru, so I don't know for sure, but this is what my intuition tells me, and tends to be the standard answer to "how would I make a faster alternative for libraryFunction()?" questions.
Cast it to an int in PHP before you json_encode() it:
$foo->bar = (int)$foo->bar;
print('var foo = ' . json_encode($foo));
Incidentally, when using parseInt it's good practice to always specify the second parameter unless you really want string starting with 0 to be interpreted as octal and so on:
parseInt('010', 10); // 10
Fast shortcut to parseInt is
("78.5" | 0) //bitwise or forces the string to parse as int
This is what ASM uses to represent ints in js.
You aren't going to get better than parseInt, but the real bug is that the PHP is providing what is supposed to be a number as a string.
And ditto to what Daniel said - don't go looking for micro-optimisations like this until you have benchmarked your code and discovered that it's worth doing.
The Number constructor also exists, but it should be the same as parseInt in term of speed (as already said you should correct the PHP part instead of the javascript one anyway) :
var i = "123";
i = new Number(i); // Number numeric wrapper
var j = "123";
j = Number(j); // Number primitive
BTW if someone is interested i searched by curiosity for the V8 (Google chrome) implementation of parseInt and it's here on google code.
How slow can it be? How many times per second is this process being called? How many different numeric return values are there? I whipped together a script and tested 100,000 numbers. Parsing them from strings took 687ms. Searching them in an array took 541ms. That's a very small improvement. I agree with other posters. You may not get better than the native parseInt() method.
Casting is a wee bit faster than parsing but slower than searching.
Also, in Firefox the fastest method turns out to be parseInt() followed by searching. Firefox also turned out to be 6 times faster on average than IE. Interesting.
Cool idea using the unary operator. In Firefox that turned out to be comparable to parseInt(). In IE it turned out to be the fastest method.
if the objects are larger you could try JSON, it is a typed format so you do not need to convert the values.
This solution is faster than parseInt() if you parse strings of decimal integer that is 20 or less in length. For some browser, you may still be faster than parseInt() up to 33 digits in length. Also, you still be faster than auto-cast.
It is because, the parseInt() for the browser does take some time to warm up, so if you only using a simple method to parse, you beat it for a while until it catches up. Don't use this for nodeJS though. When run parseInt() from nodeJS, it is startup time is a lot less than when running from a browser.
45 is the '-' sign in ASCII, 43 is the '+' sign in ASCII. 48 is '0'. Only 48 to 57 xor 48 become 0 - 9(in their order). No other numbers xor 48 yields 0-9.
This will return undefined if the string is not a valid decimal integer string or if the string is empty. It throws a string with value "Not a string" if the input is not of type string.
var toNumber = function (input) {
if ( typeof input !== "string" ) throw "Not a string";
var length = input.length;
if ( length === 0 ) return;
var c1 = input.charCodeAt(0);
if ( c1 === 45 || c1 === 43 ){
if ( length === 1 ) return;
var start = 1;
} else {
var start = 0;
}
var out = 0, c;
while( start < length && input.charCodeAt(start) === 48 ) start++;
for ( ; start < length; start++){
c = input.charCodeAt(start) ^ 48;
if ( c > 9 ) return;
out = (out * 10) + c;
}
if ( c1 === 45 ) return out * -1;
return out;
}