How can I get the EBCDIC value of a character in RPGLE?
ibm-midrange, rpg, rpgle
Solution
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
Problem
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.