RE: [EXT] Convert an integer to a MAC address in unlang / expr
Thanks Brian and Alan for your advice. If I just perform the mask transform on the last octet this works but of course it returns the result as an int, so I can't find any built in function to get that back to hex. This is what I've ended up with; please let me know if you feel there is a better/cleaner way without needing the IF statement update control { &Tmp-String-1 := "%{1}-%{2}-%{3}-%{4}-%{5}" &Tmp-String-2 := "%{expr: 0x%{6} & 0xC0}" } if (&control:Tmp-String-2 && &control:Tmp-String-2 != "") { if (&control:Tmp-String-2 == "00") { update control { &Tmp-String-2 := "00" } } if (&control:Tmp-String-2 == "64") { update control { &Tmp-String-2 := "40" } } if (&control:Tmp-String-2 == "128") { update control { &Tmp-String-2 := "80" } } if (&control:Tmp-String-2 == "192") { update control { &Tmp-String-2 := "C0" } } update request { &Called-Station-Id := "%{control:Tmp-String-1}-%{control:Tmp-String-2}" } } Thanks, James
On Aug 27, 2020, at 6:44 AM, James Wood <james.wood@purplewifi.com> wrote:
If I just perform the mask transform on the last octet this works but of course it returns the result as an int, so I can't find any built in function to get that back to hex.
There's %{hex:...} but it only works on attributes, not raw numbers.
This is what I've ended up with; please let me know if you feel there is a better/cleaner way without needing the IF statement
After some thinking I don't think there is. I'll see if I can add some features to make this easier. Alan DeKok.
James Wood <james.wood@purplewifi.com>
Thanks Brian and Alan for your advice.
If I just perform the mask transform on the last octet this works but of course it returns the result as an int, so I can't find any built in function to get that back to hex.
This is what I've ended up with; please let me know if you feel there is a better/cleaner way without needing the IF statement
Take only the first digit into %{6}. Then mask with 0xC. Then divide by 16. 0/16 is 0, 64/16 is 4, 128/16 is 8 and 192/16 is 12. So then just one if statement makes 12 into "C" and you can just put that back in with a hard coded 0 after it... no need to convert 0 to "0" or 4 to "4" or 8 to "8", they should stringify on their own.
participants (3)
-
Alan DeKok -
Brian Julin -
James Wood