ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Get Hex string of a number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get Hex string of a number

    I need to get the hex string of a numeric field's value.

    For example, if a numeric field has a value of 177, I need get the string "B1" using RPGLE (v7.3).

    I can't use any SQL functions.

    Any ideas?

    Thanks,
    Mike
    Last edited by mlopez01; January 18, 2023, 02:44 PM.

  • #2
    Have you tried to google it?
    for example after "RPG convert to hex"?

    In this tip I’m going to address a question that arises regularly on RPG-oriented Internet lists, namely: “Is there an easy way to convert a character string to its hexadecimal equivalent?” One answer, of course, would be to write your own routine using lookup tables, but there is a far easier way. We can take

    Comment


    • JonBoy
      JonBoy commented
      Editing a comment
      You beat me to it Peder!

  • #3
    Thanks for the response.

    Of course I Googled it.

    I know those MI instructions, and have used them in the past, but the input was not character, it was a packed decimal. I didn't want "177F", nor did I want "F1F7F7". I wanted the string "B1".

    After overthinking it for a while I wound up prototyping the input parameter of "cvthc" as a pointer. I then introduced a uint variable to the program and assigned it the value of the packed decimal field, and passed the address of the uint field to "cvthc". The output was the string "000000B1" and I was able to work with this.

    I was thinking that it would be nice to have a %hex() BIF but in reality over the course of my career I can count on one hand the number of times I needed something like this.

    Mike​

    Comment


    • #4
      If your value of 177 equates to B1 then it wasn't packed decimal. 177 packed would be 177F unless I'm much mistaken. 177 only equates to B1 if it is a one byte integer. That could have been handled just by mapping the one-byte integer as a character field. e.g.

      Code:
      dcl-ds  char;
         myInt  int(3) Inz(177);
      end-ds;
      It is rarely a good idea to have "custom" versions of prototypes.

      Comment


      • #5
        The hexidecimal equivalent of decimal (base10) 177 is B1. 177 is being stored in a packed decimal field. In order to get the hex equivalent as a string it needs to be assigned to an unsigned integer (not signed integer that you specified in your example because it would overflow) and run through "cvthc". Finally the result (char in your example) needs to be run through "cvtch" to get string "B1" as a 1-byte character whose hex value is B1.

        I agree and I didn't like changing the prototype. I'm working on streaming images to a SATO and it's pretty involved and I didn't see the simpler solution, that being the one you provided.

        Thanks for the tip. I'm putting the prototype back and using the data structure (with uns(3), not int(3)).

        Mike​

        Comment

        Working...
        X