<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/</id><updated>2023-09-19T13:43:57.494000Z</updated><subtitle>Recent posts to Hex string</subtitle><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#ec92/3e23" rel="alternate"/><published>2023-09-19T13:43:57.494000Z</published><updated>2023-09-19T13:43:57.494000Z</updated><author><name>alimans</name><uri>https://forge.codesys.com/u/alimans/</uri></author><id>https://forge.codesys.com5e60cf8c6c4fa9f2b1b72874a623e6fddf283f32</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi kdkwhite,&lt;br/&gt;
for Word you still can use suggested code by using a union structure and crack down your Word to two byte as bellow:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kr"&gt;TYPE&lt;/span&gt; &lt;span class="n"&gt;CrackWordToByte&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="n"&gt;UNION&lt;/span&gt;
    &lt;span class="n"&gt;InWord&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;WORD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;OutBytes&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;ARRAY&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="kr"&gt;OF&lt;/span&gt;  &lt;span class="kt"&gt;BYTE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_UNION&lt;/span&gt;
&lt;span class="n"&gt;END_TYPE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;then define your variable as this type:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;udInput&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CrackWordToByte&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;now assign your Word variable input to InWord and send OutBytes[x] to the mentioned method:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;udInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InWord&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WordVariableInput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;Input&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;udInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OutBytes&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;</summary></entry><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#ec92" rel="alternate"/><published>2023-05-12T08:35:20.539000Z</published><updated>2023-05-12T08:35:20.539000Z</updated><author><name>kdkwhite</name><uri>https://forge.codesys.com/u/marien/</uri></author><id>https://forge.codesys.com5868bbd06022e0f14b6931e1630d7a342cf0c76a</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello NBCC, I work on the same problem as you but I have to work with WORDs and not BYTEs, could you please explain me how did you managed to get it to work for longer variable types ?&lt;/p&gt;
&lt;p&gt;I know this is an old post but I still hope that someone will answer...&lt;/p&gt;
&lt;p&gt;in fact (this question is more for scott_cunningham) , I don't understand why and what is really done here : &lt;br/&gt;
"&lt;br/&gt;
Temp := SHR(Input, 4);&lt;br/&gt;
IF Temp &amp;gt; 9 THEN&lt;br/&gt;
Chr := Temp + 55;&lt;br/&gt;
ELSE&lt;br/&gt;
Chr := Temp + 48;&lt;br/&gt;
END_IF&lt;br/&gt;
"&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#0ef9" rel="alternate"/><published>2020-02-27T17:24:51.782000Z</published><updated>2020-02-27T17:24:51.782000Z</updated><author><name>NBCC</name><uri>https://forge.codesys.com/u/nbcc/</uri></author><id>https://forge.codesys.comf888d74cb3e3769f0f869f190f99e68761f82ae0</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;scott_cunningham hat geschrieben:&lt;/strong&gt;&lt;br/&gt;
58 = 0011 1010 in binary.  Hex uses four bits (nibble).  You have to look at the upper four bits (3), then the lower four bits (A).&lt;br/&gt;
A WORD uses two bytes (four nibbles) and can have a number from 0000 to FFFF hex.&lt;br/&gt;
A BYTE uses one byte (two nibbles) and can have a number from 00 to FF hex.&lt;br/&gt;
Here is a crude function to convert a BYTE to a two char string.  It is up to you to expand it for longer variable types...&lt;br/&gt;
FUNCTION BYTE_TO_HEX_STRING : STRING&lt;br/&gt;
VAR_INPUT&lt;br/&gt;
    Input : BYTE;&lt;br/&gt;
END_VAR&lt;br/&gt;
VAR&lt;br/&gt;
    Temp : BYTE;&lt;br/&gt;
    Chr  : BYTE;&lt;br/&gt;
    Ans  : STRING;&lt;br/&gt;
END_VAR&lt;br/&gt;
Temp := SHR(Input, 4);&lt;br/&gt;
IF Temp &amp;gt; 9 THEN&lt;br/&gt;
    Chr := Temp + 55;&lt;br/&gt;
ELSE&lt;br/&gt;
    Chr := Temp + 48;&lt;br/&gt;
END_IF&lt;br/&gt;
Ans&lt;span&gt;[0]&lt;/span&gt; := Chr;&lt;br/&gt;
Temp := SHR(SHL(Input,4),4); //wipes upper nibble (could also mask...)&lt;br/&gt;
IF Temp &amp;gt; 9 THEN&lt;br/&gt;
    Chr := Temp + 55;&lt;br/&gt;
ELSE&lt;br/&gt;
    Chr := Temp + 48;&lt;br/&gt;
END_IF&lt;br/&gt;
Ans&lt;span&gt;[1]&lt;/span&gt; := Chr;&lt;br/&gt;
Ans&lt;span&gt;[2]&lt;/span&gt; := 0; //null char&lt;br/&gt;
BYTE_TO_HEX_STRING := Ans;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thanks &lt;br/&gt;
I managed to get it to work for longer variable types. Worked like a charm &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#800c" rel="alternate"/><published>2020-02-27T17:24:48.987000Z</published><updated>2020-02-27T17:24:48.987000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com5c90e4cf453af67e7fc8e0cb29632f74f28c18cb</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;58 = 0011 1010 in binary.  Hex uses four bits (nibble).  You have to look at the upper four bits (3), then the lower four bits (A).&lt;/p&gt;
&lt;p&gt;A WORD uses two bytes (four nibbles) and can have a number from 0000 to FFFF hex.&lt;br/&gt;
A BYTE uses one byte (two nibbles) and can have a number from 00 to FF hex.&lt;/p&gt;
&lt;p&gt;Here is a crude function to convert a BYTE to a two char string.  It is up to you to expand it for longer variable types...&lt;/p&gt;
&lt;p&gt;FUNCTION BYTE_TO_HEX_STRING : STRING&lt;br/&gt;
VAR_INPUT&lt;br/&gt;
    Input : BYTE;&lt;br/&gt;
END_VAR&lt;br/&gt;
VAR&lt;br/&gt;
    Temp : BYTE;&lt;br/&gt;
    Chr  : BYTE;&lt;br/&gt;
    Ans  : STRING;&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;Temp := SHR(Input, 4);&lt;br/&gt;
IF Temp &amp;gt; 9 THEN&lt;br/&gt;
    Chr := Temp + 55;&lt;br/&gt;
ELSE&lt;br/&gt;
    Chr := Temp + 48;&lt;br/&gt;
END_IF&lt;br/&gt;
Ans&lt;span&gt;[0]&lt;/span&gt; := Chr;&lt;/p&gt;
&lt;p&gt;Temp := SHR(SHL(Input,4),4); //wipes upper nibble (could also mask...)&lt;br/&gt;
IF Temp &amp;gt; 9 THEN&lt;br/&gt;
    Chr := Temp + 55;&lt;br/&gt;
ELSE&lt;br/&gt;
    Chr := Temp + 48;&lt;br/&gt;
END_IF&lt;br/&gt;
Ans&lt;span&gt;[1]&lt;/span&gt; := Chr;&lt;br/&gt;
Ans&lt;span&gt;[2]&lt;/span&gt; := 0; //null char&lt;/p&gt;
&lt;p&gt;BYTE_TO_HEX_STRING := Ans;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#7923" rel="alternate"/><published>2020-02-27T17:24:47.992000Z</published><updated>2020-02-27T17:24:47.992000Z</updated><author><name>NBCC</name><uri>https://forge.codesys.com/u/nbcc/</uri></author><id>https://forge.codesys.comd6369df743e29dccddfc63f05562013ad6f0185a</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;scott_cunningham hat geschrieben:&lt;/strong&gt;&lt;br/&gt;
You can make your own solution by using two offsets.  Look at each byte. If it is &amp;gt;=10 then add 55 to it (A is ascii 65).  For smaller number, add ascii offset for 0 - sorry don't remember if it is 40 or 42).  Examine bytes from highest to lowest and con at your chars. &lt;br/&gt;
There is probably a prebuilt function somewhere, but it may take you longer searching for it than building it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;OK that's great but still facing a major problem for me. &lt;br/&gt;
Let's say i got value 58 in one byte. Then i want it displayed as 3A. (Hex is 4 bits). And i don't see any solution in it then&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#4df5" rel="alternate"/><published>2020-02-27T17:24:46.995000Z</published><updated>2020-02-27T17:24:46.995000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com3bf98ef3b1b5122b449f053793151ecabd2657f7</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;You can make your own solution by using two offsets.  Look at each byte. If it is &amp;gt;=10 then add 55 to it (A is ascii 65).  For smaller number, add ascii offset for 0 - sorry don't remember if it is 40 or 42).  Examine bytes from highest to lowest and con at your chars. &lt;/p&gt;
&lt;p&gt;There is probably a prebuilt function somewhere, but it may take you longer searching for it than building it.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Hex string</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/840d914da8/?limit=25#70e5" rel="alternate"/><published>2020-02-27T17:24:44.225000Z</published><updated>2020-02-27T17:24:44.225000Z</updated><author><name>NBCC</name><uri>https://forge.codesys.com/u/nbcc/</uri></author><id>https://forge.codesys.com79ce7e11f29b97795a4acca36544b66458c19777</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi all,&lt;/p&gt;
&lt;p&gt;I have converted an array of bytes into a string. Worked like a charm with the oscat function btw. &lt;br/&gt;
Now i have to put it into hex-values. Any way to create a string with hex values? &lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>