Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Converting binary sequence to decimal

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.

14 Replies
sunny_talwar

Got it. So this was your alternative method. Thanks Stefan

jagan
Luminary Alumni
Luminary Alumni

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.

Not applicable
Author

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!

petter
Partner - Champion III
Partner - Champion III

One solution using the splitting into 14 digits at a time could look like this:

2016-01-15 #1.PNG

hic
Former Employee
Former Employee

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