Convert Hex to Binary

Kev77

Member
Join Date
Mar 2008
Location
Beaverton, Or
Posts
410
Good morning,

I am running a controllogix and I am connected to a hydrocarbon gas alalyzer over an EWEB module. I am picking up a Hex number that when broke down, will give me status flags. I am trying to find a way to break this number down;

0052000

What I am looking for is something like this;

0000 0000 0101 0010 0000 0000 0000 0000

Is there a instruction in control logix that will do this for me?

šŸ™ƒ
 
It sounds like the output that you are looking for is in Binary Coded Decimal (BCD). I can't look up the instruction right now, but I would bet that there is a DINT to BCD instruction in there somewhere.
 
Go into the tag database, find the tag.

Then select "Edit Tags" tab at the bottom, and then change the "Style" of the tag (use the dropdown), to Binary.

You will then see your tag in Binary format on the Monitor Tags tab.

You could just as easily have coded your bit instuctions to look for the binary bits anyway, regardless of the tag's style

e.g. MyTag.13 is binary bit 13
 
Last edited:
What type of variable is the 0052000 stored?


Are you sure you have the correct number of digits?
The above has 7 this has 8

0000 0000 0101 0010 0000 0000 0000 0000
 
Just to put it into perspective for you, no "conversion" is actually needed.

Hex is just another way to express a large binary number, where each group of 4 bits is assigned a symbol as follows

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111

So 16#EF3A5B0C becomes 2#1110_1111_0011_1010_0101_1011_0000_1100

hope that helps
 
Thanks to all,

I found that I had the correct instruction but the wrong tag style. As for as the correct number of digits, I was missing a "0". It should have been "00520000".

I now show 2#0000 0000 0101 0010 0000 0000 0000 0000

What does the 2# stand for? (Sorry, I found out that it is the prefix, 2# means binary)

I am using a "MID" instruction to extract the flags from a string. Then I am using a "STOD (string to Decimal)" instruction to convert to Hex. Next I am using a "TOD (To decimal)" instruction to convert to Binary. Is this the correct way to do this?

If I want to use the highlighted numbers, what is the best way to extract it?

0000 0000 0101 0010 0000 0000 0000 0000
 
Last edited:
Kev77 said:
I am using a "MID" instruction to extract the flags from a string. Then I am using a "STOD (string to Decimal)" instruction to convert to Hex. Next I am using a "TOD (To decimal)" instruction to convert to Binary. Is this the correct way to do this?

If it works for you - yes.

Kev77 said:
If I want to use the highlighted numbers, what is the best way to extract it?

0000 0000 0101 0010 0000 0000 0000 0000

If you mean you want to test for those bits being ON, then simply address the bits in question.


0000 0000 0101 0010 0000 0000 0000 0000
| bit 19
| bit 20



eg. MtTag.19 or MyTag.20
 
Of course that should have been bits 20 and 21 - not 19 and 20 - šŸ™ƒ
 
The reason I ask about other ways is there are different combinations of numbers using 2 to 3 bits.

Example:

001 = Flame lit
010 = Flame lighting
101 = Flame out
and so on...

The way I have it now is

---|\|---|\|---| |-------------------(Flame Lit)

---|\|---| |---|\|-------------------(Flame Lighting)

---| |---|\|---| |-------------------(Flame out)

and so on.

Is this the best way to decode this?

Thanks,

Kevin
 
001 = Flame lit
010 = Flame lighting
101 = Flame out
and so on...



if MyTag = 1------------------(Flame Lit)

if MyTag = 2-------------------(Flame Lighting)

if MyTag = 5-------------------(Flame out)

useing a compare function to look at the specific integer.
 
It all depends on how the status encoding is described in the analyser's documentation. You'd be best to "match" your decoding to the way it is encoded for transparency.

I would think the way you have it coded is adequate if the bits have specific individual meaning.

But if it is documented as numeric values in specific positions in the status word, then you could use the BTD instruction to move the group of bits down to bit 0 in another tag, and then equate to values as has been suggested by dahnuguy.

eg. if bits 20, 21, & 22 are a "Flame Status Code" then you could extract the numeric values with :-

BTD
Source : Analyser_Status
Source Bit : 20
Destination : Flame_Status
Dest. Bit : 0
Length : 3

After the BTD, your Flame_Status would take values of 0 to 7.
 
why not just take the byte or word and AND it with 1,2,4,8,16 etc.
this gives your flags.
i have a bcd to bin converter as a standard.
 
???

I am still having some issues with converting to binary. Can anyone tell me why if I have a SINT of "5" gives me 2#0011_0101 and not 2#0000_0101? I have attached a screen shot of my program. As I said before I have a number that is broadcasted which comes across as (example) 00520000. I moved values from the data portion of the string to SINT tags set to binary. When I have an "A" (example 005A0000) and move the 4 position (A) I get 2#0100_0001. I have tried everything I can think of and still get the same result. Help!

Thanks,

Kevin
 
Your MOV command moved not the NUMBER 5 but the ASCII CHARACTER '5' which has the encoding 35 in hex. Get an ASCII table and start comparing. I don't believe that there is aa single command to interpret a string which represents a hex value and store the equivalent as a number.

You will have to write a routine that does the following: (I'm sorry that I don't have RSLogix5000 at home so that I could actually write the code)

Declare three variables, Final_Number, Temp_Hex and Character_Pos.

CLR Final_Number and Character_Pos

Do this while Character_Pos < String_Length
MOV String_Data[Character_Pos] to Temp_Hex (*)
Subtract 48 from Temp_Hex (*)
If Temp_Hex > 15 then subtract 6 from Temp_Hex (*)
Multiply Final_Number by 16
Add Temp_Hex to Final_Number
Add 1 to Character_Pos
You should end up with Final_Number being equal to the string representation.



We are starting from the leftmost character of the string, getting its ASCII value, converting that to 0-15, left shifting Final_Number by 4 bits and adding in the converted character. We continue until we have processed the last character.

By the way - if you only want the value from one position then the starred (*) instructions are all you need.
 
Last edited:

Similar Topics

Hi I need to convert a DINT with HEX value e.g A0F15663 to a string with the same value 'A0F15663'. Any tips of good instructions to use ...
Replies
11
Views
3,381
I actually solved this, but I thought this info may help others. Condition which prompted this need: I was using a function provided to me by...
Replies
13
Views
3,746
Hey All, i have been fortunate enough to play around with an Applied Motion Products Servo Drive SV200 with 100 watt/ 24vdc congif. I managed...
Replies
2
Views
1,875
I'm trying to display a Allen Bradley Controller Serial Number that's in HEX to String for my SCADA to display. Any ideas?
Replies
3
Views
2,840
Hi All. I have a PLC connected to citect by modbus.I need to transfer a value in HEX from the PLC to the Citect, and to convert it to Time value...
Replies
12
Views
4,300
Back
Top Bottom