Solved

C# Ascii to Hex Conversion

Posted on 2010-08-18
4
842 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using MS Code on my Mac 6 47
C# Update Status Bar.... 1 24
c# code 19 61
XML & .net 5 21
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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

24 Experts available now in Live!

Get 1:1 Help Now