CFML InputBaseN does not return the expected value

When using Adobe ColdFusion and trying to convert a hexadecimal string to a decimal string, we don't seem to get the result we want / expect.

<cfset i = #InputBaseN("A1000050", 16)# > <cfdump var="#i#"> 

displayed -1593835440 We expected 2701131856

In the Windows calculator, when we convert A1000050 to dec qword, we get our expected result. However, if we use dword, this gives us the save value that ColdFusion gives.

Are we doing something wrong in ColdFusion? How can we get the expected value?

Binary value of the expected value (according to Windows calc programmer mode)

 0000 0000 0000 0000 0000 0000 0000 0000 1010 0001 0000 0000 0000 0000 0101 0000 

= 2701131856

The binary value that we actually get

 1010 0001 0000 0000 0000 0000 0101 0000 

= -1593835440

+5
source share
2 answers

Just to expand, Sean will answer that this is a mistake ... Traditionally, most (though not all) CF numeric functions have been limited to 32-bit integers. CF 2016 changed this by receiving inputBaseN() returning a 64-bit integer or Long. Most workarounds mentioned in the error reports try to do the opposite of what you need (repeat the old behavior under CF2016). To reproduce the new behavior in CF10 / 11, try instead of Long.parseLong () :

 // Option 1. Using javacast. Returns 2701131856 value = javacast("long", 0).parseLong("A1000050", 16); // Option 2. Using createObject. Returns 2701131856 value = createObject("java", "java.lang.Long").parseLong("A1000050", 16); 

For CF servers running Java8, technically you can also call toUnsignedLong () on the resulting Integer, but .. it's fragile. Only works with CF10 / 11 and Java8 +.

 // ONLY works under CF10/11 and Java8+. Returns -2701131856 origValue = InputBaseN(input, 16); newValue = origValue .toUnsignedLong(origValue ); 

Trycf.com example

+1
source

Source: https://habr.com/ru/post/1275078/


All Articles