Step 7 Umwandlung Hex in ASCII bzw. String

pat125

Level-1
Beiträge
57
Reaktionspunkte
1
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo Zusammen,

habe eine Frage bzw. ein Problem und würde mich über Hilfe von euch sehr freuen.

Ich habe folgendes Problem: Aus einem Hex Wert möchte ich ein String machen.
Soll folgendes heissen, habe ein Integerwert von 2000, der als Hex-Wert 16#07D0 ist. Nun möchte ich ‚07D0‘ als String haben. Habe schon diverse Umwandelungen versucht, aber nie zu einem Ergebnis gekommen. Dieser Funktionsbaustein soll in SCL geschrieben werden (PCS7 V8.2)

Kann mir jemand hierfür einen Tipp geben oder eventuell einen Lösungsansatz?

Vielen Dank im Voraus!

Gruß Pat
 
Mit PCS7 habe ich keine Erfahrung. Gibt es da etwas fertiges wie die Function HTA?
Man könnte das auch etwa so machen:
Code:
hexChars : String := '01234567890ABCDEF';

hexString := '0000';
hexString[1] := hexChars[iValue / 4096 + 1];
hexString[2] := hexChars[(iValue / 256) MOD 16 + 1];
hexString[3] := hexChars[(iValue / 16) MOD 16 + 1];
hexString[4] := hexChars[iValue MOD 16 + 1];

Harald
 
Zuviel Werbung?
-> Hier kostenlos registrieren
ich würde habe so in der Art gesehen und übernommen:

Code:
FUNCTION "WordToAsciiString" : Void
{ S7_Optimized_Access := 'TRUE' }
//Convert a WORD (16 Bit / 2 Byte / 4 nibble) to STRING of max 4 characters
//In := WORD to convert, starting by LSB of WORD
   VAR_INPUT 
      IN_Word : Word;   // value as WORD to convert into ASCII-String
   END_VAR


   VAR_IN_OUT 
      ASCII_String : String;   // ASCII String to append converted Value
   END_VAR


   VAR_TEMP 
      t_Index : SInt;   // how many nibbles to convert
      t_USInt : USInt;   // temp nibble storage to convert in to ASCII Char
      t_String : String[4];   // temp String to build ASCII String from variable
      t_byte : Array[1..4] of Byte;
   END_VAR


BEGIN
	// Loop trough input variable and calculate characters 
	#t_Index := 4;
	WHILE #t_Index > 0 DO
	  #t_USInt := WORD_TO_USINT(#IN_Word AND 16#0F); // get last nibble from WORD to convert into char
	  
	  IF #t_USInt < 10 THEN    // if smaller add 
	    #t_USInt := #t_USInt + 48;
	  ELSE                    // if greater add 
	    #t_USInt := #t_USInt + 55;
	  END_IF;
	  
	  #t_byte[#t_Index] := USINT_TO_BYTE(#t_USInt); // add char to byte array
	  #IN_Word := SHR(IN := #IN_Word, N := 4); // shift to next nibble
	  #t_Index := #t_Index - 1; // decrement index
	END_WHILE;
	// copy byte array to IN/OUT String
	Chars_TO_Strg(Chars := #t_byte,
	              pChars := 0,
	              Cnt := 2,
	              Strg => #t_String);
	#ASCII_String := CONCAT(IN1 := #ASCII_String, IN2 := #t_String);
END_FUNCTION
 
Zurück
Oben