Convert HEX to RGB in Excel - excel

I have a column "HEX" and three columns "R", "G", and "B".
How can I convert a HEX to RGB, e.g. ff0000 to R=255, G=0, and B=0?
I know that the first 2 characters ff belongs to "R", the next 2 00 belongs to "G", and the final 2 00 belongs to "B". So I will have to use =LEFT(A1, 2) for "R", =RIGHT(LEFT(A1, 4), 2), and =RIGHT(A1, 2) for the last.
But how can I convert ff to 255 and 00 to 0, etc.? I guess I will have to do something to parse from hexadecimal (base 16) to decimal (base 10)?
I would like to do it without VBA.

You can try with the =Hex2Dec(A1) indeed, but you should split its input into 3 parts:
One for R, one for G and one for B, considering that you would always get them in a format like this ff0000.
=HEX2DEC(LEFT(B1,2))&"-"&HEX2DEC(MID(B1,3,2))&"-"&HEX2DEC(RIGHT(B1,2))
This is the result from the formula:

You can convert from hex to decimal using the HEX2DEC() function. For instance:
=HEX2DEC(A1)
Where cell A1 contains the string FF, this will return 255.
More details here.

I have managed to create a excel spreadsheet that converts Hex to RGB in three columns; Red, Green and Blue. Below is the code for it:
Red: =HEX2DEC(LEFT([Cell],2))
Green: =HEX2DEC(MID([Cell],3,2))
Blue: =HEX2DEC(RIGHT([Cell],2))
So far, I have not found any restrictions to this code.
I also made code for RGB to hex:
=DEC2HEX(([Red]*65536)+([Green]*256)+[Blue],6)
I hope this answers your question.

Some pages make that (w3SCHOOL PICKER), but in excel you can make this
vba rgb to hex and inverse
If you try without vba, you'll only can use 56 colors.
(in case use the colors from a cell)

Related

Excel - convert cell from hex string to SHIFT-JIS characters

I need to convert space delineated hex values (ie. "B2 DD C0 B0 C8 AF C4 20 B9 DE B0 D1 81 48") in a column of cells to their SHIFT-JIS character equivalent in Excel. These strings may also include line breaks that need to be included in the translated cell value.
All of the functions and VBA code examples I've located so far appear to only work with western ascii values or unicode, which displays the incorrect characters. Converting by importing from CSV is not a viable solution, since the values are extracted from another hex dump in another worksheet (hex string extracted by using an offset table for length).
Tried using a sample VBA function to convert the hex characters, but it's not able to properly convert the SHIFT-JIS encoding.
=HexToString(SUBSTITUTE(I2,CHAR(32),"")) will result in "²ÝÀ°È¯Ä ¹Þ°ÑH" instead of "インターネット ゲーム?"
Public Function HexToString(InitialString As String) As String
Dim i As Long
For i = 1 To Len(InitialString) Step 2
HexToString = HexToString & Chr("&H" & (Mid(InitialString, i, 2)))
Next i
End Function

convert 16 bit decimal into two 8 bit decimal in excel

I am finding a formula which can convert a 16 bit binary number into two separate decimal number
0000000110010000 -> 0x0190
I want the decimal number to be 1 and 144
I have 50 columns(say M1 to M50) of binary numbers so need to make a generic formula for this
If M1 contains your binary number (as a text string), then use
=BIN2DEC(LEFT(M1, 8))
to extract the left part
and
=BIN2DEC(RIGHT(M1, 8))
to extract the right part.
If you want the result in the same cell then use something like
=BIN2DEC(LEFT(M1, 8)) & "|" & BIN2DEC(RIGHT(M1, 8))
where the | is an arbitrary separator, which you can change or omit to suit personal taste.
are they all exactly 16 characters long? You could do:
=BIN2DEC(RIGHT(M1,8))
=BIN2DEC(LEFT(M1,8))

ASCII text to Hexadecimal in Excel

I want to this but i don't know what to do, the only functions it seems to be useful is "DEC.TO.HEX".
This is the problem, i have in one cell this text:
1234
And in the next cell i want the hexadecimal value of each character, the expected result would be:
31323334
Each character must be represented by two hexadecimal characters. I don't have an idea how to solve this in excel avoiding make a coded program.
Regards!
Edit: Hexadecimal conversion
Text value Ascii Value (Dec) Hexadecimal Value
1 49 31
2 50 32
3 51 33
4 52 34
Please try:
=DEC2HEX(CODE(MID(A1,1,1)))&DEC2HEX(CODE(MID(A1,2,1)))&DEC2HEX(CODE(MID(A1,3,1)))&DEC2HEX(CODE(MID(A1,4,1)))
In your version you might need the .s in the function (and perhaps ;s rather than ,s).
DEC2HEX may be of assistance. Use, as follows:
=DEC2HEX(A3)
First split 1234 to 1 2 3 4 by using MID(), then use Code() for each character, and then again concentate. Below is the formula, Y21 is the cell in which 1234 is written
=CONCATENATE(CODE(MID(Y21,1,1)),CODE(MID(Y21,2,1)),CODE(MID(Y21,3,1)),CODE(MID(Y21,4,1)))
1234 >> 49505152

How do I convert a 64bit number to hexadecimal in excel?

I'm trying DEC2HEX(1000000000050000000) but it comes out as #NUM! as the number is too large for this function.
Is there another function I could use to turn this number into hexadecimal?
If you want to convert a decimal number to a 64 bit hex string, assuming that the decimal number is in cell A1 you can use the following:
=CONCATENATE(DEC2HEX(A1/2^32),DEC2HEX(MOD(A1,2^32),8))
This will work up to decimal value of 18,446,744,073,709,500,000 or hex value of 0xfffffffffffff800.
Bonus:
To convert from hex string to decimal, assuming that the 64bit hex string is in cell A1 and contains 16-characters then you can use the following:
=HEX2DEC(LEFT(A1,8))*2^32+HEX2DEC(RIGHT(A1,8))
You can adjust the number of characters in the LEFT(text,[num_chars]) to better suit your needs.
If your hex string has a 0x then you can use the following:
=HEX2DEC(MID(A1,3,8))*2^32+HEX2DEC(RIGHT(A1,8))
I found a simple solution for converting HEX to DEC and vice versa without the limits of characters.
HEX to DEC: use DECIMAL(input number or cell coordinates, input base number)
Case 1: I want to convert hex value "3C" to decimal, the formula is DECIMAL(3C, 16).
Case 2: I want to convert binary value "1001" to decimal, the formula is DECIMAL(1001, 2).
DEC to HEX: use BASE(input number or cell coordinates, output base number)
Case 1:I want to convert number value "1500" to hexadecimal, the formula is BASE(1500, 16)
Case 2:I want to convert number value "1500" to binary, the formula is BASE(1500, 2)
The DEC2HEX function has a limit of 549,755,813,887, try this formula it works for numbers up to 281,474,976,710,655.
=DEC2HEX(A7/(16^9),3)&DEC2HEX(MOD(A7,16^9),9)
There is a free add-in available that will handle that: Xnumbers
Seems to work OK:
=cvDecBase("1000000000050000000",16) --> DE0B6B3AA5EF080
Long formula but it is working for 64-HEX characters:
=HEX2DEC(MID(A24,1,8))*2^512 *(4) +HEX2DEC(MID(A24,9,8))*2^512 *(2) +HEX2DEC(MID(A24,17,8))*2^512+HEX2DEC(MID(A24,25,8))*2^256+HEX2DEC(MID(A24,33,8))*2^128+HEX2DEC(MID(A24,41,8))*2^64+HEX2DEC(MID(A24,49,8))*2^32+HEX2DEC(MID(A24,57,8))
please note: (*4) = *4 (remove brackets) and: (*2) = *2 (remove brackets)
also note: all 64 character must be present like the following example:
0000000000000000000000000000000000000000000000000000000000000fd1

Python 3 string formatting with filler more than one character long

I am attempting to format a string in such a way, that I can make a repeating sequence of numbers an arbitrary length.
I've been looking at these examples: How do I format a number with a variable number of digits in Python? and String Formatting in Python 3.
Here's what I tried to do:
print("{0:{1}{2}d}".format(0, 0, 8))
will result in eight pretty 0's all in a row like so: 00000000
but attempting to change the second argument from 0 to 25
print("{0:{1}{2}d}".format(0, 25, 8))
Results in an a single 0 that is as far right as it can go in my console instead of 25252525 So I think the issue is using a string with more than one character as filler instead of a single character.
The specification for string formatting goes like this:
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
In this case, we're only interested in the [0][width] part. [0] is an optional parameter which pads numbers with zeros, so you can format 4 as '004'. [width] specifies the total width of the field.
When you write:
print("{0:{1}{2}d}".format(0, 0, 8))
It becomes:
print("{0:08d}".format(0))
Which is a 0 padded with zeroes up to a length of 8: 00000000.
However, your other example:
print("{0:{1}{2}d}".format(0, 25, 8))
Becomes:
print("{0:258d}".format(0))
Which is a 0 padded with spaces (because there is no 0 in the formatter) up to a length of 258.
I think string formatting is not suited to solve your problem. You can use other fill characters than 0 (using the [fill] option in the formatting spec), but it can't be more than one character.
The simplest way to get what you want is probably this:
>>> print((str(25) * 8)[:8])
25252525

Resources