Convert int/decimal number into binary (16 - bit ) + 2 bit columns

Is there a simple way in flux to convert simple decimal number into binary number?

For example :

decimal : 1431655760
= binary : 0101 0101 0101 0101 0101 0101 0101 0000

If so, what is the easiest way to split the binary number into bin’s of 2 bit’s in new columns?

image

I hope someone can help me out :)!

Thanks,

@bart1992 Currently there isn’t anything in Flux that converts values to their binary representation, but you could submit this as a feature request or a contribution on the Flux project.

@scott unfortunately… But i’ve managed to do it manually :slight_smile:

import "strings"
from(bucket: "kgnv_rd")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "${DeviceID}" and (r._field == "ECU_TC_Ddi" or r._field == "ECU_TC_Value" ))
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> filter(fn: (r) => r.ECU_TC_Ddi == 161  )
  //-- Section 1 
  |> map(fn: (r) => ({ r with _value0: ((int(v: r.ECU_TC_Value) / 2 ))}))
  |> map(fn: (r) => ({ r with _bit0: (string(v:(int(v: r.ECU_TC_Value)% 2))  )}))
  |> map(fn: (r) => ({ r with _value1: (r._value0 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit1: (string(v: r._value0 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section1: (r._bit1 + r._bit0 )}))
  |> drop(columns: ["_bit0", "_bit1", "_value0","ECU_TC_Ddi","ECU_TC_Value"])

  //-- Section 2 
  |> map(fn: (r) => ({ r with _value2: (r._value1 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit2: (string(v: r._value1 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value3: (r._value2 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit3: (string(v: r._value2 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section2: (r._bit3 + r._bit2 )}))
  |> drop(columns: ["_bit2", "_bit3", "_value1", "_value2"])
  
  //-- Section 3 
  |> map(fn: (r) => ({ r with _value4: (r._value3 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit4: (string(v: r._value3 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value5: (r._value4 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit5: (string(v: r._value4 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section3: (r._bit5 + r._bit4 )}))
  |> drop(columns: ["_bit4", "_bit5", "_value3", "_value4"])
  
  //-- Section 4 
  |> map(fn: (r) => ({ r with _value6: (r._value5 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit6: (string(v: r._value5 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value7: (r._value6 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit7: (string(v: r._value6 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section4: (r._bit7 + r._bit6 )}))
  |> drop(columns: ["_bit6", "_bit7", "_value5", "_value6"])
  
  //-- Section 5 
  |> map(fn: (r) => ({ r with _value8: (r._value7 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit8: (string(v: r._value7 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value9: (r._value8 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit9: (string(v: r._value8 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section5: (r._bit9 + r._bit8 )}))
  |> drop(columns: ["_bit8", "_bit9", "_value7", "_value8"])

  //-- Section 6 
  |> map(fn: (r) => ({ r with _value10: (r._value9 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit10: (string(v: r._value9 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value11: (r._value10 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit11: (string(v: r._value10 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section6: (r._bit11 + r._bit10 )}))
  |> drop(columns: ["_bit10", "_bit11", "_value9", "_value10"])
  
  //-- Section 7 
  |> map(fn: (r) => ({ r with _value12: (r._value11 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit12: (string(v: r._value11 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value13: (r._value12 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit13: (string(v: r._value12 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section7: (r._bit13 + r._bit12 )}))
  |> drop(columns: ["_bit12", "_bit13", "_value11", "_value12"])
  
  //-- Section 8 
  |> map(fn: (r) => ({ r with _value14: (r._value13 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit14: (string(v: r._value13 % 2 ))})) 
  |> map(fn: (r) => ({ r with _value15: (r._value14 / 2 )}))  
  |> map(fn: (r) => ({ r with _bit15: (string(v: r._value14 % 2 ))})) 
  |> map(fn: (r) => ({ r with  Section8: (r._bit15 + r._bit14 )}))
  |> drop(columns: ["_bit14", "_bit15", "_value13", "_value14", "_value15"])


  //|> drop(columns: ["_bit0", "_bit1","_bit2", "_bit3","_bit4", "_bit5","_bit6", "_bit7","_bit8", "_bit9","_bit10", "_bit11","_bit12", "_bit13","_bit14", "_bit15"])
  //|> drop(columns: ["_value0", "_value1","_value2", "_value3","_value4", "_value5","_value6", "_value7", "_value8","_value9", "_value10","_value11", "_value12", "_value13","_value14", "_value15"])

Nice work! It’s certainly not ideal, but it gets the job done.