PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] How to edit the values of hexadecimal integer in Java?

 July 20, 2022     colors, integer, java, string     No comments   

Issue

I'm trying to edit values in a hexadecimal in Java.

EDIT: By the way, the hexadecimal contains not just RGB but also alpha.

For example:

int j = 0xAARRGGBB

How would I change the value of AA or RR after the integer has been created?

I couldn't find this question answered anywhere on Google.

Thank you so much for the help!


Solution

Here is all you need I guess to do that

public static int toARGB(int a,int r,int g,int b){
  return ((((((a << 8) | r) << 8) | g) << 8) | b);
}

public static int setB(int origin,int value){
  return set(origin,0,value);
}
public static int setG(int origin,int value){
  return set(origin,1,value);
}
public static int setR(int origin,int value){
  return set(origin,2,value);
}

public static int setA(int origin,int value){
  return set(origin,3,value);
}

public static int set(int origin,int pos,int value){
  return (origin & ~(0xFF << (pos * 8)) | value << (8 * pos));
}


Answered By - Julian Kreuzer
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing