Using AsHexString

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
User avatar
MARCOREMMEDATA
Posts: 14
Joined: Mon Apr 11, 2022 4:48 pm
Location: ITALIA
Contact:

Using AsHexString

Post by MARCOREMMEDATA »

Hi

I've to convert a integer value in Hexadecimal. But intrincic function AsHexString give back a wrong value.
can you help me understand if I am using the function incorrectly?

The value is 49202, Window Calc for HEX is C032 for lansa il 32C00000

Image

https://docs.lansa.com/14/en/lansa016/P ... STRING.htm
Attachments
2024-12-03 11_50_24-MR000030 - DEBUG -   Build Status_ Compiled - LANSA Editor [run].png
2024-12-03 11_50_24-MR000030 - DEBUG - Build Status_ Compiled - LANSA Editor [run].png (68.89 KiB) Viewed 65896 times
MARCO ROSSI | Software Developer Sr. - Software Production
chriskarge
Posts: 5
Joined: Wed May 25, 2016 10:28 pm

Re: Using AsHexString

Post by chriskarge »

Howdy! Agreed, it looks like the intrinsic method doesn't work. I created a quick form / routine to do the conversion; it isn't very elegant, but the uDec2Hex routine seems to return the correct value, so maybe you can use it as a starting point?

Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientwidth(484) Clientheight(301) Componentversion(2)
Define_Com Class(#STD_NUM.Visual) Name(#STD_NUM) Componentversion(1) Displayposition(1) Height(21) Left(17) Parent(#COM_OWNER) Tabposition(1) Top(23) Usepicklist(False) Width(352)
Define_Com Class(#STD_TEXT.Visual) Name(#STD_TEXT) Componentversion(1) Displayposition(2) Height(21) Left(17) Parent(#COM_OWNER) Tabposition(2) Top(55) Usepicklist(False) Width(895)
Define_Com Class(#PRIM_PHBN) Name(#Button1) Caption('Button1') Displayposition(3) Left(360) Parent(#COM_OWNER) Tabposition(3) Top(122) Buttondefault(True)

Evtroutine Handling(#com_owner.CreateInstance)
Set Com(#com_owner) Caption(*component_desc)
Endroutine

Evtroutine Handling(#Button1.Click)
#Com_Owner.uDec2Hex Inumber(#STD_NUM) Ohexstring(#STD_TEXT)
Endroutine

Mthroutine Name(uDec2Hex) Access(*PRIVATE)
Define_Map For(*INPUT) Class(#STD_NUM) Name(#INumber) Desc('Number to convert to hex')
Define_Map For(*OUTPUT) Class(#STD_TEXT) Name(#OHexString) Desc('Number converted to hex')
*
Define Field(#W_Remain) Type(*DEC) Length(9) Decimals(0) Desc('Remainder / Index to Array')
Define Field(#W_HexChar) Type(*CHAR) Length(16) Desc('Array of hexadecimal digits') Default('0123456789ABCDEF')
*
#OHexString := *NULL
*
If_Null Field(#INumber)
#OHexString := '0'
Else
Dowhile Cond('(#INumber *GT *ZERO)')
* Remainder (plus 1) is index to array
#W_Remain := #INumber.Mod( 16 ) + 1
* Assemble hex string by placing character in front of any previously calculated characters
#OHexString := #W_HexChar.Substring( #W_Remain 1 ) + #OHexString
* Calculate quotient for next operation
#INumber := #INumber / 16
Endwhile
Endif
Endroutine
End_Com

HTH, Chris
User avatar
MARCOREMMEDATA
Posts: 14
Joined: Mon Apr 11, 2022 4:48 pm
Location: ITALIA
Contact:

Re: Using AsHexString

Post by MARCOREMMEDATA »

Hi

thank you.

I realised that the intrinsic function works but in reverse. The Hex string is inverted for pairs of characters

In a old Lansa newsletter https://lansa.com/downloads/support/new ... feb10e.pdf pag 19

Int: 49202, Window Calc for HEX: C0 32; lansa: 32 C0
Int:169999999999, Windows Calc for HEX: 27 94 CA 23 FF; lansa: FF 23 CA 94 27

I solved it by reversing the string

Code: Select all

DEFINE FIELD(#CC) TYPE(*DEC) LENGTH(3) DECIMALS(0)
DEFINE FIELD(#CRC) REFFLD(#RTE_PNPDU)
DEFINE FIELD(#REVERTCRC) REFFLD(#RTE_PNPDU)
DEF_ARRAY NAME(#REV) INDEXES(#CC) OVERLAYING(#REVERTCRC) TYPE(*CHAR) ENTRY_LEN(2)

#REVERTCRC := ( 49202 ).AsBinString.AsHexString

#CRC := ""
BEGIN_LOOP USING(#CC) FROM(128) TO(1) STEP(-1)
CONTINUE IF((#REV#CC = '00') *Or (#REV#CC = ''))
#CRC += #REV#CC
END_LOOP
MARCO ROSSI | Software Developer Sr. - Software Production
chriskarge
Posts: 5
Joined: Wed May 25, 2016 10:28 pm

Re: Using AsHexString

Post by chriskarge »

Howdy!

I noticed the octets seemed to be reversed as well. Personally, I would not use the code to reverse the errant values because you never know when LANSA might provide a correction to the intrinsic method in an EPC/upgrade, which would then cause the application to start returning incorrect results. That's just my opinion though. :-)

Take care!
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Using AsHexString

Post by Dino »

Fix maybe a hot fix, clearly a defect, if you are in maintenance, try to get that. That will solve the issue for you and everyone trying to use the same intrinsic
Post Reply