Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function to convert ascii to dec,hex, octal or binary?

Status
Not open for further replies.

nbgoku

Programmer
May 25, 2004
108
US
is there a function in c++ or vc++ that converts a ascii to its dec,hex,octal, or binary value?
 
If your platform uses ASCII (which it probably does), then you can just cast the character to an int and display it in dec, hex, or octal format. Binary takes a little more work, but it should be pretty easy.

Example:
Code:
#include <iostream>
#include <iomanip>

int main()
{
    char c = 'A';
    std::cout << "   char: " << c << std::endl;
    std::cout << "decimal: " << static_cast<int>(c) << std::endl;
    std::cout << "  octal: 0" << std::oct << static_cast<int>(c) << std::endl;
    std::cout << "    hex: 0x" << std::hex << static_cast<int>(c) << std::endl;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top