You have a common but ultimately wrong model of what color is.
A series of numbers represented by a hex value is not a color! It is a device specific instruction. Each monitor naturally shows a different color for that value. CMYK values are the same. So what your asking is how do I convert one uncertain thing to another uncertain thing.
You can however make a device show reasonably close to same color if you profile and or calibrate the output device. You can then map the number to a color and vice versa. We can naively understand web color as meaning sRGB color space. It does not mean any of uncalibrated device shows this color but at least there is now a robust meaning for the numeric values.
You can think of the color code as a instruction like "go to the central railway station and move 500 m along the southern road towards west". That is all good and well, but i have neglected to tell what city i am referring to. Naturally i end up in different places in different cities.
Now this is where your problems really begin. So one might ask what am going to do if the color can not be represented. This is an all too common problem, in fact it is nearly guaranteed to be the problem. You can solve this in many ways. How you solve this depends on what you mean color to be. One way of solving this is to just find the nearest color. This is fine if you design a limited color range. However one could take the view that due ti the human auto white balance this is not needed and you can account for that, OR because the entire color vision is relative to nearby colors you could just scale the whole used space to fit the new one. Or failing all that you might just choose the brightest most saturated color available. Or any method that you can think of.
So in addition to profile of starting space, profile of target space you need the conversion intent. Standard defines 4 intents (absolute colorimetric, relative colorimetric, perceptual and presentation) but any number of intents could be devised.
So at the end of the day each color has potentially 4 different standard solutions. Off course each color management engine is slightly different so the results vary widely. And RGB to CMYK is especially un-defined as there is 2 different ways to generate the black colors (unlike RGB, CMYK can generate the same(ish) color in several ways).
So how would you do the conversion. Well if you really want any semblance of accuracy then you would realize there is no formula. Instead there are unlimited number of formulas depending on profile. So you need a system that understand profiles - a color management system. You can get commercial ones, use the one that ships with your os or use a free ware one like little cms.