最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Convert ARGB to RGBA format - Stack Overflow

matteradmin6PV0评论

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

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

Share Improve this question asked Jun 26, 2018 at 7:39 Sachila RanawakaSachila Ranawaka 41.5k8 gold badges61 silver badges87 bronze badges 3
  • 3 so, you have a string #FFFF2323? you want '#FFFF2323'.replace(/#(..)(......)/, '#$2$1') – Jaromanda X Commented Jun 26, 2018 at 7:41
  • @JaromandaX Very elegant solution. Please make it an answer to the question :-) – Emil S. Jørgensen Commented Jun 26, 2018 at 7:45
  • where did that non regex answer go! It was fine – Jaromanda X Commented Jun 26, 2018 at 7:51
Add a ment  | 

2 Answers 2

Reset to default 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')}`);

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];

Post a comment

comment list (0)

  1. No comments so far