In Focus

AuthorQuestion
Convert from hexadecimal to bytes
Posted By kyle on 25 Nov 2008
Hello Everyone,

I am reading in hexadecimal values into a C# program that are coming in the serial port buffer as ASCII text representation of hex.  I need to convert this down to the bit level so I can see certain 0's become 1's when specific switches are toggled.  How would I accomplish this?

Thank you in advance for your help.

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By Mamta M on 25 Nov 2008

The following link may help:

-Mamta

• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 26 Nov 2008
Thank you Mamta.  I will give that a try later today.

Thanks for your time and help.

Kyle

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 27 Nov 2008
I had some time to look over the code.  I have not tried to run it yet but I do have some questions.

Why is the length of the string being divided in half?  Since every hex value is 4 bits I thought there would be some sort of multiplier for the size of the array.

I need to view the byte as bits so FF  would appear as 11111111.

As you can see, I am very new to C# and object oriented programming.

Thank you in advance for your time and help.

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By Alan on 27 Nov 2008

Here's an approach you can use to convert a byte to its bit representation:

byte b = 0xff; // or whatever
string bits = String.Format("{0:D8}", int.Parse(Convert.ToString(b, 2)));
Console.WriteLine(bits); // 11111111

You can then access the individual bits using the string indexer, bits[i], where 0 <= i <= 7.

• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 30 Nov 2008
Hello Gentlemen,

I ran the code that was suggested by Mamta which was here pcheruku.wordpress.com/2008/10/21/sample-c-code-to-convert-hexadecimal-string-to-bytesThis only accepts 2 bytes and it converts the low byte only to decimal.

Let me explain better what I am trying to accomplish.  I will be reading in several strings which look similar to this

:S584N009E07460046000A;
:S584N100018010C00F807;
:S584N0032000000000000;
:S584N1000000000003200;
:S584N00EE021800F40114;
:S584N10052008BC090200;
:S584N0014000000A00F14;
:S584N100B3C03B00E5407;

The :S584N will have to be stripped off as well as the semi colon at the end and the rest of the string will always contain 8 bytes.  I need to convert the hex string to binary so for example the last string in the above list will display as:
1000000001011001111000000001110110000000011100101010000000111

I need to be able to watch these bits change state based on switch position and then change the color of a label based on the state of the bit.

I assumed the way to do it would be to create an array for all of the bits and then map a label to a bit position in the array.  I am new to C# and object oriented programming so if anyone has a better idea please say so.

In summary, first I need to convert the hex string to binary and then change the color of a label based on the state of a bit.

Thank you in advance for your time and help.

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By Alan on 30 Nov 2008

OK, try this code:

using System;

class Test
{
static void Main()
{
string s = ":S584N100B3C03B00E5407;";
s = s.Substring(6).TrimEnd(';');
Console.WriteLine(s);
string bin ="";
for(int i = 0; i < s.Length; i += 2)
{
string hex = s.Substring(i, 2);
byte b = Convert.ToByte(hex, 16);
string bits = String.Format("{0:D8}", int.Parse(Convert.ToString(b, 2)));
bin += bits;
}
Console.WriteLine(bin);
}
}

• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 30 Nov 2008
You are the man, Alan.

Do you agree with creating an array for the bits and pointing labels to the bit positions and have the labels change based on a 1 or a 0?

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By Alan on 30 Nov 2008

You don't really need to create an array, kyle, as a string can be thought of as a 'read only' array of characters.

So bin[0] will be the first and bin[63] the last character in the string - either '0' or'1'.

However, if you'd prefer an actual array, you can simply do:

char[] chars = bin.ToCharArray();

• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 30 Nov 2008
That makes perfect sense.  I like the bin[0] to bin[63] idea.  That should work perfectly.

I still lack some understanding how strings are extracted or dealt with while still in the buffer.  I am using a ReadExisting method to read the buffer on a timer.  Maybe that isn't the best way.  How would I assign bin[x] values when I could have as many as 15 8 byte strings in the buffer?  Or would the entire ReadExisting be considered 1 string?  If so I should parse the string by the semi-colon perhaps to break the string up into 8 byte chunks?

Thank you very much for your time and help Alan.

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By Alan on 01 Dec 2008

If the ReadExisting() method is working OK, I'd stick with that.

However, it does return a single string and so you will need to split it on the semi-colon as you suggested (using string.Split(';')). You can then apply the previous code to each string in the resulting array to obtain its bit representation

• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 01 Dec 2008
After I split up the string what method is used to identify or "name" each 8 byte string?

Thanks for your help Alan.  Every one of your posts have been a wealth of information for me.  Where can I learn more about manipulating strings?  The MSDN library is not user friendly to someone with very little OO programming like myself.

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By Alan on 02 Dec 2008

Once you've split up the string into 8 byte chunks, then the strings can simply be referenced using their index in the array. The first element in the array always  has an index of 0 and the last element has an index equal to the length of the array minus one.

If this is inconvenient then you can always assign the array members to named individual strings.

There are a lot of articles on the internet about manipulating .NET strings but here's one (taken from a book) which I particularly like:

http://en.csharp-online.net/Manipulating_Strings_in_CSharp

• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 02 Dec 2008
That looks to be a great website Alan.  Thanks again for the valuable and much appreciated information.  I will follow up with you regarding the results.

Thanks again.

Age and treachery will always overcome youth and skill.
• 0
• 0
Re: Convert from hexadecimal to bytes
Posted By kyle on 20 Dec 2008
Hi Alan.  I hope you are enjoying your weekend.

All the previous was done using HyperTerminal to send the information into my program.  I am now using  the device that I need to communicate with and am connected to it with my program however the information coming from the device comes in as garbage.  I connect the device to HyperTerminal and can read it just fine.  One of the things this device does is send GPS information every 10 seconds.  I can see it in HyperTerminal but not my program.  My program reads the data but it is garbage.  If I send test data to my program from HyperTerminal I can read it just fine.

Is it possible that I must convert the data leaving the device into another format?  I poked around looking at some ASCII convert information but was unsuccessful.

Thank you in advance for your time.

Age and treachery will always overcome youth and skill.