Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:36:53+00:00 2026-06-05T17:36:53+00:00

I have some code which takes strings representing hexadecimal numbers – hex colors, actually

  • 0

I have some code which takes strings representing hexadecimal numbers – hex colors, actually – and adds them. For example, adding aaaaaa and 010101 gives the output ababab.
However, my method seems unnecessarily long and complicated:

var hexValue = "aaaaaa";
hexValue = "0x" + hexValue;
hexValue = parseInt(hexValue, 16);
hexValue = hexValue + 0x010101;
hexValue = hexValue.toString(16);
document.write(hexValue); // outputs 'ababab'

The hex value is still a string after concatenating 0x, so then I have to change it to a number, then I can add, and then I have to change it back into hex format! There are even more steps if the number I’m adding to it is a hexadecimal string to begin with, or if you take into consideration that I am removing the # from the hex color before all this starts.

Surely there’s a simpler way to do such simple hexadecimal calculations! And just to be clear, I don’t mean just putting it all on one line like (parseInt("0x"+"aaaaaa",16)+0x010101).toString(16) or using shorthand – I mean actually doing less operations.

Is there some way to get javascript to stop using decimal for all of its mathematical operations and use hex instead? Or is there some other method of making JS work with hex more easily?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:36:55+00:00Added an answer on June 5, 2026 at 5:36 pm

    No, there is no way to tell the JavaScript language to use hex integer format instead of decimal by default. Your code is about as concise as it gets but note that you do not need to prepend the "0x" base indicator when you use "parseInt" with a base.

    Here is how I would approach your problem:

    function addHexColor(c1, c2) {
      var hexStr = (parseInt(c1, 16) + parseInt(c2, 16)).toString(16);
      while (hexStr.length < 6) { hexStr = '0' + hexStr; } // Zero pad.
      return hexStr;
    }
    
    addHexColor('aaaaaa', '010101'); // => 'ababab'
    addHexColor('010101', '010101'); // => '020202'
    

    As mentioned by a commenter, the above solution is chock full of problems, so below is a function that does proper input validation and adds color channels separately while checking for overflow.

    function addHexColor2(c1, c2) {
      const octetsRegex = /^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i
      const m1 = c1.match(octetsRegex)
      const m2 = c2.match(octetsRegex)
      if (!m1 || !m2) {
        throw new Error(`invalid hex color triplet(s): ${c1} / ${c2}`)
      }
      return [1, 2, 3].map(i => {
        const sum = parseInt(m1[i], 16) + parseInt(m2[i], 16)
        if (sum > 0xff) {
          throw new Error(`octet ${i} overflow: ${m1[i]}+${m2[i]}=${sum.toString(16)}`)
        }
        return sum.toString(16).padStart(2, '0')
      }).join('')
    }
    
    addHexColor2('aaaaaa', 'bogus!') // => Error: invalid hex color triplet(s): aaaaaa / bogus!
    addHexColor2('aaaaaa', '606060') // => Error: octet 1 overflow: aa+60=10a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control which takes some strings and displays them in the
I have some code which takes a few minutes to process, it has to
I have some code (it's part of a wordpress plugin) which takes a text
I have some Ruby code which takes dates on the command line in the
I have some code which I would like to pass instances or classes interchangeably.
I have some code which iteratively appends data to a file and looks similar
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance
I have some code which is built both on Windows and Linux. Linux at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.