LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

hex to binary and extract sequence of bits

how can I convert a string representing hex data into a binary sequence from which to extract a sequence of bits at a defined place and display that as a dezimal number
0 Kudos
Message 1 of 10
(5,296 Views)
Hi Rich,
      When you say "a string representing Hex data", do you mean each byte represents a hex digit (ASCII), or each byte holds some binary data (0-255)?
If it's a binary string to start with, here's one of a million ways to pluck some bits...
Cheers
When they give imbeciles handicap-parking, I won't have so far to walk!
0 Kudos
Message 2 of 10
(5,289 Views)

Hi RichL,

Sorry to start with a series of questions, but in order to provide an accurate solution I will need to know the following:

1.  How do you obtain or create the "string representing hex data"?

2.  Is the string an actual ASCII-text string? or are they HEX data obtained through communication, such as serial, etc?

3.  If you answer yes to the second part of question 2, then it is most likely that the HEX representation is actually the Hex value.  In other words, does the string contain values such as 0xFF is the same as 255 decimal and the same as 11111111 binary?  or is it a string which would comprise of two letter "F" for this example?

The reason for the questions is that a value (number) is the same and can be displayed as decimal, Hex or binary, but is still the same number.  However, it appears that you would like to place the binary equivalent in a string or array in order to extract a defined bit (probably as an index for a Case Statement, etc..).  If this is the case, then you can "see" the value (number) using a display, right click and choose the type of display (decimal, hex, binary).   If you want to use a string or create an array, you can typecast the value to a string or use the "HEX to String.vi".  You can then pick the desired by and compare it.  However, at this time, it is no longer a number but ASCII-text. 🙂

As for the display, you can do what I mentionned earlier to display the numeric value, or you can display it as text after the conversion to text (string).  The only danger of displaying the ASCII version is that the display is not the value and cannot be manipulated later.

Hope this helps,

JLV

Message 3 of 10
(5,285 Views)
Hello

thanks for the answers. I think I have to explain myself a bit more clearly.
I am tapping the read display input of the CAN-SendandReceive.vi which shows for example the following string (ASCII I assume) :
3C0118D0  8  00 00 12 12
where the first eight characters are the hex display of a message ID consisting of 32 bits. My problem is that I have to extract out of these characters a sequence of bits which are not aligned to the hex boundaries, i.e. bit 3 to 13.  So what has to happen is that each of the eight ASCII characters, representing a hex value, has to be converted into binary value, out of which I can extract the bit I need. However, the binary representation has to be that of the hex value, not the ASCII code, meaning for example that the "C" in the string has to be converted into "1100" (hex to binary) and not "0100 0001" (ASCII code binary) .

Hope that helps to get a solution.
0 Kudos
Message 4 of 10
(5,266 Views)

Hi RichL,

It does look like an ASCII string: 3C0118D0  8  00 00 12 12

 I am tapping the read display input of the CAN-SendandReceive.vi which shows for example the following string (ASCII I assume) :

where the first eight characters are the hex display of a message ID consisting of 32 bits. My problem is that I have to extract out of these characters a sequence of bits which are not aligned to the hex boundaries, i.e. bit 3 to 13.  So what has to happen is that each of the eight ASCII characters, representing a hex value, has to be converted into binary value, out of which I can extract the bit I need. However, the binary representation has to be that of the hex value, not the ASCII code, meaning for example that the "C" in the string has to be converted into "1100" (hex to binary) and not "0100 0001" (ASCII code binary) .

Message 5 of 10
(5,257 Views)

Hi RichL,

It does look like an ASCII string:  3C0118D0  8  00 00 12 12

 I am tapping the read display input of the CAN-SendandReceive.vi which shows for example the following string (ASCII I assume) :

where the first eight characters are the hex display of a message ID consisting of 32 bits. My problem is that I have to extract out of these characters a sequence of bits which are not aligned to the hex boundaries, i.e. bit 3 to 13.  So what has to happen is that each of the eight ASCII characters, representing a hex value, has to be converted into binary value, out of which I can extract the bit I need. However, the binary representation has to be that of the hex value, not the ASCII code, meaning for example that the "C" in the string has to be converted into "1100" (hex to binary) and not "0100 0001" (ASCII code binary) .

Message 6 of 10
(5,257 Views)

Hi RichL,

It does look like an ASCII string:   3C0118D0  8  00 00 12 12

The number 8 would be 08 if it was HEX, and the first set of numbers would be in pairs 3C 01 18 D0.

So you first need to extract the message ID:  3C0118D0, right?

The first thing is how do you find this message?  Is it embedded as part of other messages?  Is it indexed?

Let's say you are able to extract the message ID such as it appears in the message: 3C0118D0  8  00 00 12 12

You could first get a string subset by length, to extract the message ID, only: 3C0118D0

 

Message Edited by JoeLabView on 09-29-2005 08:13 AM

Message 7 of 10
(5,259 Views)

Sorry about the posts below... The TAB key sent the message..

The edit function didn't fix the original post..  Yikes...  I probably need a coffee...  😉

Now where was I???

Let's start again...

================================================================

The message { 3C0118D0  8  00 00 12 12  }  looks like an ASCII string because the number 8 would be 08 if it was HEX, and the first set of numbers would be in pairs 3C 01 18 D0. 

So you first need to extract the message ID:  3C0118D0, right?

The first thing is how do you find this message?  Is it embedded as part of other messages?  Is it indexed?

Let's say you are able to extract the message ID such as it appears in the message: 3C0118D0  8  00 00 12 12

You could first get a string subset by length, to extract the message ID, only: 3C0118D0

You can create a loop to process the string and convert it to hex (sting to Hex.vi).  In the loop, indexed by "string length / 2" (=4), you extract each pair of characters and convert it to Hex.  In the case that the string is an odd number, you must first pad it with a leading zero.

Unfortunately, I do not have LV on this machine, so I may not provide exact details, but you'll get the picture:

Convert the desired Hex into binary.  You may have to convert to string first, then "String to Binary.vi"  I cannot recall if you can convert hex to binary.  They are both the same value, and selecting the type of display will show hex - binary - decimal.  From what I understand, you want to be able to extract bytes 3 to 13.  So.. you could once again convert the binary to string which will give you characters 1100111010110101.  From this string, you can extract (substring) index starting at 3 (or is it 2 😉  I can't remember... look at context help to see if the index starts at 0 or 1) and length 11 to include byte # 3.  This should give you in this example: 00111010110

You can then use the string 00111010110 as desired..  remember.. from this example, it is now a character string.

If you still need help, I may be able to write a quick vi tonight.

Regards,

JLV

============== EDIT ==========

Wow... I'm having difficulty posting this.. It converted my right bracket to smilies..

sorry...  I hope it looks ok now..  (and I am getting some coffee)

Message Edited by JoeLabView on 09-29-2005 08:37 AM

Message 8 of 10
(5,247 Views)
Hi Rich
 
  I've done exactly what I believe you're tring to do with other register based communication protocols.  Try the attached VI that will allow you to enter your Hex String, up to 8 characters, (32bits), then you can specify the starting bit location and the number of bits to include. The VI will spit out the result.  Once you get the numeric output, you can then format it into hex or binary as you desire.
 
 Hope this is what you're trying to accomplish.
 
Todd K.
Message 9 of 10
(5,231 Views)
Hi Joe, hi Todd,

both answers got me to where I wanted to go.Thanks!

RichL
0 Kudos
Message 10 of 10
(5,209 Views)