Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Ascii to Hex Conversion

Posted on 2010-08-18
4
Medium Priority
?
955 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
[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
4 Comments
 

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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