CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Hex to Bin

  1. #1
    Guest

    Hex to Bin

    Hi,
    I need to convert hex to binary. If u know any functions in MFC or other please reply.

    Thanks


  2. #2
    Join Date
    May 1999
    Posts
    44

    Re: Hex to Bin

    Hi!

    Hope this little code sample will carry you on....

    void main( int argc, char** argv )
    {
    char *hex = argv[1];
    char *bin = (char *)calloc(1, sizeof(char));
    char *pr = (char *)calloc(32, sizeof(char));

    while(int i = 0 < (int)strlen(hex))
    {
    int itmp = 0;
    char ctmp = hex[i];
    if(ctmp == 'A') itmp = 10;
    else if(ctmp == 'B') itmp = 11;
    else if(ctmp == 'C') itmp = 12;
    else if(ctmp == 'D') itmp = 13;
    else if(ctmp == 'E') itmp = 14;
    else if(ctmp == 'F') itmp = 15;
    else if(ctmp == '9') itmp = 9;
    else if(ctmp == '8') itmp = 8;
    else if(ctmp == '7') itmp = 7;
    else if(ctmp == '6') itmp = 6;
    else if(ctmp == '5') itmp = 5;
    else if(ctmp == '4') itmp = 4;
    else if(ctmp == '3') itmp = 3;
    else if(ctmp == '2') itmp = 2;
    else if(ctmp == '1') itmp = 1;
    else if(ctmp == '0') itmp = 0;

    int ii = 1;
    for(; ii <=4; bin = strcat(&bin[i], ((itmp & 1) == 1) ? "1" : "0"), itmp >>= 1, ii++ );
    ii = 0 + (i*4);
    for(int r = strlen(bin)-1; r>=0; pr[ii++] = bin[r], r--);
    i++;
    }
    free (hex); free (bin); free (pr);
    cout << pr;
    }

    apartis AG http://www.apartis.de
    Normen Mueller

  3. #3
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Hex to Bin



    CString CSomeClass::SomeFunction(const CString& instr)
    {
    CString outStr;
    const int len = instr.GetLength();
    for (int i = 0; i < len; ++len )
    {
    CString tempStr;
    switch (outStr[i])
    {
    case '0' : tempStr = "0000"; break;
    case '1' : tempStr = "0001"; break;
    case '2' : tempStr = "0010"; break;
    case '3' : tempStr = "0011"; break;
    case '4' : tempStr = "0100"; break;
    case '5' : tempStr = "0101"; break;
    case '6' : tempStr = "0110"; break;
    case '7' : tempStr = "0111"; break;
    case '8' : tempStr = "1000"; break;
    case '9' : tempStr = "1001"; break;
    case 'a' :
    case 'A' : tempStr = "1010"; break;
    case 'b' :
    case 'B' : tempStr = "1011"; break;
    case 'c' :
    case 'C' : tempStr = "1100"; break;
    case 'd' :
    case 'D' : tempStr = "1101"; break;
    case 'e' :
    case 'E' : tempStr = "1110"; break;
    case 'f' :
    case 'F' : tempStr = "1111"; break;
    }
    outStr += tempStr;
    }
    return outStr;
    }






  4. #4
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Hex to Bin


    CString CSomeClass::SomeFunction(const CString& instr)
    {
    CString outStr;
    const int len = instr.GetLength();
    for (int i = 0; i < len; ++len )
    {
    int num = 0;
    const char ch = instr.GetAt(i);
    if (ch >= '0' && ch <= '9')
    num = ch - '0';
    else if (ch >= 'A' && ch <= 'F')
    num = ch - 'A' + 10;
    else if (ch >= 'a' && ch <= 'f')
    num = ch - 'a' + 10;
    char buffer[256];
    const CString tempStr(::_ltoa(num, buffer, 2));
    outStr += tempStr;
    }
    return outStr;
    }





  5. #5
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Hex to Bin

    ---
    CString strHexDigits("0123456789ABCDEF");
    CString strBinaryNibbles[16] = {"0000","0001","0010","0011",
    "0100","0101","0110","0111",
    "1000","1001","1010","1011",
    "1100","1101","1110","1111"};

    CString HexToBinary(CString strHex)
    {
    // This assumes the supplied string contains no prefix, only raw hex digits.

    char cDigit ;
    CString strBinary("");
    int n, iLength, iIndex ;

    strHex.MakeUpper();
    iLength = strHex.GetLength();
    for (n = 0 ; n < iLength ; n++)
    {
    cDigit = strHex[n];
    iIndex = strHexDigits.Find(cDigit);
    if (iIndex == -1)
    {
    strBinary = "Illegal hex digit (";
    strBinary += cDigit ;
    strBinary += ").";
    break ;
    }
    else
    strBinary += strBinaryNibbles[iIndex];
    }

    return strBinary ;
    }

    CString BinaryToHex(CString strBinary)
    {
    // This assumes the supplied string contains no prefix, only raw binary digits.

    CString strHex(""), strNibble ;
    int n, iLength, iTargetLength, iIndex ;

    iLength = strBinary.GetLength();
    // Pad to the left to make a multiple of four digits.
    iTargetLength = ( (iLength + 3) & (~0x0003) );
    while (iLength < iTargetLength)
    {
    strBinary = "0" + strBinary ;
    iLength++ ;
    }

    for (n = 0 ; n < iLength ; n += 4)
    {
    strNibble = strBinary.Mid(n, 4);
    for (iIndex = 0 ; iIndex < 16 ; iIndex++)
    {
    if (strBinaryNibbles[iIndex] == strNibble)
    break ;
    }

    if (iIndex == 16) // Loop ran to the end.
    {
    strHex = "Illegal binary nibble (" + strNibble + ").";
    break ;
    }
    else
    strHex += strHexDigits[iIndex];
    }

    return strHex ;
    }


    ---

    How's that?



    --
    Jason Teagle
    [email protected]

  6. #6
    Join Date
    May 1999
    Posts
    44

    Re: Hex to Bin

    Sorry, but I think that does not work.

    const int len = instr.GetLength(); -> l-value specifies const object
    switch (outStr) -> switch expression of type 'class CString' is illegal
    and further on, what value does the variable outStr has at the beginning?

    The idea itself is quite good and much more efficient than my one, but I did not want to use the MFC.

    Thanx for your comment
    Normen

    --
    apartis AG http://www.apartis.de
    Normen Mueller

  7. #7
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Hex to Bin


    There are many ways....
    Let me comment on the comments.



    const int len = instr.GetLength(); // nothing wrong here, I am just telling all that I do not intend to change len
    switch(outStr) // is a BAD typing mistake, and should have been switch(instr.GetAt(i)) . I appologise
    CString outstr; // calls the default constructor, whch initialises outstr to an empty string, power uf OO





    Thanks for your comments...

    Sally


  8. #8
    Join Date
    May 1999
    Posts
    44

    Re: Hex to Bin

    OK, you are right, but

    // nothing wrong here, I am just telling all that I do not intend to change len
    const int len = instr.GetLength(); // What's about your for-statement?
    // is a BAD typing mistake, and should have been switch(instr.GetAt(i)) . I appologise
    switch(outStr) // I also thought, that it was just a typing error
    // calls the default constructor, whch initialises outstr to an empty string, power uf OO
    CString outstr; // Ups...




    --
    apartis AG http://www.apartis.de
    Normen Mueller

  9. #9
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Hex to Bin




    const int len = instr.GetLength();
    for (int i = 0; i < len; ++len )

    // in the for statement:
    // 'i' is the 'stear'-variable and can not be const for obvious reasons
    // 'len' is part of the end test condition, and can be a const expression

    // I could have written it all this way:

    for (int i = 0; i < instr.GetLength(); ++len )

    // here, you can see that the end test condition is a rather constant value





    Thanks for nickpicking on my code, I appreciate all the help I can get
    and if something is wrong, I really like to know... Again, thanks

    Sally


  10. #10
    Join Date
    May 1999
    Posts
    44

    Re: Hex to Bin

    You are welcome and I hope we'll stay in contact for further questions.
    May be you could send me your email address as a part of a private message, because I think it is very good to have many persons to turn to relating to developers stuff...

    Bye
    Normen

    --
    apartis AG http://www.apartis.de
    Normen Mueller

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured