Solved

Calculating values from string

Posted on 2011-03-09
3
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to remove duplicate code from my project 5 66
C# Formatting DateTime String Value 5 69
What are MicroServices? 4 69
Need a quick overview of Selenium testing 2 36
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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