Solved

C# Ascii to Hex Conversion

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

Technology Partners: 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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