Solved

Calculating values from string

Posted on 2011-03-09
3
248 Views
Last Modified: 2012-05-11
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
Comment
Question by:oddszone
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35082466
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

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35082496
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;
        }

Open in new window

0
 

Author Closing Comment

by:oddszone
ID: 35082635
Spot on thanks !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now