Converting from hexadecimal to binary in Igor 8
lorien
I am parsing a string of data from an instrument that contains values in hex, but I need to convert them to binary in order to interpret them in a useful way (each bit corresponds to the state of a channel from the instrument).
From what I can tell, there doesn't seem to be a native function in Igor to convert from binary to hex or back. I know that Igor can interpret hex as a decimal value (ie 0xFF = 255), and I am aware that there are some functions that read binary data (VDTReadBinary2()), but I couldn't find a simple translator for hex -> binary.
Is such a function available, or should I try implementing one myself?
Thanks so much in advance for any input!
The sscanf operation will do it. Here is a simple example:
Variable var
sscanf "0xff34", "%x", var
print var
end
65332
The receiving variable must be a variable, so if you want the result in a wave you must assign it after the call to sscanf.
September 17, 2021 at 03:36 pm - Permalink
I suggest you execute the following command to bring up the related help topic:
DisplayHelpTopic "Using Bitwise Operators"
You can do what john suggested to get the hex value into a numeric variable. Then you can test each of the bits to get the state of the channel.
If you really need this in a binary representation (eg. "01011010101010") then you can use printf with the %b conversion character.
September 17, 2021 at 04:47 pm - Permalink
Thank you, these comments were very helpful!
I ended up using aclight's second suggestion, which was using sscanf suggested by John and then using sprintf with the %b character:
sprintf binary_value, "%b\r", int_value
Thanks again for your help!
October 13, 2021 at 08:50 am - Permalink