Convert integer to hexadecimal and back again

// Store integer 182 int intValue = 182; // Convert integer 182 as a hex in a string variable string hexValue = intValue.ToString(“X”); // Convert the hex string back to the number int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber); from http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html HINT (from the comments): Use .ToString(“X4”) to get exactly 4 digits with leading 0, or .ToString(“x4”) … Read more

C++ convert hex string to signed integer

use std::stringstream unsigned int x; std::stringstream ss; ss << std::hex << “fffefffe”; ss >> x; the following example produces -65538 as its result: #include <sstream> #include <iostream> int main() { unsigned int x; std::stringstream ss; ss << std::hex << “fffefffe”; ss >> x; // output it as a signed type std::cout << static_cast<int>(x) << std::endl; … Read more

How to get hex color value rather than RGB value?

TLDR Use this clean one-line function with both rgb and rgba support: const rgba2hex = (rgba) => `#${rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+\.{0,1}\d*))?\)$/).slice(1).map((n, i) => (i === 3 ? Math.round(parseFloat(n) * 255) : parseFloat(n)).toString(16).padStart(2, ‘0’).replace(‘NaN’, ”)).join(”)}` 2021 updated answer Much time has passed since I originally answered this question. Then cool ECMAScript 5 and 2015+ features become largely available on … Read more

Convert hex string to int in Python

Without the 0x prefix, you need to specify the base explicitly, otherwise there’s no way to tell: x = int(“deadbeef”, 16) With the 0x prefix, Python can distinguish hex and decimal automatically. >>> print(int(“0xdeadbeef”, 0)) 3735928559 >>> print(int(“10”, 0)) 10 (You must specify 0 as the base in order to invoke this prefix-guessing behavior; if … Read more