Hexadecimal Arithmetic Operations with Jquery Possible? - javascript

I am receiving numbers in Hex format. I want to do arithmetic operations with these without having to convert them to decimal and back again? It would be something like:
var a= a23b
var b = f65
var c = a + b
Thanks!

You don't need jQuery - Javascript can do this.
If your numbers are hex you can do:
var a= 0xa23b;
var b = 0xf65;
var c = a+b;
if you need to convert from some string data you can use parseInt() first:
var a = parseInt('a23b',16);
.
.
And finally, if you need to display a hex result convert to a hex string with .toString;
var aDisplay = a.toString(16); // Plain hex number
or
var aDisplay = '0x'+a.toString(16); // hex number with '0x' prefix

Related

ParseFloat not working

i am trying to parse a float in javascript in this way:
var number = '25.492.381';
var cost = parseFloat(number);
This returns 25.492
I wish this would return 25.492.381
How can this be done?
Remember that . in standard notation split the Integer and Fractional part of the number. You could want this:
var number = '25.492.381'.replace('.', '').replace(",",".") ;
var cost = parseFloat(number);
25.492.381 is not a float. You need to use a formatter.
Check Numbro

How to get ASCII of number in JavaScript?

I am aware of name.charCodeAt(0). I am having issued with the following code, so I want a solution for below.
var number= 2;
var t = number.charCodeAt(0);
console.log(t);
The answer needs to be in ASCII. I am getting the log as 2 and not as ASCII value 50. What can be the issue?
ASCII is a way of representing String data. You need to first convert your Number to String;
Remember also that String can have arbitrary length so you'll need to iterate over every character.
var x = 2,
str_x = '' + x,
chrs = Array.prototype.map.call(str_x, function (e) {return e.charCodeAt(0);});
chrs; // [50]
Finally, JavaScript works with UTF-16/UCS-2 and not plain ASCII. Luckily the values for digits are the same in both so you don't have to do any further transformation here.
You have to cast a number to a string first to use .charCodeAt to get the numerical character code.
var number = 2;
var t = String( number ).charCodeAt( 0 );
console.log( t ); // 50
Just convert the number to String and call the method on it.
var number = 2;
var numberInString = number.toString();
var code = numberInString.charCodeAt(0);
console.log(code);

javascript Convert string representation of hex value to hex

In Javascript, how do I convert a string representation of a hex value into it's hex representation ?
What I have returning from a checksum routine is a string value "FE". What I need is it's hex representation "\xFE"
I cannot simply do this, as it gives me an error:
var crc = "FE";
var hex = "\x" + crc;
This just gives me a new 4 character ASCII string:
var crc = "FE";
var hex = "0x" + "FE";
thxs for any guidance.
like this
var hex = parseInt("FF", 16);
For the string \xFE, escape the backslash: var hex = '\\x'+'FE'
To convert 'FE' to a Number use +('0xFE')
To show +('0xFE') as a hexadecimal, use (224).toString(16), or '0x'+((254).toString(16))

How to get specific part of a strings from a string using javascript?

I have a RGBA color in this format:
RGBA:1.000000,0.003922,0.003922,0.003922
How can I separate each value from this string such as:
var alpha = 1.000000;
var red = 0.003922;
var green = 0.003922;
var blue = 0.003922;
I want to do this in javascript.
There is no need to use jQuery. It is rather straightforward JavaScript operation. The easiest way is to use String.prototype.split() method:
var rgba = '1.000000,0.003922,0.003922,0.003922'.split(',');
console.log(rgba[0]); // "1.000000"
console.log(rgba[1]); // "0.003922"
console.log(rgba[2]); // "0.003922"
console.log(rgba[3]); // "0.003922"
To get numbers instead of strings you may use parseFloat() or a shortcut + trick:
var red = parseFloat(rgba[0]); // 1.000000
var green = +rgba[1]; // 0.003922
If your string contains extra data you may either first remove it with replace():
var str = 'RGBA:1.000000,0.003922,0.003922,0.003922'.replace('RGBA:', ''),
rgba = str.split(',');
or use regular expression to match numbers:
var rgba = 'RGBA:1.000000,0.003922,0.003922,0.003922'.match(/\d+\.\d+/g);
>> ["1.000000", "0.003922", "0.003922", "0.003922"]

JavaScript: How to convert an HTML string into a JavaScript number?

I have a number that I need to pull from my html:
<span>123,456.78</span>
How can I convert this string into a number that I can do math on?
var numberString = $('span').text();
var realNumber = Number(numberString); //returns NaN
A jQuery-only solution would be okay.
parseInt() or parseFloat() would just about do it.
var number = parseFloat($('span').text());
after checking and seeing this doesn't work...
try
var number =
$('span').text().replace(/([^0-9\.])/g,"");
var number = parseFloat($('span').text().replace(/([^0-9\\.])/g,""));
I'm not sure what realNumber does, but here's how I'd convert that string into a number:
var numberString = $('span').text();
var amount = + numberString.replace(/,/g, '');
This removes the commas, then uses the + unary operator to convert the string to a number. In your example, the result is the number 123456.78.
updated
var numberString = $('span').text();
var number = Number(numberString.replace(/[^0-9\.]+/g,""));

Categories

Resources