decode hex to string

Author: fearandir@gmail.com (fearandir)

I want to convert a HEX value to DEC value variables RAW vRaw STRING vString endvariables   vString="500" VRaw= $decode("HEX","500") --> returns "P" vString= $encode("USTRING",VRaw) --> still returns "P". should be "1280"   what am i doing wrong?   Thanks in advance Sergio

3 Comments

  1. fearandir said I want to convert a HEX value to DEC value Sergio  

    Hi Sergio $decode("HEX","500") will interpret "500"  as a hexadecimal represtantation of a string. A better example will be $decode("HEX","41") which results in "A". There is no need to convert this to "USTRING" as it is allready a simple string What you want to do, is to interpret "500" as a hexadecimal represantation of a numeric integer value. i.e 0x500 = 1280 This is not possible with UnifAce $decode or $encode but one can write a simple function (as attached) Ingo  

    ENTRY LF_HEX2DEC
      returns string
      params
        string v_STR:IN
      endparams
      variables
        numeric v_VAL,v_NUM
        numeric v_POS,v_LEN
        string v_CHR
      endvariables
      v_VAL = 0
      v_LEN = $length(v_STR)
      IF(v_LEN==0)
        $status = 0
        RETURN(0)
      ENDIF
      FOR v_POS = 1 to v_LEN
        v_CHR = $uppercase(v_STR[v_POS:1])
        v_NUM = $scan("0123456789ABCDEF",v_CHR)-1
        IF(v_NUM<0)
          putmess "String '%%v_STR%%%' is not a hex string"
          RETURN(-1234)
        ENDIF
        v_VAL = v_VAL*16+v_NUM
      ENDFOR
      $status = 0
     RETURN(v_VAL)
    E ND ; LF_HEX2DEC

    Author: istiller (i2stiller@gmx.de)
  2. Thanks Ingo.   I dont understand the difference betweeen interpret “500”  as a hexadecimal represtantation of a string and interpret “500” as a hexadecimal represantation of a numeric integer value and what uniface is capable to do.   But ok. It´s my fault Cry Function is nice, but i think i will relly on DOS if is not a option: "osunix".commandout("cmd /C set /a dec=0x%%vString",vString)   regards, Sergio.


    Author: fearandir (fearandir@gmail.com)
  3. Hi Sergio This with "interpretation of a bit pattern" is very simple. Or not? Wink Each data item in memory does have only bits with the possible value 0 or 1 How do deal with integer values or even characters/strings? Just by interpreting not one bit but a few of them, say 8 bits in a row So e.g 00000001 stands for the integer value of 1 and 01000001 for 65 In this manner, it is called the binary system But you have to known, that this bits represent a integer value and nothing else On the other side, how to encode characters. Its by counting them from 1 to 26. But in the early days of computing, the decided to start with number 65 for the capital letter "A" up to 90 for "Z" Each of this numbers could then encode as normal integer value. "A"  -> 65  -> 01000001 "Z"  -> 90  -> 01011010 But also here: You have to know, that this bits are characters not integers. Now to HEX As this binaray representation tend to get very long, one smarte guy said: Use the numbering system based on 16 (hexadecimal) Each binary number can easily convert into a HEX-number, To distungisch between the diffirent bases, there a prefixes/infixes/postfixes to each number Binary          Decimal   Hexadecimal             0b1      1           0x1           0b10      2           0x2 ... 0b11111111    15           0xF Be aware: All you see in this text a characters, not bits, number or anything else. So "0xF" ist a string and not the number 15 Back to the topic If you do have a string, representating a hexadecimal number and want a programm to convert this string to an interger value, you need a function like hex2dec. This one takes a string value as paramter and returns a integer value. But alos here: If yiout print out this integer value, you see a string interretation of this value and not the bit pattern itself. Next thing: A "string" is a string of characters (in UnifAce, but not allways elsewhere), Each of this characters could be encode be the procedure desribed further above. In memory you got a bit pattern, that, if yout don't what for what the stand, you could tell there meaning. Some programms (incl. UnifAces) allows to interpret this pattern as a hexadecimal number and display them in this manner. This is was STRING2 = $enocde("HEX",STRING1) does You do have a memory pattern STRING1 and got an string STRING2 as hexadecimal representation of STRING1 STRING1=$decode("HEX",STRING2) does the opposite Check if the string STRING2 only contains valid characters for a hexadecimal represenation of a number, convert each character into a number and write this bit pattern back to memory STRING1. Next time you read STRING1 annd display it, you see the original characters. URAW and USTRING are "AddOns" by UnifAce dealing with some odd behavior when using characters outside the "nomal" ASII set like GOLD-characters Btw: Why not using SQL: SELECT 0x500*1 Ingo


    Author: istiller (i2stiller@gmx.de)