Skip to content

RSLogix 5000 Hex String To Decimal and Back

November 18, 2015
//HEX STRING TO DECIMAL STRUCTURED TEXT ROUTINE
 sbr(_HSTOD_Hex);

size(_HSTOD_Convert,0,_HSTOD_Length);
if _HSTOD_Hex.LEN <_HSTOD_Length then
 _HSTOD_Length:=_HSTOD_Hex.LEN;
end_if;
_HSTOD_Length:=_HSTOD_Length-1;

for _HSTOD_Index:=0 to _HSTOD_Length do
 case _HSTOD_Hex.Data[_HSTOD_Index] of
 48: _HSTOD_Convert[_HSTOD_Index]:=0;
 49: _HSTOD_Convert[_HSTOD_Index]:=1;
 50: _HSTOD_Convert[_HSTOD_Index]:=2;
 51: _HSTOD_Convert[_HSTOD_Index]:=3;
 52: _HSTOD_Convert[_HSTOD_Index]:=4;
 53: _HSTOD_Convert[_HSTOD_Index]:=5;
 54: _HSTOD_Convert[_HSTOD_Index]:=6;
 55: _HSTOD_Convert[_HSTOD_Index]:=7;
 56: _HSTOD_Convert[_HSTOD_Index]:=8;
 57: _HSTOD_Convert[_HSTOD_Index]:=9;
 65: _HSTOD_Convert[_HSTOD_Index]:=10;
 66: _HSTOD_Convert[_HSTOD_Index]:=11;
 67: _HSTOD_Convert[_HSTOD_Index]:=12;
 68: _HSTOD_Convert[_HSTOD_Index]:=13;
 69: _HSTOD_Convert[_HSTOD_Index]:=14;
 70: _HSTOD_Convert[_HSTOD_Index]:=15;
 end_case;
end_for;

_HSTOD_Temp:=0;
for _HSTOD_Index:=0 to _HSTOD_Length do
 _HSTOD_RevIndex:=_HSTOD_Length - _HSTOD_Index;
 _HSTOD_Temp:=_HSTOD_Temp + _HSTOD_Convert[_HSTOD_Index]*16**_HSTOD_RevIndex;
end_for;

_HSTOD_Dec:=_HSTOD_Temp;
ret(_HSTOD_Dec);




//DECIMAL TO HEX STRING STRUCTURED TEXT ROUTINE
 sbr(_DTOHS_Dec);

dtos(_DTOHS_Dec,Temp_String);
_DTOHS_Length:=Temp_String.len - 1;

for _DTOHS_Index:=0 to _DTOHS_Length do
 if _DTOHS_Index=0 then
 _DTOHS_Remainder := _DTOHS_Dec mod 16;
 _DTOHS_Quotient := _DTOHS_Dec / 16;
 else
 _DTOHS_Remainder := _DTOHS_Quotient mod 16;
 _DTOHS_Quotient := _DTOHS_Quotient / 16;
 end_if;

 case trunc(_DTOHS_Remainder) of
 0:_DTOHS_Convert[_DTOHS_Index]:=48;
 1:_DTOHS_Convert[_DTOHS_Index]:=49;
 2:_DTOHS_Convert[_DTOHS_Index]:=50;
 3:_DTOHS_Convert[_DTOHS_Index]:=51;
 4:_DTOHS_Convert[_DTOHS_Index]:=52;
 5:_DTOHS_Convert[_DTOHS_Index]:=53;
 6:_DTOHS_Convert[_DTOHS_Index]:=54;
 7:_DTOHS_Convert[_DTOHS_Index]:=55;
 8:_DTOHS_Convert[_DTOHS_Index]:=56;
 9:_DTOHS_Convert[_DTOHS_Index]:=57;
 10:_DTOHS_Convert[_DTOHS_Index]:=65;
 11:_DTOHS_Convert[_DTOHS_Index]:=66;
 12:_DTOHS_Convert[_DTOHS_Index]:=67;
 13:_DTOHS_Convert[_DTOHS_Index]:=68;
 14:_DTOHS_Convert[_DTOHS_Index]:=69;
 15:_DTOHS_Convert[_DTOHS_Index]:=70;
 end_case;
end_for;

for _DTOHS_Index:=0 to _DTOHS_Length do
 _DTOHS_RevIndex:=_DTOHS_Length - _DTOHS_Index;
 _DTOHS_Hex.Data[_DTOHS_Index]:=_DTOHS_Convert[_DTOHS_RevIndex];
end_for;
_DTOHS_Hex.LEN:=_DTOHS_Length + 1;

ret(_DTOHS_Hex);
Advertisements

From → Rockwell

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: