4

I have an 8-bit hex decimal which is in aarrggbb format. I need methods to convert this to rrggbbaa format and vise versa. For eg

ARGB format

#FFFF2323

What i need is RGBA format

#FF2323FF

Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80

2 Answers2

5

If you're dealing with a string #FF123456

let x = '#FF123456';
console.log(x.replace(/#(..)(......)/, '#$2$1'));

If, however, x is a number, 0xFF123456 -

let x = 0xFF123456
console.log(`#${(x & 0x0FFFFFF).toString(16).padStart(6, '0')}${(x >>> 24).toString(16).padStart(2, '0')}`);
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
2

All you have to do is move a[1] and a[2] to the end

var a = "#AABBCCDD";

var b = "#"+a.slice(3,9)+a[1]+a[2];

alynurly
  • 1,669
  • 1
  • 15
  • 15