Sign in to follow this  
Followers 0
rhastalei

String to Hex

2 posts in this topic

i want to convert my ascii string to hex value. I used HEX function but result is not what i expected.

My string: 'BD1' > expected converted result is 0BD1H .. however it return me 01DBH ...

Value from Device Memory Batch Monitor:

D40: 4442   ('DB')

D41: 0031 ('1');

 

May i know how to convert string D40 to Hex? thank you for viewing.

 

Share this post


Link to post
Share on other sites

Good day rhastalei.

 

The problem is with the order you store the string characters and the order the HEX instruction reads them.

To replicate your issue I am using GXWorks2 to simulate a Q03UDE CPU  PLC (Debug Simulation).

Sorry I can't share screen captures from this PC, but I'll try my best:

  • -| SM400 |-----[$MOV "DB" D40]-   // Moves the string "DB" to D40, in your case you probably moved "BD", getting the opposite result
  • -| SM400 |-----[$MOV "10" D41]-   // Moves the string "10" to D41, in this case I was not sure if you used "10" or "01" but from your post I took "10"
  • -| SM400 |-----[HEX D40 D50 K3]-   // Converts 3 ASCII characters to HEX value and stores them in D50

From this operation, I get the following results:

  • D40 = 42 44HEX = B D ASCII
  • D41 = 30 31 HEX = 0 1 ASCII
  • D50 = 01BD HEX 

 

Now let's check how the HEX instruction reads ASCII values. GXWorks Help Menu has very useful images to explain this, but here is a quick summary:

  •  It starts by reading the lower 8 bits of D40 (in my case, 44 HEX = "D" ASCII)
  • This value is converted to 4-bit binary representation and stored in the 4 lower bits of D50, bits 0~3 (1101 BIN = D HEX)
  • The instruction reads the upper 8 bits of D0 (in my case, 42 HEX = "B" ASCII)
  • This value is converted to 4-bit binary representation and stored in the next 4 bits of D50, bits 4~7 (1011 BIN = B HEX)
  • Finally, the instruction reads the lower 8 bits of D41 (31 HEX = "1" ASCII)
  • This value is converted to 4-bit binary representation and stored in the next 4 bits of D50, bits 8~B (0001 BIN = 1 HEX)

As you can see, we have constructed the HEX value 1BD starting from the least significant digit up to the most significant digit.

In your case, you are storing the most significant digit first ("B") and storing the least significant digit last ("1").

Try storing the ASCII String "backwards", start by storing the least significant digit first ("1") and the most significant digit last ("B").

In my case I used this program to get 0BD1 HEX in D50.

  • -| SM400 |-----[$MOV "1D" D40]-  
  • -| SM400 |-----[$MOV "B0" D41]-  
  • -| SM400 |-----[HEX D40 D50 K3]-  

 Depending on how you are receiving your data, you could make some arrangements using $MOV and SWAP instructions to process your ASCII values before reading them with the HEX instruction.

I hope this is helpful, best regards!

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0