0

The FormatNumber function can convert decimal to hex but how do I convert from hex back to decimal? It doesn't work and breaks with error:

An error occurred when attempting to evaluate a FormatNumber function call. The input value (7D29896) cannot be formatted as a number. The first parameter value for a FormatNumber call must be a numeric type supported by the requested format. Value: 7D29896 Format: D

Code:

%%[
@VAR myNum
SET @myNum = 131242134
SET @Hex = FormatNumber(@myNum, "X")
SET @Decimal = FormatNumber(@Hex, "D") /* breaks on this call */
]%%

Original:  %%=v(@myNum)=%%
HEX:  %%=v(@Hex)=%%
DEC:  %%=v(@Decimal)=%%
  • So you're trying to format or convert a number that's not a number? Don't you already have the number value in @myNum? It'd help to know what you're trying to accomplish. – Adam Spriggs Nov 17 at 18:42
  • The problem he is facing is the following: You cannot set a hexadecimal number into a variable that is numeric. If you try to do set @num = FF it won't work. Then if you do set @num = "FF" it is not a numeric type any longer -> you cannot use FormatNumber because it requires a numeric. Do you have a plan for that? ;) – Johannes Schapdick Nov 18 at 7:43
2

The problem is where the ??? is. This is not an answer but helps to understand. The problem he is facing is the following: You cannot set a hexadecimal number into a variable that is numeric. If you try to do set @num = FF it won't work. Then if you do set @num = "FF" it is not a numeric type any longer -> you cannot use FormatNumber because it requires a numeric. Even when you cast a variable with formatnumber into a hex numeric, you cannot use this variable to perform hex back to decimal.

 <script runat="server">
        /* Surrounding SSJS try catch block start to get every error that might happen */
        /* Not loading platform core library because of performance aspects */
        Platform.Load("Core", "1.1.5");
        try {
    </script>
%%[
@VAR myNumDec, @myNumHex
SET @myNumDec = 255
SET @Hex = FormatNumber(@myNumDec, "X") 

SET @myNumHex = ???
SET @Dec = FormatNumber(@myNumHex,"D")
]%%
<script runat="server">
     } catch (e) {
        Write("<br>" + Stringify(e));
    }
</script>

OriginalDecimal:  %%=v(@myNumDec)=%%
DecToHex:  %%=v(@Hex)=%%

OriginalHex:  %%=v(@myNumHex)=%%
HexToDEC:  %%=v(@Dec)=%%
| improve this answer | |
1

I don't think there is a built in function in ampscript to achieve that. Anyway, below my SSJS option in case it is useful for you:

<script runat=server>
    Platform.Load("core","1.1.1");

     var myNum = 131242134;
     var Hex = myNum.toString(16);
     var Decimal = parseInt(Hex, 16);

     Write("Original: " + myNum);
     Write("HEX: " + Hex);
     Write("DEC: " + Decimal);
</script>
| improve this answer | |
  • Doesn't ssjs have sever performance impact? – Jordan Nov 18 at 20:39
  • Nope, it is true that in million records processing emails there could be a different performance between SSJS and ampscript, but should not make a significant impact. – Javier De Mauri Nov 18 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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