Link to home
Start Free TrialLog in
Avatar of oddszone
oddszoneFlag for Switzerland

asked on

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
         
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

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;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oddszone

ASKER

Spot on thanks !