1

I've this command that converts a character EBCDIC to Hexadecimal on my Iseries (AS400) and it works perfect.

sprintf((char*)(codeHex),"%02X", input[loop]);   

Now, I would like to do the opposite command, I mean.. from an hexadecimal code, convert it to a character EBCDIC, and move it to a string char. How can I do it?

Now the info that I recieve has this format:

char input[300] ="0x004C0x004F0x00430x004B0x00450x00440x0000..."; 
sprintf((char*)(VariableCharacterEBCDIC),"?..", input[loop]);

Regards,

sigsag
  • 37
  • 8
  • 1
    The counterpart to `printf()` is `scanf()`. – alk Nov 22 '19 at 14:01
  • See [How to use `sscanf()` in loops?](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops). Note that you'll need to be careful and/or clever to avoid having `sscanf()` read `0x004C0x…` as hex constant 0x004C0. You'll probably need `%6x` to limit the data read. – Jonathan Leffler Nov 22 '19 at 14:04
  • Assuming you can be sure to have an even number of hex digits and you want to convert two hex digits each to a character value, I suggest to implement a loop that (after skipping the `"0x"`) copies 2 characters each from your input to a temporary string and uses `strtol` (if necessary with pre-checking or error handling) to convert it as a base 16 number until you reach the end of the input string. – Bodo Nov 22 '19 at 15:01

2 Answers2

3

Rather than building your own function, why not use the functions built into the MI level of the OS..

Convert Hex to Character (CVTHC)

Convert Character to Hex (CVTCH)

They are easily callable from any language on the IBM i, including C.

Note the naming/description is a little wonky, here's a cheat sheet...
CVTHC - Convert to Hex 'A' => 'C1'
CVTCH - Convert to Character 'C1' => 'A'

The RPGLE prototypes look like so:

dcl-pr tohex extproc('cvthc');                 
  hexresult char(65534) options(*varsize);     
  charinp char(32767) const options(*varsize); 
  charnibbles int(10) value;                   
end-pr;                                        

dcl-pr fromhex extproc('cvtch');               
  charresult char(32767) options(*varsize);    
  hexinp char(65534) const options(*varsize);  
  hexlen int(10) value;                        
end-pr;                                        

So for C, you're passing a couple of char pointers and an integer. I just don't recall the C equivalent of extproc('cvthc')

edit - C prototypes courtesy of Player1st

void cvthc(char* hexresult, char* charinp, int charnibbles); 
void cvtch(char* charresult, char* hexinp, int hexlen);
Charles
  • 21,637
  • 1
  • 20
  • 44
  • 1
    No need for any extproc. `void cvthc(char* hexresult, char* charinpt, int charnibbles);` and `void cvtch(char* charresult, char* hexinp, int hexlen);` will work just fine. Feel free to update your answer with this. – Player1st Nov 22 '19 at 17:09
  • @Player1st done...thank you... my C experience was a long time ago. Isn't there a way to use an alternate name in C? – Charles Nov 22 '19 at 17:13
  • The typical way is to just use a macro or a typedef to do so. The IBM i version of C may have some special syntactic sugar for it but I don't know it off the top of my head if it exists. – Player1st Nov 22 '19 at 17:15
  • 1
    Just in case you want it, the syntax for renaming using a macro is `#define fromhex cvtch` and `#define tohex cvtch`. You would still need the prototype definitions from earlier as well. – Player1st Nov 22 '19 at 23:21
1

The C prototypes are in QSYSINC/MIH, members CVTCH and CVTHC.

For RPG, you need to code your own prototypes.

Barbara Morris
  • 3,195
  • 8
  • 10