Results 1 to 6 of 6

Thread: Dec ----to----> Hex <<urgent>>

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    in e middle of nowhere..
    Posts
    40

    Talking Dec ----to----> Hex <<urgent>>

    hey guyz!!!

    doez anybody know how to change the value of a variable to Hexadecimal from decimal???
    in VC++.......

    thanks a lot!!!!
    i need it urgently,,,,,,,,,,
    pls ,,,,,,,
    HELP!!!!

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Of an integer? Integers are represented as binary in memory, it only depends on you formatting how it is outputted:

    printf("%x", i);
    or
    cout << setbase(16) << i;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    in e middle of nowhere..
    Posts
    40
    thanks,,,,,, cornedbee,,

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    in e middle of nowhere..
    Posts
    40
    hey thanks a lot cornedbee =-
    but how to pack with 0's(zeros) in front???

    =o)

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    in e middle of nowhere..
    Posts
    40
    hey nvm,,,
    i tried "%04x",, it works.....
    anyway thanksss,,,,,,

  6. #6
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Use the first function, the 2 last functions will be use by the first one.

    Code:
    //Use this one
    string AsciiToHex(const string &sAll)
    {
    	string sHex;
    	string sTempo;
    	char cEach;
    
    	for (int i = 0 ; i < sAll.length() ; i++)
    	{
    		cEach = sAll.at(i);
    		sTempo = DecToHex((int)cEach);
    		sHex.append(sTempo);
    		sHex.append(" ");
    	}//End For
    
    	return sHex;
    }//End AsciiToHex
    //Do not use this one
    string DecToHex(const int &iDecimal)
    {
    	int i1;
    	char c[1];
    	char c2[1];
    	string sHexa;
    
    	i1 = abs(iDecimal / 16);
    	itoa(i1,c,10);
    	i1 = abs(iDecimal%16);
    	itoa(i1,c2,10);
    	if (i1>9)
    		c2[0] = ConvertH(i1);
    
    	sHexa.append(c);
    	sHexa.append(c2);
    	return (sHexa.substr(0,2));
    }//End DecToHex
    
    //Not this one either
    char ConvertH(const int &iNumber)
    {
    	switch(iNumber)
    	{
    	case 10:return 'A';break;
    	case 11:return 'B';break;
    	case 12:return 'C';break;
    	case 13:return 'D';break;
    	case 14:return 'E';break;
    	case 15:return 'F';break;
    	default:return 'X';break;
    	}//End switch
    	return 'X';
    }//End ConvertH

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