Excel Conversion

VBA and general Excel Q&A and examples
Post Reply
Phillip Mathis
Posts: 21
Joined: Thu Dec 10, 2009 10:55 pm

Excel Conversion

Post by Phillip Mathis »

I have a question about unit conversions. First, I have data which is recorded in hex and I'm tying to convert it to binary. If the hex number is numerical, excel treats it as an integer (1840). If it contains a letter (C08A) excel treats it as text. So I'm trying to create code that will find the first value (MSB); C in the example above. Then convert this value to binary and then move to the next hex value. After conversion, I need to reassemble these values from MSB to LSB.

Your assistance would be greatly appreciated.
User avatar
DPlotAdmin
Posts: 2312
Joined: Tue Jun 24, 2003 9:34 pm
Location: Vicksburg, Mississippi
Contact:

Post by DPlotAdmin »

On the spreadsheet you can use HEX2DEC, as in
=HEX2DEC(A1) (if your hex value is in cell A1)

and
=INT(B1/256)+256*MOD(B1,256)
to byte-swap, assuming your converted decimal value is in B1.

A nice feature about HEX2DEC is it doesn't assume anything about your input: HEX2DEC(1840) will treat 1840 as a hexadecimal value even though it shows as a normal decimal number on your sheet.

In VBA you just need to add WorksheetFunction, as in:

Cells(1,2) = WorksheetFunction.Hex2Dec(Cells(1,1))
Visualize Your Data
support@dplot.com
Phillip Mathis
Posts: 21
Joined: Thu Dec 10, 2009 10:55 pm

Post by Phillip Mathis »

Thanks David, the WorksheetFunction is what I was missing.
Post Reply