FP_BIN_TO_ASCII

Binary -> ASCII conversion

This FP instruction converts 16-bit/32-bit binary data stored in the area specified by s2_BinaryData to ASCII code. The conversion method is specified by the control string of s1_Format. The converted result is stored in the area specified by d_AsciiData.

Parameters

Input

s1_Format (STRING)

Control and format string set in inverted commas

s2_BinaryData (BOOL, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, DATE, TOD, DT, STRING)

Starting address for storing binary data

n_Conversions (WORD, INT, UINT)

Quantity of numbers to be converted: 0–65535

n_AsciiDataStartPosition (INT)

Starting position in ASCII data: 0–255

Output

d_AsciiData (BOOL, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, DATE, TOD, DT, STRING)

Starting address for storing ASCII data

Explanation and examples of control string s1_Format for 16-bit PLCs

s1_Format: e.g. +%08lX

Position in the example

Description

s1_Format

Binary data

Conversion result in ASCII data

Comment

+

forward (only for hexadecimal numbers)

'+%4X'

16#B2A

'2AB'

forward direction (only available with specifier x, X)

if plus sign is omitted: reverse (default setting)

'%4X'

16#B2A

'B2A'

reverse direction

%

format string specifier (mandatory)

padding format

0

fill with zeros

'%06x'

16#100

'000100'

width six characters, decimal value with three leading zeros (not allowed for specifier i, d, mandatory for specifier x, X)

8

width of the ASCII data element

'%06d'

100

'100'

width six characters with three leading spaces

Permissible range for the value before the specifier:

  • 1–15 before specifiers d, ld, i, li

  • 1–4 before specifier X

  • 1–8 before specifier lX

I

double length specifier

e.g. specifier i with l = Ii -> DINT

'+%4Id'

100

'100'

width four characters, requires DINT or DWORD for the converted result

format specifier

i

INT

'%6i'

-100

'-100'

width six characters, three leading spaces, signed integer

d

INT

'%6d'

100

'000100'

width six characters, decimal value with three leading zeros

X

hexadecimal upper case

'%4X'

16#12A

'12A'

width four characters, hexadecimal number in upper case

Explanation and examples of control string s1_Format for 32-bit PLCs

s1_Format: e.g. +%#12.5lxBCD

Position in the example

Description

s1_Format

Binary data

Conversion result in ASCII data

Comment

+

forward (only for hexadecimal numbers)

'+%4X'

16#B2A

'2AB'

forward direction (only available with specifier x, X or b)

if plus sign is omitted: reverse (default setting)

'%4X'

16#B2A

'B2A'

reverse direction

%

format string specifier (mandatory)

padding format

0

fill with zeros

'%06d'

100

'000100'

width six characters, decimal value with three leading zeros

+

add a plus sign

'%+4d'

100

'+100'

width four characters, decimal value, + sign added

-

left alignment

'%-6d'

100

'100'

width six characters, decimal value, left aligned

(space) add a space instead of plus sign

'%4d'

100

'100'

width four characters, decimal value filled with spaces

#

insert 0x for hexadecimal numbers

'%#4X'

100

'0X64'

width four characters, numerical value in hexadecimal format starting with 0X

append always a decimal point for real number

'%#8.0f'

123.45678

'123.'

width eight characters, floating-point number, four leading spaces

12

width of the ASCII data element (with or without comma)

'%012d'

100

'000000000100'

width 12 characters with nine leading zeros

no width:

  • FP_BIN_TO_ASCII: required minimum width is assumed

  • FP_ASCII_TO_BIN and FP_ASCII_CHECK: comma separator is required

'%d,'

100

100,

no width is specified, comma is appended

precision after decimal point

.5

any digit after decimal point

'%8.3f'

123.45599

'123.456'

width eight characters, three characters precision after decimal point

double length specifier

I

double the length of the specifier

e.g. specifier i with l = Ii -> DINT

'+%4Iu'

100

'100'

width four characters, requires DINT or DWORD for the converted result

format specifier

i

INT

'%10i'

-100

'-100'

width 10 characters, six leading spaces, signed integer

u

UINT1)

'%10u'

-100

'100'

width 10 characters, seven leading spaces, unsigned integer

d

INT

'%6d'

100

'000100'

width six characters, decimal value with three leading zeros

x

hexadecimal lower case

'%4x'

16#12A

'12a'

width four characters, hexadecimal number in lower case

X

hexadecimal upper case 1)

'%4X'

16#12A

'12A'

width four characters, hexadecimal number in upper case

b

BCD

'%5b'

16#123

'123'

width five characters, BCD data

f

floating

'%-6.2f'

1.2345

'1.23'

width six characters, left aligned, precision two digits after decimal point

e

exponential 1.23e10

'%9.3e'

1234.5678

'1.235e+03'

width 9 characters, precision 3 digits after decimal point, exponential lower case

E

exponential upper case 1.23E10 1)

'%9.3E'

1234.5678

'1.235E+03'

width 9 characters, precision 3 digits after decimal point, exponential upper case

g

floating or exponential 1)

'%12g'

1234.5678

'1234.57'

width 12 characters, floating-point number

G

floating or exponential upper case 1)

'%#9.3G'

1234

'1.E+03'

width nine characters, precision three characters after decimal point

  • For FP_BIN_TO_ASCII: any string can be appended to the conversion

  • For FP_ASCII_TO_BIN and FP_ASCII_CHECK: only comma is allowed

'%8dPANA'

100

'100PANA'

width eight characters, decimal number, 'PANA' is appended

1) The FP instructions FP_ASCII_TO_BIN and FP_ASCII_CHECK do not differentiate between upper and lower case

Error flags

sys_bIsOperationErrorHold (turns to TRUE and remains TRUE)
  • if there is an error in the control string specified by sFormat.

  • if forward direction (+) is specified in sFormat when the format is decimal.

  • if the number of ASCII characters per converted unit specified by n_Conversions exceeds 4 for 16-bit data or 8 for 32-bit data when hexadecimal format is specified by s1_Format.

  • if 0 is specified for the no. of 16- or 32-bit (1- or 2-word) units to be converted in n_Conversions.

  • if the number of 16- or 32-bit decimal numbers to be converted specified by n_Conversions exceeds the area for storing ASCII data.

  • if the converted result exceeds the area.

sys_bIsOperationErrorNonHold (turns to TRUE for one scan)
  • if there is an error in the control string specified by sFormat.

  • if forward direction (+) is specified in sFormat when the format is decimal.

  • if the number of ASCII characters per converted unit specified by n_Conversions exceeds 4 for 16-bit data or 8 for 32-bit data when hexadecimal format is specified by s1_Format.

  • if 0 is specified for the no. of 16- or 32-bit (1- or 2-word) units to be converted in n_Conversions.

  • if the number of 16- or 32-bit decimal numbers to be converted specified by n_Conversions exceeds the area for storing ASCII data.

  • if the converted result exceeds the area.

Example

POU header

All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.

	VAR
		bStart: BOOL:=FALSE;
		iArray1: ARRAY [0..1] OF INT:=[1234,-56];
		iAscii1: ARRAY [0..4] OF WORD:=[5(16#FFFF)];
	END_VAR

POU body

When the variable bStart changes from FALSE to TRUE, the function is carried out. It converts two 16-bit units to 2 x 4 decimal ASCII data. Offset = 1 ASCII character (8-bit).

LD body

BODY
    WORKSPACE
        NETWORK_LIST_TYPE := NWTYPELD ;
        ACTIVE_NETWORK := 0 ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 10 ;
        NETWORK_BODY
B(B_VARIN,,'%4d',10,5,12,7,);
B(B_VARIN,,iArray1,10,6,12,8,);
B(B_VARIN,,2,10,7,12,9,);
B(B_VAROUT,,iAscii1,25,5,27,7,);
B(B_CONTACT,,bStart,7,4,9,6,R);
B(B_COMMENT,,Converts two 16-bit units to 2x4 decimal ASCII data. Offset = 1 ASCII character (8-bit).,5,1,38,3,);
B(B_F,FP_BIN_TO_ASCII!,,12,3,25,10,,?DEN?hs1_Format?ds2_BinaryData?dn_Conversions?dn_AsciiDataStartPosition?AENO?cd_AsciiData);
B(B_VARIN,,1,10,8,12,10,);
B(B_COMMENT,,Result iAscii1: 'x1234 -56x',28,8,41,9,);
L(9,5,12,5);
L(1,5,7,5);
L(1,0,1,10);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

ST body

IF DF(bStart) then
    FP_BIN_TO_ASCII(s1_Format := '%4d', 
        s2_BinaryData := iArray1, 
        n_Conversions := 2, 
        d_AsciiData => iAscii1);
END_IF;

Modified on: 2021-08-13Feedback on this pagePanasonic hotline