Solved

Hexadecimal Data

Posted on 2000-04-14
9
212 Views
Last Modified: 2010-04-10
To convert from hexa to dec, someone (RONSLOW, I think) wrote here some time ago:

int hexa2dec (CString input) {
  char* dummy;
  return strtol(input,&dummy,16);
}

I have used it, and it works as long as the hexa input begins with a number. I mean, I cannot convert the a1e4 value to decimal, for instance.
Why? What can I do?
0
Comment
Question by:javigil1
  • 4
  • 4
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
ID: 2716153
The string must begin with "0x" or "0X" to be considered a HEX number string  (it can have whitespace before the 0.)

continues.
0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 2716184
Here is some code

int getNumFromChar(char c){
 switch(c){
 case '0':return 0;
 case '1':return 1;
 case '2':return 2;
 case '3':return 3;
 case '4':return 4;
 case '5':return 5;
 case '6':return 6;
 case '7':return 7;
 case '8':return 8;
 case '9':return 9;
 case 'A':
 case 'a':return 10;
 case 'B':
 case 'b':return 11;
 case 'C':
 case 'c':return 12;
 case 'D':
 case 'd':return 13;
 case 'E':
 case 'e':return 14;
 case 'F':
 case 'f':return 15;
 }
 return 0;
}

int pow(int base,int exp){
 for(int i=0;i<exp;++i){
  base*=base;
 }
 return base;
}


int hexa2dec(CString input){
 int forw,back,len=input.GetLength();
 int ret=0;

 for(forw=0,back=len-1;back>=0;
                         ++forw,--back){

  ret+=pow(16,forw) *
           getNumFromChar(input[back]);

 } //end for loop

 return ret;
}


I know this is longer than your current solution, but it will work.

Good Luck!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2716208
The bet bet is probalby to do the conversion manually,

int HexStr2Bin(const char *S)
{
   int i = 0;

   while (isspace(*S)) // While in leading spaces.
        ++S; // Skip leading spaces.
   if (*S == '0') // If there is a leading 0.
   {
       ++S; // Skip leading 0.
        if (*S = 'x' || *S = 'X") // If this is a leading 0x, then
           ++S; // Skip the X.
    }
    while (char c = *s++)
    {
       if (!isxdigit(c)) // If not a hex digit, then
           break; // Stop converting.
       i *= 16; // make "room" for new digit.
       if (isdigit(c)) // If this is a digit, then
       {
           i += c - '0'; // add on digit.
       }
        else
       {
          c =  toupper(c);
          i += c - 'A';
       }
    }  
    return i;
}


Let me know if you havne any questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2716235
tvanceplus, you might want to look at the code I wrote.  Its a lot more efficient, and a bit more flexible too.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:javigil1
ID: 2716331
Im afraid your code doesnt work. If I write 0xA11, it translates 11, so it doesnt begin to translate until it finds a number, and it doesnt translate the first characters (no-numbers) anyway.
For instance, 0xaaeca isnt translated at all.
0
 

Author Comment

by:javigil1
ID: 2716355
nietod, my last comment wasnt accurate: 0xaaeca is translated, but bad translated.

Tvanceplus, check your code.
Your programm converts 1(hexa) to 16(decimal)!!
0
 

Author Comment

by:javigil1
ID: 2716408
Adjusted points from 50 to 60
0
 
LVL 22

Expert Comment

by:nietod
ID: 2716653
There were two little mistakes.  The compiler should have reported 1.  The line

     if (*S = 'x' || *S = 'X")

shoiuld be

     if (*S == 'x' || *S == 'X')

and

         i += c - 'A';

should be

         i += c - 'A' + 10;

(Which is the REAL problem).

Note that the procedure is pretty flexible.  The "0x" is optional as are leadig whitespace characters.
0
 

Author Comment

by:javigil1
ID: 2716823
Adjusted points from 60 to 70
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now