x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 285

# Calculating values from string

I have an input string "BEA4F1" into which is encoded the following printer colour settings.

10 bits are colour intensity (0 to 1023)
10 bits are colour range ( 0 to 1023)
4 bits are for colour (0 to 15)

So an example message would be  "BEA4F1"

intensity = 762 = 1011111010
range     = 591 = 1001001111
colour    = 1   = 0001

So binary (24 bits = 1011 1110 1010 0100 1111 0001 = "BEA4F1"

how do I get intensity, range and colour values back from string "BEA4F1"?

Thanks

0
oddszone
• 2
1 Solution

Commented:
Code below to get them back in binary
``````string x = hex2binary("BEA4F1");

string intensity = x.Substring(0,10);
string range = x.Substring(10,10);
string colour = x.Substring(20, 4);

static string hex2binary(string hexvalue)
{
string binaryval = "";
binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2);
return binaryval;
}
``````
0

Commented:
Also added the convertion to integer-values
``````static void Main(string[] args)
{
string x = hex2binary("BEA4F1");

string sIntensity = x.Substring(0,10);
string sRange = x.Substring(10,10);
string sColour = x.Substring(20, 4);

int intensity = Convert.ToInt32(sIntensity, 2);
int range = Convert.ToInt32(sRange, 2);
int colour = Convert.ToInt32(sColour, 2);

}

static string hex2binary(string hexvalue)
{
string binaryval = "";
binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2);
return binaryval;
}
``````
0

Author Commented:
Spot on thanks !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.