user2512053
user2512053

Reputation: 79

Node js convert ascii to decimall

I have an array that looks contains say [6 5 7 7], however it is stored in ascii so it is [54 53 55 55]. I want to have it as the actual decimal values not ascii. I feel like the conversion back to decimal is simple, but I am unsure what the comman would be. thanks

Upvotes: 0

Views: 2319

Answers (2)

cybersam
cybersam

Reputation: 66999

Assuming the array variable is named arr, the following statement will convert every element to a Number:

arr.map(Number);

Upvotes: 1

mbcrute
mbcrute

Reputation: 1127

If you have the ASCII values in an array called asciiValues you can do something like this:

var asciiValues = [54, 53, 55, 55];

var decimalValues = asciiValues.map(function(val) {
    return Number(String.fromCharCode(val));
});

console.log(decimalValues); // [6, 5, 7, 7]

String.fromCharCode will take the ASCII code and return the string representation. You can then use the Number constructor to turn it into an actual number.

Upvotes: 2

Related Questions