Solved

C# Ascii to Hex Conversion

Posted on 2010-08-18
4
872 Views
Last Modified: 2012-05-10
I have a serial message that I want to convert and re-transmit.

Input is 10 data bytes say..

byte[] bt_message = 0x38,0x38,0x30,0x31,0x30,0x30,0x30,0x31,0x46,0x46

This is equivalent to ASCI '8','8','0','1','0','0','0','1','F','F'


I want to combine these bytes in pairs and resend so I would have output string as 5 bytes...


0x88,0x01,0x00,0x01,0xFF


ie each output byte combined from two input hex bytes.

Any help apreciated.

Thanks
0
Comment
Question by:oddszone
4 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33465748
0
 

Author Comment

by:oddszone
ID: 33466069
Done it with following...Seems very convoluted but works !




string StrValue = "";
                                byte[] tx_data = new byte[data.Length / 2];

                                for (int nIdx = 0; nIdx < data.Length; nIdx += 2)
                                {
                                    StrValue  = System.Convert.ToChar(System.Convert.ToUInt32(String.Format("{0:x2}", data[nIdx]), 16)).ToString();
                                    StrValue += System.Convert.ToChar(System.Convert.ToUInt32(String.Format("{0:x2}", data[nIdx+1]), 16)).ToString();
                                    tx_data[nIdx / 2] = (byte)(Int32.Parse(StrValue, System.Globalization.NumberStyles.AllowHexSpecifier)); ;

                                }

Open in new window

0
 
LVL 7

Expert Comment

by:jdavistx
ID: 33466088
This seems pretty far from a good solution, but maybe it works?
byte[] input = {0x38,0x38,0x30,0x31,0x30,0x30,0x30,0x31,0x46,0x46};
byte[] output = new byte[5];

for(int i = 0; i < output.Length-1; i++)
	output[i] = Convert.ToByte(((char)input[i]).ToString() + ((char)input[i+1]).ToString());

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33466175
byte[] bt_message = new byte[] { 0x38, 0x38, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x46, 0x46 };            string bytes = Encoding.ASCII.GetString(bt_message);            List newBytes = new List(bytes.Length/2);            for (int i = 0; i < bytes.Length; i+=2)            {                newBytes.Add((byte)Convert.ToInt32(string.Format("{0}{1}", bytes[i], bytes[i + 1]), 16));            }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 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