Need help in number conversions

I’m trying to do some number conversions as below. Could someone please help me? I think I can do this a long way. I’d like to know if there is a simpler solution.

Let’s suppose there is a 32-bit hardware register called R.
R[31:15] = x
R[14:7] = y
R[6:0] = z

A user enters 3 decimal numbers: x, y, and z. The program then displays the resulting value of R[31:0] as a hex value. Since x is a 17-bit number, y an 8-bit, and z a 7-bit number, the range for each are different: x = {0, …, 131071}, y = {0, …, 255}, z = {0, …, 127}.

Example)
So, if I type in x = 255, y = 127, z = 63, it should display 0x007FBFBF (if I calculated this correctly).

I’d also like to be able to perform a reverse operation. The user enters a 32-bit hex value and the program slices it to 3 separate decimal numbers x, y, & z.

What’d be the easiest way to do this? I’m thinking of converting each decimal value to binary, concatenating them into one binary number, and then converting that into a hex value. Would this be the only way? Are there pre-built converts for decimal-to-binary, binary-to-hex, etc? I couldn’t locate it in JUCE doc.

Thanks for your help.

Surely you just need the basic c++ bitshift operators?

String::toHexString might also be what you’re looking for - or use BitArray::setRange to create your numbers. There’s loads of functions in there that might be useful, I’m surprised you say you can’t find them!

Jules, thanks for replying to such a newbie question. I don’t know how to use the doc very well right now :oops: I’ll learn.

This is probably very common thing to do for embedded programming and driver coding to configure hardware register settings. Just wanted to check with the experts if this is how they typically deal with number conversions.

I’m trying to impress my engineering colleagues at work, but it’s not going very well so far… :cry:

You could try using a C bitfield; it’s a standard C feature. Here’s Microsoft’s documentation:

http://msdn2.microsoft.com/en-us/library/yszfawxh(VS.80).aspx