How to convert a byte array to a hex string in Java?

From the discussion here, and especially this answer, this is the function I currently use: My own tiny benchmarks (a million bytes a thousand times, 256 bytes 10 million times) showed it to be much faster than any other alternative, about half the time on long arrays. Compared to the answer I took it from, … Read more

Integer to hex string in C++

Use <iomanip>‘s std::hex. If you print, just send it to std::cout, if not, then use std::stringstream You can prepend the first << with << “0x” or whatever you like if you wish. Other manips of interest are std::oct (octal) and std::dec (back to decimal). One problem you may encounter is the fact that this produces … Read more

RGB to hex and hex to RGB

Note: both versions of rgbToHex expect integer values for r, g and b, so you’ll need to do your own rounding if you have non-integer values. The following will do to the RGB to hex conversion and add any required zero padding: Converting the other way: Finally, an alternative version of rgbToHex(), as discussed in @casablanca’s answer and suggested in the comments by @cwolves: … 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: With the 0x prefix, Python can distinguish hex and decimal automatically. (You must specify 0 as the base in order to invoke this prefix-guessing behavior; if you omit the second parameter int() will assume base-10.)