Convert Decimal to Hex or Unicode using Javascript - javascript

the problem i'm facing is when i inputting emoji from an android device to display on a browser, the emoji is converted its form of Decimal code like 💙 💙 💙. is there any way to detect the decimal code then convert it to Hex or Unicode with javascript? because with Hashtag in front of the number, it might lead to confusion as hashtag input.
I've used the toString() method but it doesn't seem to solve the problem
decimalNumber.toString( radix )

maybe use \u---- where ---- is hexacode ( in utf8 string) ?

Related

JavaScript function parseFloat() fails for negative values in certain locales

While working with locale translation and parsing decimal numbers from text to numerical values in Angular 10, I came across the following problem:
Consider the string value value = "-35.17 %". I want to convert this a numerical value using parseFloat(value). This works fine for application locale en-US.
However, if the user changes application locale to nb-NO (Norwegian), the parsing fails, resulting in a NaN.
The reason for this is that the Norwegian locale uses a different character for the negative prefix (− instead of -).
The workaround for this particular issue is simple, by performing a .replace("−", "-") on the string before parsing, but shouldn't JavaScript be able to handle parsing of both these characters? Is it only safe to perform parsing on locale en-US?
The JavaScript function parseFloat() needs an input string that meets certain requirements, including (but not limited to) the following:
If parseFloat encounters a character other than a plus sign (+), minus sign (- U+002D HYPHEN-MINUS), numeral (0–9), decimal point (.), or exponent (e or E), it returns the value up to that character, ignoring the invalid character and characters following it.
Localized strings may contain characters that does not meet those requirements.

Add trailing zeros to an integer without converting to string in JS?

I'm looking to add decimals to the end of my integer. As an example:
15 => 15.00
The problem with methods like toFixed is that it will convert it into a string. I've tried to use parseFloat() and Number() on the string, but it'll convert it back to an integer with no decimals.
Is this possible? If not, can someone explain to me the logic behind why this isn't possible?
EDIT: Welp the intent was to display the number as a number, but from the going consensus, it looks like the way the only way to go about it is to use a string. Found an answer on the why: https://stackoverflow.com/a/17811916/8869701
The problem you are finding is that all numbers in javascript are floats.
a = 0.1
typeof a # "number"
b = 1
typeof b # number
They are the same.
So there is no real way to convert to from an integer to a float.
This is the reason that all of the parseFloat etc are string methods for reading and writing numbers from strings. Even if you did have floats and integers, specifying the precision of a number only really makes sense when you are displaying it to a user, and for this purpose it will be converted to a string anyway.
Depending on your exact use case you will need to use strings if you want to display with a defined precision.
When working with numbers 15 and 15.00 are equal. It wouldn't make any sense to use memory to store those trailing or leading zeros.
If that information is needed it is usually for displaying purposes. In that case a string is the right choice.
In case you need that value again you can parse the string as a number.

reverse base58 encoding - decoding?

On this site forknote creator in the second input box you enter a decimal value and it display the decoded prefix on the right.
Is it possible to reverse the js function so that you can enter text ie "BOB" and it displays the decimal value?
I think the "BOB" need to be converted to bytes? then to base 58 hex and convert the hex to dec?
Thanks
Every value encoded by baseXX function can be decoded. But this answer doesn't solve your problem. I looked at the code behind this input and it's way more complicated and has many cryptographic functions. I don't know if it's reversible.

Data Types Validation in Javascript

I tried to search here but seems I can't find the best answer to my problem.
How can I validate if the user input is double, Float or Long (data types in JAVA) in javascript?
If you want to check for an object type you can use typeof keyword of javascript. For example if you want to check for a number you can do something like this:
typeof i === 'number'
or using regex for floating types:
^\d{0,2}(\.\d{0,2}){0,1}$
I don't think there is such a difference in javascript.
Does JavaScript have double floating point number precision?
There are not such types in javascript.
The types you said in javascript is a primitive data type called Number.
In JavaScript, all numbers are 64-bit floats. Functions like parseInt() treat their input like a signed int, but create a float. And bit-wise operators recreate the same behaviour you would expect with ints, but on floats.
The Javascript number primitive can't represent the range of values that the combination of Javas int, long and double can. If you really need to validate this anyway you could always write some kind of hack. For example, for the Java long you could:
store javaLongMaxValue as a string,
interpret your input number as a string,
left pad your input string with "0" until it is javaLongMaxValue.length long
and then compare the strings.
You would of course have to validate that the input can be interpreted as a number and handle the case when it's negative.
use if statements and parse it
parseInt('var');
parseDouble('var');
so on..
example..
if(parseInt('var')){
// code above checks if it is a integer.. returns 'true' if yes and 'false' if not
// then your code
}else if(parseDouble('var')){
// your code
}
.. so on

JSON.parse parses / converts big numbers incorrectly

My problem is really simple but I'm not sure if there's a "native" solution using JSON.parse.
I receive this string from an API :
{ "key" : -922271061845347495 }
When I'm using JSON.parse on this string, it turns into this object:
{ "key" : -922271061845347500 }
As you can see, the parsing stops when the number is too long (you can check this behavior here). It has only 15 exact digits, the last one is rounded and those after are set to 0. Is there a "native" solution to keep the exact value ? (it's an ID so I can't round it)
I know I can use regex to solve this problem but I'd prefer to use a "native" method if it exists.
Your assumption that the parsing stops after certain digits is incorrect.
It says here:
In JavaScript all numbers are floating-point numbers. JavaScript uses
the standard 8 byte IEEE floating-point numeric format, which means
the range is from:
±1.7976931348623157 x 10308 - very large, and ±5 x 10-324 - very small.
As JavaScript uses floating-point numbers the accuracy is only assured
for integers between: -9007199254740992 (-253) and 9007199254740992
(253)
You number lies outside the "accurate" range hence it is converted to the nearest representation of the JavaScript number. Any attempt to evaluate this number (using JSON.parse, eval, parseInt) will cause data loss. I therefore recommend that you pass the key as a string. If you do not control the API, file a feature request.
The number is too big to be parsed correctly.
One solution is:
Preprocessing your string from API to convert it into string before parsing.
Preform normal parsing
Optionally, you could convert it back into number for your own purpose.
Here is the RegExp to convert all numbers in your string (proceeded with :) into strings:
// convert all number fields into strings to maintain precision
// : 922271061845347495, => : "922271061845347495",
stringFromApi = stringFromApi.replace(/:\s*(-?\d+),/g, ': "$1",');
Regex explanation:
\s* any number of spaces
-? one or zero '-' symbols (negative number support)
\d+ one or more digits
(...) will be put in the $1 variable

Categories