Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends. I need a hex calculator. I could not do. What is your suggestion. What I want is exactly like this ;

for example;

C#
Hexdata = "2020202020442042497c31323334414243353637387c";


calculation;

20+20+20+20+20+44+20+42+49+7c+31+32+33+34+41+42+43+35+36+37+38+7c = 4f1

C#
result= "4f1";


C#
private void HexCalc(string data) 
{

 //Code Here.....

}


Thanks for your help. I apologize for the bad english and the narration.

What I have tried:

C#
string hex;
string result;
int count;

 private void button2_Click(object sender, EventArgs e)
        {
            string data = dataBox.Text;
            string hexdata = ConvertToHex(data);

            for (var i = 0; i< hexdata.Length; i+= 2)
            {
                hex = hexdata.Substring(i, 2);
                count += 2;
                result = Convert.ToString(toplam + hexdata.Substring(count,2));
                if(i == hexdata.Length)
                {
                    textBox7.Text = Convert.ToString(result);
                    return;
                }
               
            }
        }
Posted
Updated 3-Dec-16 11:31am

1 solution

Well, you didn't show us your ConvertToHex method, so I'm assuming that's where you went wrong.

C#
public static byte[] StringToByteArray(string text) 
{
    return Enumerable.Range(0, text.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(text.Substring(x, 2), 16))
                     .ToArray();
}


Further, I would simply do this for the math part:

C#
string text = "2020202020442042497c31323334414243353637387c";
int total = this.StringToByteArray(text).Sum(x=>x);
string totalStr = string.Format("{0:x}", total);
 
Share this answer
 
Comments
CPallini 4-Dec-16 3:39am    
5.
#realJSOP 7-Dec-16 11:44am    
This has absolutely nothing to do with your original question. If my code answered your question, why did you unmark it as the answer?
EssenceGold 7-Dec-16 12:51pm    
Yeah you worked your code right. You are right. I had to ask this question in another topic.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900