Icon

kn_​forum_​61537_​decimal_​to_​binary_​conversion_​python

Conversion decimal to binary - using Python nodes and IEEE 754 binary representation of floating-point numbers

Conversion decimal to binary - using Python nodes and IEEE 754 binary representation of floating-point numbers

Conversion decimal to binary - using Python nodes and IEEE 754 binary representation of floating-point numbershttps://forum.knime.com/t/conversion-decimal-to-binary/61537/3?u=mlauber71 import knime.scripting.io as knioimport numpy as npimport pandas as pdimport structdf = knio.input_tables[0].to_pandas()def convert_to_binary_hex(df, column_name): # A list of floating-point numbers double_numbers = df[column_name].tolist() # Convert each floating-point number to its binary and hexadecimal representation binary_numbers = [] hexadecimal_numbers = [] for i in double_numbers: # Pack the floating-point number into a bytes object in the IEEE 754 standard format packed = struct.pack("!d", i) # Convert the packed bytes object to a binary string binary_number = ''.join(format(x, '08b') for x in packed) binary_numbers.append(binary_number) # Convert the packed bytes object to a hexadecimal string hexadecimal_number = ''.join(format(x, '02x') for x in packed) hexadecimal_numbers.append(hexadecimal_number) # Create a new column in the original DataFrame to store the binary representation df['Binary'] = binary_numbers # Create a new column in the original DataFrame to store the hexadecimal representation df['Hexadecimal'] = hexadecimal_numbers return df # Example usage# df = pd.DataFrame({'Double': [10.5, 20.2, 30.7, 40.3, 50.8]})result = convert_to_binary_hex(df, 'Double')knio.output_tables[0] = knio.Table.from_pandas(result) decimal_numbers = [10, 20, 30, 40, 50]IEEE 754 binary representation of floating-point numbershttps://www.h-schmidt.net/FloatConverter/IEEE754.htmlDoubleIEEE 754 binary representation of floating-point numbershttps://www.h-schmidt.net/FloatConverter/IEEE754.htmlresult_binary.parquet Python Script Python Script Table Creator Python Script Parquet Writer Conversion decimal to binary - using Python nodes and IEEE 754 binary representation of floating-point numbershttps://forum.knime.com/t/conversion-decimal-to-binary/61537/3?u=mlauber71 import knime.scripting.io as knioimport numpy as npimport pandas as pdimport structdf = knio.input_tables[0].to_pandas()def convert_to_binary_hex(df, column_name): # A list of floating-point numbers double_numbers = df[column_name].tolist() # Convert each floating-point number to its binary and hexadecimal representation binary_numbers = [] hexadecimal_numbers = [] for i in double_numbers: # Pack the floating-point number into a bytes object in the IEEE 754 standard format packed = struct.pack("!d", i) # Convert the packed bytes object to a binary string binary_number = ''.join(format(x, '08b') for x in packed) binary_numbers.append(binary_number) # Convert the packed bytes object to a hexadecimal string hexadecimal_number = ''.join(format(x, '02x') for x in packed) hexadecimal_numbers.append(hexadecimal_number) # Create a new column in the original DataFrame to store the binary representation df['Binary'] = binary_numbers # Create a new column in the original DataFrame to store the hexadecimal representation df['Hexadecimal'] = hexadecimal_numbers return df # Example usage# df = pd.DataFrame({'Double': [10.5, 20.2, 30.7, 40.3, 50.8]})result = convert_to_binary_hex(df, 'Double')knio.output_tables[0] = knio.Table.from_pandas(result) decimal_numbers = [10, 20, 30, 40, 50]IEEE 754 binary representation of floating-point numbershttps://www.h-schmidt.net/FloatConverter/IEEE754.htmlDoubleIEEE 754 binary representation of floating-point numbershttps://www.h-schmidt.net/FloatConverter/IEEE754.htmlresult_binary.parquet Python Script Python Script Table Creator Python Script Parquet Writer

Nodes

Extensions

Links