MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2009, 06:26 AM   #1
Jaymond Flurrie
 
Join Date: Sep 2008
Posts: 289
Default VBA Hex-to-Dec

This should be easy for anyone who has ever used hex numbers.

So, I use Hex$(334) and get as a result 14E. How do I convert that 14E to 334? I tried to look for function "dec$", but clearly it's something else. What?
Jaymond Flurrie is offline   Reply With Quote
Old Mar 24th, 2009, 06:31 AM   #2
Makrini
 
Join Date: May 2007
Location: Brisbane, Australia
Posts: 1,034
Default Re: VBA Hex-to-Dec

HEX2DEC
Show All
Hide All
Converts a hexadecimal number to decimal.

If this function is not available, and returns the #NAME? error, install and load the Analysis ToolPak add-in.

How?

On the Tools menu, click Add-Ins.
In the Add-Ins available list, select the Analysis ToolPak box, and then click OK.
If necessary, follow the instructions in the setup program.
Syntax

HEX2DEC(number)

Number is the hexadecimal number you want to convert. Number cannot contain more than 10 characters (40 bits). The most significant bit of number is the sign bit. The remaining 39 bits are magnitude bits. Negative numbers are represented using two's-complement notation.

Remark

If number is not a valid hexadecimal number, HEX2DEC returns the #NUM! error value.

Example

The example may be easier to understand if you copy it to a blank worksheet.

How to copy an example

Create a blank workbook or worksheet.
Select the example in the Help topic.
Note Do not select the row or column headers.



Selecting an example from Help

Press CTRL+C.
In the worksheet, select cell A1, and press CTRL+V.
To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Tools menu, point to Formula Auditing, and then click Formula Auditing Mode.

1
2
3
4
A B
Formula Description (Result)
=HEX2DEC("A5") Converts hexadecimal A5 to decimal (165)
=HEX2DEC("FFFFFFFF5B") Converts hexadecimal FFFFFFFF5B to decimal (-165)
=HEX2DEC("3DA408B9") Converts hexadecimal 3DA408B9 to decimal (1034160313)
__________________
One of the reasons I love Excel so much is every time I think I have it figured out someone shows me a better/more efficient/ cleaner or faster method to do what I have been doing the long way for years....


http://www.rentacoder.com/RentACoder...RL=AId_7206185
Makrini is offline   Reply With Quote
Old Mar 24th, 2009, 06:40 AM   #3
Makrini
 
Join Date: May 2007
Location: Brisbane, Australia
Posts: 1,034
Default Re: VBA Hex-to-Dec

Oops sorry - I missed the part where you said VBA

&H10

will return 16 (Simply prefix with &)
__________________
One of the reasons I love Excel so much is every time I think I have it figured out someone shows me a better/more efficient/ cleaner or faster method to do what I have been doing the long way for years....


http://www.rentacoder.com/RentACoder...RL=AId_7206185
Makrini is offline   Reply With Quote
Old Mar 24th, 2009, 07:00 AM   #4
Jaymond Flurrie
 
Join Date: Sep 2008
Posts: 289
Default Re: VBA Hex-to-Dec

Quote:
Originally Posted by Makrini View Post
HEX2DEC
Show All
Hide All
Converts a hexadecimal number to decimal.

If this function is not available, and returns the #NAME? error, install and load the Analysis ToolPak add-in.

How?

On the Tools menu, click Add-Ins.
In the Add-Ins available list, select the Analysis ToolPak box, and then click OK.
If necessary, follow the instructions in the setup program.
Syntax

HEX2DEC(number)

Number is the hexadecimal number you want to convert. Number cannot contain more than 10 characters (40 bits). The most significant bit of number is the sign bit. The remaining 39 bits are magnitude bits. Negative numbers are represented using two's-complement notation.

Remark

If number is not a valid hexadecimal number, HEX2DEC returns the #NUM! error value.

Example

The example may be easier to understand if you copy it to a blank worksheet.

How to copy an example

Create a blank workbook or worksheet.
Select the example in the Help topic.
Note Do not select the row or column headers.



Selecting an example from Help

Press CTRL+C.
In the worksheet, select cell A1, and press CTRL+V.
To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Tools menu, point to Formula Auditing, and then click Formula Auditing Mode.

1
2
3
4
A B
Formula Description (Result)
=HEX2DEC("A5") Converts hexadecimal A5 to decimal (165)
=HEX2DEC("FFFFFFFF5B") Converts hexadecimal FFFFFFFF5B to decimal (-165)
=HEX2DEC("3DA408B9") Converts hexadecimal 3DA408B9 to decimal (1034160313)
Yes, I was aware of this, but it seems to have nothing to do with VBA. Thank you for the answer anyway!
Jaymond Flurrie is offline   Reply With Quote
Old Mar 24th, 2009, 07:03 AM   #5
Jaymond Flurrie
 
Join Date: Sep 2008
Posts: 289
Default Re: VBA Hex-to-Dec

Quote:
Originally Posted by Makrini View Post
Oops sorry - I missed the part where you said VBA

&H10

will return 16 (Simply prefix with &)
Does it work with variables too? I have a number in Mid$(strCrypted, l, 4), which returns a four digit hexadecimal number (well, a string of four numbers , but shouldn't matter), which I try to turn into a normal number, so that I can pass it to ChrW(), so that it will return a character.

One option would be, of course, to use a worksheet function, but I thought that there has to be some very simple function, like that Hex$() is.

Last edited by Jaymond Flurrie; Mar 24th, 2009 at 07:05 AM.
Jaymond Flurrie is offline   Reply With Quote
Old Mar 24th, 2009, 07:17 AM   #6
Makrini
 
Join Date: May 2007
Location: Brisbane, Australia
Posts: 1,034
Default Re: VBA Hex-to-Dec

CLng("&H" & Range("A1").Value)
__________________
One of the reasons I love Excel so much is every time I think I have it figured out someone shows me a better/more efficient/ cleaner or faster method to do what I have been doing the long way for years....


http://www.rentacoder.com/RentACoder...RL=AId_7206185
Makrini is offline   Reply With Quote
Old Mar 24th, 2009, 07:22 AM   #7
Jaymond Flurrie
 
Join Date: Sep 2008
Posts: 289
Default Re: VBA Hex-to-Dec

Quote:
Originally Posted by Makrini View Post
CLng("&H" & Range("A1").Value)
Works great! Exactly that kind of solution I was looking for. Thank you!
Jaymond Flurrie is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 05:28 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2010 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes