# Convert a number in attribute table to binary

I would like to convert whole integers from decimal to binary in ArcGIS using the field calculator.

I am looking for the equivalent of DEC2BIN excel command.

All values are 255 or below so 8 bit is fine.

It must have 0 padding to the left. So for example 4 must be 00000100.

I can do it by exporting the attribute table to Excel and using the Dec2Bin command in Excel and rejoin the data but I would like it in a model so want to use the field calculator.

Dec2Bin (Value, 8) works in Excel.

Attached is how I would like it to look when complete. So basically change the value to and 8 digit binary value stored as a text field with leading 0s so all field have 8 values.

I found this code in arcscripts but cannot get it to function.

Dim valI as integer Dim valB as string Dim x as integer valI = [fieldName] for x = 7 to 0 step -1 if valI >= 2^x then valB = valB & 1 valI = valI - 2^x else valB = valB & 0 end if Next valB = valB & "00000000"

fieldName = valB

• Just to clarify I'm looking for a field calculator expression to change the [value] to binary as shown above in the 'textbin field. This was done via an excel join.
– user17614
Aug 7, 2013 at 17:45

``````>>> bin(100)
• To expand on this: using the field calculator, set the parser to Python, then use the expression `bin(!VALUE!)[2:].zfill(8)`, where VALUE is the name of the field you want to convert. Aug 7, 2013 at 17:41