Converting UUIDV4 to decimal on base 16 - javascript

I don't want to share my primary key in the API, So I used UUID4 to generate unique row id.
But I need to find that row by using generated uuid which may cause performance issues as it's string and also length is too long.
I tried converting this uuid to decimal on base 16.
const uuid = UUIDV4() //57d419d7-8ab9-4edf-9945-f9a1b3602c93
const uuidToInt = parseInt(uuid, 16)
console.log(uuidToInt) //1473518039
By default it is only converting first chunk to decimal
Is safe to use it this way?
How much possibility is there to loose uniqueness of the row?

I tried converting this uuid to decimal on base 16.
decimal or hexadecimal. A number can't be both. Besides that, the uuid is already a hexadecimal format.
That's how you can convert it into a decimal value.
var uuid = "57d419d7-8ab9-4edf-9945-f9a1b3602c93";
var hex = "0x" + uuid.replace(/-/g, "");
var decimal = BigInt(hex).toString(); // don't convert this to a number.
var base64 = btoa(hex.slice(2).replace(/../g, v => String.fromCharCode(parseInt(v, 16))));
console.log({
uuid,
hex,
decimal,
base64
});
Careful, don't convert the BigInt value to a regular number, JS Numbers can not deal with values that big. They have only 53bits of precision. You'll lose the 75 least significant bits of your uuid.
Edit: added base64.

Is safe to use it this way?
That depends on your definition of safe.
How much possibility is there to loose uniqueness of the row?
A UUIDv4 has 128 bits, so there are 2128 theoretical possible combinations.
So that's 18'446'744'073'709'551'616 possible UUIDs.
Taking the first section of an UUID leaves you with 32 bits which gives you 232 possible combinations: 4'294'967'296.

Related

how to convert a number to float with 8 point decimal places in javascript

How can i convert a number, a input from my test case which will be of either integer or float, into a float/string number of always with 8 decimal places? say for example, if my input is 3, then i should convert into '3.00000000', if my input is 53.678, then i should convert into '53.67800000'. I have googled and tried with few conversion types like parsing, toPrecision() but could not convert it. Any help is much appreciated.
expect(a).to.equal(b) // a and be should be of same number with types too
expect(a).to.equal(b)
In JavaScript, Number is a double-precision float.
Its precision cannot be expressed in decimal places, it varies depending on how big the number is. Above Number.MAX_SAFE_INTEGER, for example, the precision is "less than 0 decimal places":
const large = 9007199254740992;
const larger = large + 1;
console.log(large === larger);
To convert a Number to a String with a fixed number of decimal places, use .toFixed(), as #epascarello suggested:
const input = 3;
const str = input.toFixed(8);
console.log(str);
As for doing financial calculations, some say you should never use IEEE 754 floats (such as JavaScript's Numbers), although many of the largest companies in finance do just that.
To be on the safe side, use a bignum library such as big.js.

How to Read 20 bit integer in javascript?

In JavaScript
I am having a variable of 20 bit(16287008619584270370) and I want to convert it into binary of 64 bit but when I used the binary conversion code(Mentioned below) then it doesn't show me real binary of 64 bit.
var tag = 16287008619584270370;
var binary = parseInt(tag, 10).toString(2);
After dec2bin code implementation:
-1110001000000111000101000000110000011000000010111011000000000000
The correct binary should be:
-1110001000000111000101000000110000011000000010111011000011000010
(last 8 binary changed)
When I checked the problem then I get to know that code only reads the variable up to 16 bit after that it assumes 0000 and shows the binary of this (16287008619584270000).
So finally i need a code from anywhere that convert my whole 20 bit number into its actual binary in java Script.
The problem arises because of the limited precision of 64-bit floating point representation. Already when you do:
var tag = 16287008619584270370;
... you have lost precision. If you output that number you'll notice it will be 370 less. JS cannot represent the given number in its number data type.
You can use the BigNumber library (or the many alternatives):
const tag = BigNumber("16287008619584270370");
console.log(tag.toString(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/8.0.1/bignumber.min.js"></script>
Make sure to pass large numbers as strings, as otherwise you already lose precision even before you started.
Future
At the time of writing the proposal for a native BigInt is at stage 3, "BigInt has been shipped in Chrome and is underway in Node, Firefox, and Safari."
That change includes a language extension introducing BigInt literals that have an "n" suffix:
var tag = 16287008619584270370n;
To read more than 16 chars we use BigInt instead of int.
var tag = BigInt("16287008619584270370"); // as string
var binary = tag.toString(2);
console.log(binary);

How to convert from Base36 to Base10 in JS

I have a base36 number 00001CGUMZYCB99J
But if I try convert it in JavaScript to base 10 with
parseInt("00001CGUMZYCB99J", 36);
I get wrong results like 177207000002463650 or 177207000002463648. The expected result is 177207000002463655. I found two websites that get the result right anyway: translatorscafe and dcode.
But how can I do this in JS?
The outcome of that conversion exceeds Number.MAX_SAFE_INTEGER (i.e. the base-10 value is too large to fit in a JavaScript integer), which means you need some sort of arbitrary precision library to do the conversion, for instance biginteger:
const BigInteger = require('biginteger').BigInteger;
let value = BigInteger.parse('00001CGUMZYCB99J', 36);
console.log( value.toString() ) // 177207000002463655
JavaScript stores all values in a double. Therefore, large numbers will have some of the less significant digits changed. If you want to deal with large numbers, you have to use a special library like this BigInteger one.
If you use this library, you can convert between bases like this:
BigInteger.parse("00001CGUMZYCB99J", 36);
Keep in mind that you need to keep using the library, you can't convert it back into a normal number, or you will face the same problem.

How to convert hex bit pattern (as string) to float in Node.js? [duplicate]

This question already has an answer here:
Convert ieee754 to decimal in node
(1 answer)
Closed 6 years ago.
Let's say I have a hex value as a string:
var hexValue = "0x43480000";
How can I parse it in Node.js and convert it to a number (float)?
Like:
var parsedNumber = hexStringToNumber(hexValue); // -> 200.0
Note that the hex value is meant to be a bit pattern for an IEEE-754 floating-point number. So the result is 200, not 1128792064 as it would be with just parseInt("0x43480000").
The Node.js Buffer class provides this feature:
var buf = new Buffer("43480000", "hex");
var number = buf.readFloatBE(0);
console.log(number); // 200
Also see:
Convert ieee754 to decimal in node
Encoding and decoding IEEE 754 floats in JavaScript
(The below obsoleted by the clarification by the OP.)
parseInt (MDN | spec) is perfectly happy to parse hex in that format for you:
var hexValue = parseInt("0x43480000");
console.log(hexValue);
It will see the 0x at the beginning and assume hex. If you had a string in hex without the prefix ("43480000"), you'd use parseInt(str, 16) to tell parseInt to use base 16 (hex). But since you have the prefix, there's no need.
The result will be a JavaScript number, which by definition is a "double" (IEEE-754 double-precision binary floating point).
You've said you want 0x43480000 to come out as 200.0. That means you're not really parsing hex, you're trying to take a string containing the bit pattern of an IEEE-754 floating-point number, expressed as hex, and get the equivalent IEEE-754 floating-point number. That's very different from "parsing hex."
The calculator you referred to works with IEEE-754 single-precision binary floating point values. The 0x43480000 bit pattern isn't 200 in double-precision binary floating point. But 0x40690000 is.
The only easy way I know of to do that is with DataView:
var dv = new DataView(new ArrayBuffer(8));
dv.setUint32(0, parseInt("0x40690000"));
var num = dv.getFloat64(0)
console.log(num);
If you want to convert the single precision version (0x43480000), I don't think there's anything built-in that's going to do it for you. You'll have to isolate the mantissa and exponent bits, do the widening, and then do the above.

Why isn't value obtained after Masking accurate in JavaScript?

I was trying to extract last 32 bits from a Hex number in Javascript.
var hex = 0x6C469F301DBBC30;
var last32bit = (hex & 0xFFFFFFFF).toString(16);
log(last32bit); //gives 1dbbc40
The result is 1DBBC40. Shouldn't this supposed to be 1DBBC30 ?
Also how do I preserve the 0 before 1DBBC40?
The ECMA standard says:
The Number type has exactly 18437736874454810627 (that is, 264−253+3)
values
Your number is too large to be represented exactly by a Number. You should look for a big number library if you wish to accurately represent large numbers. Perhaps one of these libraries would meet your needs.

Categories