Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I am loading a file that contains a column holding a binary sequence (bits) and need to convert it to a decimal value. An example is the sequence: 000010111101011101111010 which converts to the decimal value 776058. The math to do this is:
0×2²³+0×2²²+0×2²¹+0×2²⁰+1×2¹⁹+0×2¹⁸+1×2¹⁷+1×2¹⁶+1×2¹⁵+1×2¹⁴+0×2¹³+1×2¹²+0×2¹¹+1×2¹⁰+1×2⁹+1×2⁸+0×2⁷+1×2⁶+1×2⁵+1×2⁴+1×2³+0×2²+1×2¹+0×2⁰ = 776058
...but, the length of the incoming decimal value may vary so, I don't want to hardcode anything. Ideally, if there was a function in Qlik or a way of parsing this string iteratively....
Likewise, I also have hexadecimal values that need to be converted into binary.
Any creative approaches would be appreciated.
Got it. So this was your alternative method. Thanks Stefan
HI,
Try like this
Decimal :
LOAD Binary, Sum(DecimalValue) as Decimal
GROUP BY Binary;
LOAD Binary,
Alt(Pow(If(Mid(Binary, IterNo(), 1) = 1, 2), Length-IterNo()), 0) as DecimalValue
While Iterno() <= Length;
LOAD
Binary,
Len(Binary) as Length
FROM DataSource;
STORE Decimal INTO Decimal.qvd;
Regards,
jagan.
Thanks to Sunny and swuehl for sparking the solutions...all of yours worked for the conversion just great but, for my very specific situation, Rob's was the one that worked out best.
Thanks Rob, Sunny, & swuehl!
One solution using the splitting into 14 digits at a time could look like this:
I have reported this as a bug.
And if I may, I would like to show a different solution to the problem:
First, create a mapping table that converts a four-digit BIN to HEX:
Bin2Hex:
Mapping Load
Right('0000' & Num(RecNo()-1,'(BIN)'),4) as BIN,
Num(RecNo()-1,'(HEX)') as HEX
Autogenerate 16;
Use this to convert the number to HEX, and then use the Num#() function:
Num(Num#(MapSubString('Bin2Hex',Right('0000' & Binary, 4*Ceil(Len(Binary)/4))),'(HEX)')) as Number,
The 14-digit limitation is still there, but now we talk about 14 HEX-digits. And that's slightly better....
HIC