Solved

C# Ascii to Hex Conversion

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

823 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