4

I need to have some way of converting single characters in RPGLE into integers - does anyone know a good way? It has to work for all possible inputs and ideally provide a different integer for each input - at the very least it must provide a different value for all common inputs. I don't care particularly what the integers are. In a C like language I would take the ASCII value or similar - ideally I want something equivalent to that.

Example to make it clear how I want it to work:

characterData = "Hello";
for i = 1 to %len(string);
    singleCharacter = %subst(characterData:i:1);
    number = myFunction(singleCharacter);
    dsply 'The value of ' + singleCharacter + ' is ' + %char(number);
endfor;

This would print

The value of H is 72
The value of e is 101
The value of l is 108
The value of l is 108
The value of o is 111

but note that I actually don't care what the numbers are, just that they are different for each input.

All of this in aid of building a hash function for character data in RPGLE, so if you know a good way of doing that then that would be a better answer.

2
  • Is this hash going to be used for encryption? There are encryption functions in RPG that would do that for you. I have used the encryption functions. If this is the case, I can post my test app here for you to look at.
    – Mike Wills
    Oct 20, 2009 at 20:29
  • Yep I was just here looking for an ASCII function
    – danny117
    Jan 31, 2019 at 17:01

2 Answers 2

6

Create a datastructure overlaying the field positions. The first field, in position 1, is a 1-byte character field. The second field, also in position 1, is a 1-byte un-signed integer field. Move the character in question to the character field and then your un-signed integer field will have the EBCDIC value you need. Here is an example:

DConversion       DS
D CharacterValue          1      1A
D EBCDICValue             1      1U 0

/free
 CharacterValue = 'A';
 //Do something with EBCDICValue
/end-free

Because the two fields occupy the same position, changing one changes the other. Your program is just using the two variables to look at the same byte in memory in different ways.

You can get fancier by having a larger character field and have an array of 1-byte un-signed integers in the same place like this:

DConversionArray  DS                          
D CharacterField          1    100            
D EBCDICArray             1    100U 0 DIM(100)

/free
 CharacterField = 'We the people of the United States...';
 For I = 1 to %Len(%TrimR(CharacterField));
   X = EBCDICArray(I);
   //Do something with X
 EndFor;                                                  
/end-free

In the above example, you loop through the size of the character field and do something with each EBCDIC value.

Finally, if you're feeling really cool, you can create a 1-byte un-signed integer field and assign it to a pointer. Then you can scan through any character or varchar field in your program regardless of its size. For each byte in the character field, assign its memory address to the pointer assigned to your 1-byte un-signed integer field. Like this:

DEBCDICValue      S              3U 0 BASED(EBCDICPointer)
DEBCDICPointer    S               *
/free
 For I = 0 to %Len(%TrimR(CharacterField))-1;
   EBCDICPointer = %Addr(CharacterField)+I;
   X = EBCDICValue;
   //Do something with X
 EndFor;
/end-free
0
0

I can get you to the ascii value for uppercase A afaik its 65??? You can modify to get ebcdic values. This is easier than making a new function and deciding what service program to put it in.

d*Seen this and wonder why we still use it but its probably already there...
D Lo              c                   const('abcdefghijklmnopqrstuvwxyz')  
D Up              c                   const('ABCDEFGHIJKLMNOPQRSTUVWXYZ')  

eval x=%scan('A':UP)
if x <> 0
eval x +=64
endif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.