Focal Point Banner


As of December 1, 2020, Focal Point is retired and repurposed as a reference repository. We value the wealth of knowledge that's been shared here over the years. You'll continue to have access to this treasure trove of knowledge, for search purposes only.

Join the TIBCO Community
TIBCO Community is a collaborative space for users to share knowledge and support one another in making the best use of TIBCO products and services. There are several TIBCO WebFOCUS resources in the community.

  • From the Home page, select Predict: WebFOCUS to view articles, questions, and trending articles.
  • Select Products from the top navigation bar, scroll, and then select the TIBCO WebFOCUS product page to view product overview, articles, and discussions.
  • Request access to the private WebFOCUS User Group (login required) to network with fellow members.

Former myibi community members should have received an email on 8/3/22 to activate their user accounts to join the community. Check your Spam folder for the email. Please get in touch with us at community@tibco.com for further assistance. Reference the community FAQ to learn more about the community.


Focal Point    Focal Point Forums  Hop To Forum Categories  WebFOCUS/FOCUS Forum on Focal Point     [SOLVED] Converting a string to hexadecimal

Read-Only Read-Only Topic
Go
Search
Notify
Tools
[SOLVED] Converting a string to hexadecimal
 Login/Join
 
Virtuoso
posted
I had a need for converting strings to strings of hexadecimal encoded byte values and created the following to implement that. I wonder whether people see opportunities for improvement?

To be more specific, we have a report that allows users to select data-sets based on a specific key (a string value). Those key's contain all kinds of characters that are invalid in a HOLD file name, but we need to be able to uniquely identify each HOLD file based on its key string. Hence, encoding those strings as hexadecimal. The user can then select several of these data-sets to combine the results (as a logical AND).

I defined a few functions to convert a single character to a hexadecimal value of it's ordinal number:
-* File char2hex.fex
DEFINE FUNCTION DEC2HEX(DECVAL/I3)
	HEX/A512 = '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F' ||
 		'202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F' ||
 		'404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F' ||
		'606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F' ||
		'808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F' ||
		'A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF' ||
		'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF' ||
		'E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF';
	DEC2HEX/A2 = SUBSTR(512, HEX, DECVAL *2 +1, DECVAL *2 +2, 2, DEC2HEX);
END

DEFINE FUNCTION CHAR2HEX(CHAR/A1)
	DECVAL/I3 = BYTVAL(CHAR, DECVAL);
	CHAR2HEX/A2 = DEC2HEX(DECVAL);
END

I think that's fairly efficient.

This is subsequently used in a loop over the user's input string as follows
-* File string2hash.fex
-DEFAULT &STR = '';
-DEFAULT &OUTPUT = '';
-SET &LEN = &STR.LENGTH;

-INCLUDE CHAR2HEX

-REPEAT :LOOP FOR &I FROM 1 TO &LEN;
-SET &CHAR = SUBSTR(&LEN, &STR, &I, &I, 1, 'A1');
-SET &OUTPUT = &OUTPUT || CHAR2HEX(&CHAR);
-:LOOP

-TYPE &OUTPUT


That's the part that I'm less happy about. A loop with SUBSTR doesn't seem very efficient.

In the end, this is used in this manner in our report:
-DEFAULT &KEY = 'Some key we''d like to convert to hexadecimal';
-SET &OUTPUT = '';
-INCLUDE STRING2HASH
-SET &HOLDFILE = 'YADAYADA_' || &OUTPUT;

TABLE FILE YADAYADA
PRINT *
BY ARTICLE_CODE
WHERE KEY EQ '&KEY';
ON TABLE HOLD AS FOCCACHE/&HOLDFILE FORMAT FOCUS INDEX KEY
END

Which creates a HOLD-file named YADAYADA_536F6D65206B65792077652764206C696B6520746F20636F6E7665727420746F2068657861646563696D616C

We keep a list of KEYs used previously in the same (http-)session, together with their associated HOLD-files. This list is available to the user to select KEYs from that they want to combine the results of the current KEY with, such that they get a report of ARTICLE_CODEs that match all selected KEYs.

This message has been edited. Last edited by: Wep5622,


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
 
Posts: 1669 | Location: Enschede, Netherlands | Registered: August 12, 2010Report This Post
Expert
posted Hide Post
I quickly threw this together. Not totally neat but something you could play with a little. I tried to include the FPRINT into the decode but it didn't like. Anyway, to get something quickly I dropped the FPRINT out but then of course had to repeat the DECODE! Frowner

Have a play, it might give you different ideas?

T

DEFINE FUNCTION DEC2HEX(DECVAL/I3)
  NUMBFST/I3 = INT(DECVAL / 16);
  NUMBLST/I3 = DECVAL - (16 * NUMBFST);
  SNGLFST/A1 = FPRINT(NUMBFST, 'I1', 'A1');
  SNGLLST/A1 = FPRINT(NUMBLST, 'I1', 'A1');
  CHARFST/A1 = IF (DECODE NUMBFST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X')) EQ 'X' THEN SNGLFST ELSE DECODE NUMBFST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X');
  CHARLST/A1 = IF (DECODE NUMBLST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X')) EQ 'X' THEN SNGLLST ELSE DECODE NUMBLST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X');
  DEC2HEX/A2 = CHARFST || CHARLST;
END
-RUN

-REPEAT :Loop FOR &CHARVAL FROM 0 TO 255;
-SET &CHARHEX = DEC2HEX(&CHARVAL);
-TYPE &CHARVAL &CHARHEX
-:Loop



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
 
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004Report This Post
Expert
posted Hide Post
A little tidier ....

DEFINE FUNCTION DEC2HEX(DECVAL/I3)
  NUMBFST/I3 = INT(DECVAL / 16);
  NUMBLST/I3 = DECVAL - (16 * NUMBFST);
  CHARFST/A1 = DECODE NUMBFST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X');
  CHARLST/A1 = DECODE NUMBLST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X');
  CHARFST/A1 = IF CHARFST EQ 'X' THEN FPRINT(NUMBFST, 'I1', 'A1') ELSE CHARFST;
  CHARLST/A1 = IF CHARLST EQ 'X' THEN FPRINT(NUMBLST, 'I1', 'A1') ELSE CHARLST;
  DEC2HEX/A2 = CHARFST || CHARLST;
END
-RUN

-REPEAT :Loop FOR &CHARVAL FROM 0 TO 255;
-SET &CHARHEX = DEC2HEX(&CHARVAL);
-TYPE &CHARVAL &CHARHEX
-:Loop


T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
 
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004Report This Post
Expert
posted Hide Post
Just did a search and found this post from Francis (very last one) - glad to say(?) that his solution then was very similar to the one I have suggested above.

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
 
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004Report This Post
Expert
posted Hide Post
Using that function with a master placed over the string and using occurs to read each character in the string, you can get the output string like this -

-DEFAULT &KEY = 'Some key we''d like to convert to hexadecimal';

DEFINE FUNCTION DEC2HEX(DECVAL/I3)
  NUMBFST/I3 = INT(DECVAL / 16);
  NUMBLST/I3 = DECVAL - (16 * NUMBFST);
  CHARFST/A1 = DECODE NUMBFST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X');
  CHARLST/A1 = DECODE NUMBLST(10 'A' 11 'B' 12 'C' 13 'D' 14 'E' 15 'F' ELSE 'X');
  CHARFST/A1 = IF CHARFST EQ 'X' THEN FPRINT(NUMBFST, 'I1', 'A1') ELSE CHARFST;
  CHARLST/A1 = IF CHARLST EQ 'X' THEN FPRINT(NUMBLST, 'I1', 'A1') ELSE CHARLST;
  DEC2HEX/A2 = CHARFST || CHARLST;
END
-RUN

FILEDEF STR2CONV DISK STR2CONV.ftm
-RUN
EX -LINES 5 EDAPUT MASTER,STR2CONV,CF,MEM,FILENAME=STR2CONV, SUFFIX=FIX,$
SEGNAME=ONE, SEGTYPE=S0, $
  FIELD=CHARSTRING, ,A&KEY.LENGTH  ,A&KEY.LENGTH  ,$
SEGNAME=TWO, PARENT=ONE, OCCURS=&KEY.LENGTH, POSITION=CHARSTRING,  SEGTYPE=S, $
  FIELD=CHAR,       ,A1   ,A1   ,$
-RUN
-WRITE STR2CONV &KEY
-RUN

TABLE FILE STR2CONV
PRINT COMPUTE HEXCHARS/A2 = DEC2HEX(BYTVAL(CHAR, 'I3'));
      COMPUTE ORDER/I3 = ORDER + 1;
   BY CHARSTRING NOPRINT
ON TABLE HOLD AS TEMPHLD1
END
-RUN

TABLE FILE TEMPHLD1
SUM HEXCHARS AS 'OUTPUT'
ACROSS ORDER NOPRINT
ON TABLE SAVE AS CHAROUT
ON TABLE SET HOLDLIST PRINTONLY
ON TABLE SET ASNAMES ON
END
-RUN

-SET &OutLen = &KEY.LENGTH * 2;
-READ CHAROUT &OUTPUT.A&OutLen.EVAL

-TYPE &OUTPUT


T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
 
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004Report This Post
Virtuoso
posted Hide Post
Any reason why you wouldn't use UFMT?

-SET &CHAR2HEX='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
DEFINE FILE CAR
OUT/A72=UFMT('&CHAR2HEX', 36, 'A72');
END
TABLE FILE CAR 
PRINT 
  COMPUTE HEX/A20 = UFMT(COUNTRY,10,'A20');
  OUT
BY COUNTRY
END  


"There is no limit to what you can achieve ... if you don’t care who gets the credit." Roger Abbott
 
Posts: 1102 | Location: Toronto, Ontario | Registered: May 26, 2004Report This Post
Expert
posted Hide Post
I guess some of us didn't know about UFMT.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
 
Posts: 10577 | Location: Toronto, Ontario, Canada | Registered: April 27, 2005Report This Post
Virtuoso
posted Hide Post
So a function like that does exist - thanks!

That I (and clearly several others) did not manage to find it has no doubt to do with the unfathomable name of the function and the somewhat dubious location in the documentation. Why is that function called UFMT?

It's also not really converting the 'format' of the field, both input and output are alphanumeric fields. There are all kinds of similar functions that change case of characters or turn strings into patterns and such and all those are found under "String Functions". This one is under "Format Conversion Functions" - go figure...

Well, that is kind of typical for WebFOCUS, which is why I figured I should ask around.


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
 
Posts: 1669 | Location: Enschede, Netherlands | Registered: August 12, 2010Report This Post
Expert
posted Hide Post
Yup, the function has been around since at least v7.6. I usually scour the manuals, but this seemed to have remained hidden until today.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
 
Posts: 10577 | Location: Toronto, Ontario, Canada | Registered: April 27, 2005Report This Post
Virtuoso
posted Hide Post
Been in all version of FOCUS since version 3. Not sure when that was ... late 70's or early 80's.

Oh yeah, I believe it stands for Unformatted read. At least that is what Noreen told me a long while back.


"There is no limit to what you can achieve ... if you don’t care who gets the credit." Roger Abbott
 
Posts: 1102 | Location: Toronto, Ontario | Registered: May 26, 2004Report This Post
Virtuoso
posted Hide Post
TABLE FILE CAR
PRINT COUNTRY/U20 
BY COUNTRY
ON TABLE SET ONLINE-FMT STANDARD AND PAGE NOLEAD
END

1 COUNTRY                                      COUNTRY                          
  -------                                      -------                          
  ENGLAND     454E474C414E4420202020202020202020202020
  FRANCE      4652414E43452020202020202020202020202020
  ITALY       4954414C59202020202020202020202020202020
  JAPAN       4A4150414E202020202020202020202020202020
  W GERMANY   57204745524D414E592020202020202020202020


That's why it's called U-format.


- Jack Gross
WF through 8.1.05
 
Posts: 1925 | Location: NYC | In FOCUS since 1983 | Registered: January 11, 2005Report This Post
Expert
posted Hide Post
quote:
Any reason why you wouldn't use UFMT?

Yeah, because there are so many old functions that I forgot Frowner I put it down to age myself.

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
 
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004Report This Post
Virtuoso
posted Hide Post
And don't forget FORMAT=U:
FILENAME=car, SUFFIX=FOC     , $
  SEGMENT=ORIGIN, SEGTYPE=S1, $
    FIELDNAME=COUNTRY, ALIAS=COUNTRY, USAGE=A10, FIELDTYPE=I, $
  SEGMENT=COMP, SEGTYPE=S1, PARENT=ORIGIN, $
    FIELDNAME=CAR, ALIAS=CARS, USAGE=U16, $
.
.
.
DEFINE FILE CAR
X_COUNTRY/U10 = COUNTRY;
END
TABLEF FILE CAR
PRINT COUNTRY X_COUNTRY CAR
END

Cannot use on INDEXed fields in FOCUS db, but can be used on ACTUAL (USAGE=U4000,ACTUAL=A4000V),in other DBs.

or if you have SQL Server:
-SET &ECHO=ALL;
-SET &INPUT = 'Some key we'd like to convert to hexadecimal';
-SET &LEN = &INPUT.LENGTH*2;
SQL SQLMSS
SELECT master.dbo.fn_varbintohexstr(CAST(&INPUT.QUOTEDSTRING AS VARBINARY));
TABLE
HOLD
END
-RUN
-READ HOLD &0x.2 &OUTPUT.&LEN.EVAL
-TYPE &OUTPUT


Alan.
WF 7.705/8.007
 
Posts: 1451 | Location: Portugal | Registered: February 07, 2007Report This Post
  Powered by Social Strata  

Read-Only Read-Only Topic

Focal Point    Focal Point Forums  Hop To Forum Categories  WebFOCUS/FOCUS Forum on Focal Point     [SOLVED] Converting a string to hexadecimal

Copyright © 1996-2020 Information Builders