Electronic Design

  
Reprints     Printer-Friendly    Email this Article    RSS        Font Size     What's This?


[Ideas For Design]
VC++ Program Converts Hex Strings To Integers

Zhe Lou, Xing-Zhi Qiu  |   ED Online ID #1779  |   December 9, 2002


Binary numbers are seldom implemented in Visual C++ applications because C++ is considered a "high-level" language. Yet in communications systems, where information is transferred between two different devices through a network, using binary or hexadecimal numbers is much easier than decimal numbers.

In some applications, the computer's user interface will guide the user to input a hexadecimal number via the keyboard. Because this number will be transferred to an embedded system, it must be converted to a "real" number before leaving the PC. In this case, the hexadecimal-number data type in-putted by the user is string. Unfortunately, Visual C++ 6.0 doesn't offer a conversion function that can transform a string (representing a hexadecimal number) into a real hexadecimal or binary number.

The program to execute such a function implements the flow chart shown in the figure. You must first translate the inputted hexadecimal string into a binary string. Because a hexadecimal character represents a 4-bit binary number, it's better to employ a "switch case" structure to convert a hexadecimal character to a 4-bit binary string. Function convert_hex2bin() carries out this conversion.

After converting the whole hexadecimal string into a binary string, you must translate every 4-bit binary string into an integer that represents the real value of the 4-bit binary number. Function convert_sbin2int() returns this integer value. In this function, the binary string is transformed into a real integer by subfunction atoi(), which is included in the VC++ IDE. This "binary" integer is then converted into a decimal. Finally, all of the small integers are accumulated to form the final result. The resulting real number can be sent to an embedded system or be displayed on the screen as a decimal number.

To download a listing, click Download the Code at the top of the page.


Reprints   Printer-Friendly  Email this Article  RSS    Font Size   What's This?


  • C Tools Accelerate HDV Development On Xilinx FPGAs
  • A New Design Inflection Point
  • Forecasting Industry Growth For 2009 And Beyond
  • EDA Retools To Exploit Multicore Architectures
  • Design And Verification Move Up In Abstraction
  • EDA Retools To Exploit Multicore Architectures
  • A New Design Inflection Point
  • Design And Verification Move Up In Abstraction
    1) Transportation Guidelines For Lithium Batteries Get Updated
    (1263 views today)
    2) Build A Smart Battery Charger Using A Single-Transistor Circuit
    (285 views today)
    3) WHITE PAPER: Liquid-Level Monitoring Using a Pressure Sensor
    (224 views today)
    4) 1-A Switching Regulators Operate With 96% Efficiency To Replace Linear Regulators
    (148 views today)
    5) The Field Of Energy Harvesting Begins To Ripen
    (109 views today)
    ALL TOP 20



    Reader Comments

    i m lookin' for text to audio output circuit or software and also telephone ring detector...

    sathesh -August 31, 2008

    Hi

    Am looking for a program which converts a long integer to hex.

    Please provide kind guidence and send me the code.

    Thanks & regards Sajid

    Syed Sajid -July 21, 2008

    Hi

    Am looking for a program which converts a long integer to hex.

    Please provide kind guidence and send me the code.

    Thanks & regards Sajid

    Syed Sajid -July 21, 2008

    www.mercyhouse.tk

    Anonymous -June 19, 2008

    sir/mam, Iam in need of a vc++ program for my mini project.Topic is airline Reservation.pls send me airline reservation coding as soon as possible.

    justin -March 08, 2008

    Was unable to find anything to do this for me. I was going to write this myself. You saved me some time, bud. Thank you.

    Anonymous -September 12, 2007

    Please send me a program for decimal to hexadecimal conversion in c/c++.

    shiv -August 05, 2007   (Article Rating: )

    Please send me a program for decimal to hexadecimal conversion in c/c++.

    Anonymous -August 05, 2007

    Please send me a program for decimal to hexadecimal conversion

    shivkumar -August 05, 2007   (Article Rating: )

    Please send me a program for decimal to hexadecimal conversion.

    Ashay Pogade -August 01, 2007

    Please send me a program for decimal to hexadecimal conversion.

    Ashay Pogade -August 01, 2007

    PLEASE SEND THE PROGRAM TO CONVERT INTEGER INTO HEXADECIMAL IN EMBEDDED VC++

    veeresh -February 27, 2007

    The following does the hex string to integer conversion albeit with the overhead of stdio...

    #include <stdio.h>

    int hexToInt( const char* hexStr ) { int hexInt; sscanf( hexStr, "%x", &hexInt ); return hexInt; }

    int main( int argc, char* argv[] ) { char* tokenString1 = "0x0056"; char* tokenString2 = "56"; char* tokenString3 = "0056";

    printf( "Integer: = %d (%s)\n", hexToInt( tokenString1 ), tokenString1 ); printf( "Integer: = %d (%s)\n", hexToInt( tokenString2 ), tokenString2 ); printf( "Integer: = %d (%s)\n", hexToInt( tokenString3 ), tokenString3 );

    return 0; }

    Anonymous -June 21, 2006

    BLEH with your damn CString. ANSI Standardize

    Anonymous -May 01, 2006

    Create a program that converts a binary number into its equivalent decimal number. The input must be composed of only 0’s and 1’s (i.e. a binary number). The program must display the binary input and the decimal equivalent. The user must be able to repeat the process if he/she wants to. (Hint: Use the division and modulus operators to separate the number into its individual digits.)

    Can anyone help me with this problem...

    Anonymous -March 21, 2006

    strtol

    Anonymous -January 08, 2006

    Plz send me the code to convert an int into a character string in VC++

    rohini sharma -November 09, 2005

    Can any one tell me how to convert a int in to a string

    rohini sharma -November 09, 2005

    Please send code of real-time calendar program code in VC++ or any nice project program code in VC++, html and java script.

    Anonymous -March 21, 2005

    It is very common to start hexadecimal string with “0x” therefore, convert_hex2bin should have additional case: case 'X': case 'x': bin = "0000"; break;

    Nikolai -April 30, 2004

    Instead of doing: case 'e': case 'E': bin = "1110"; break;

    I think you could convert the hex string to uppercase first, and save a few lines of code.

    Vitaliy M -March 07, 2004

    This program is great....However, the return type is still CString....How can we have a return type of int?

    AUTHOR RESPONSE:

    You can make a small change of the code to get the return type of int.

    To see how to implement this, download the file 1779.zip, which now includes the new code listing as the file named 1779more.txt.

    sri -July 24, 2003

    POST YOUR COMMENTS HERE
    Name:

    Email:
    Your Comments:

    Enter the text from the image below


    Please refresh the page if you have trouble reading this text.

    Search Electronic Design
         
      
     
    Web Seminar
    Sponsored By:
    Title: Read Pacing: A Performance Enhancing Feature of PCI Express Gen 2 Switch Devices
    Speakers: 
    Date: 07/01/08
    Register: 

    Electronic Design Europe Electronic Design China EEPN Power Electronics Auto Electronics Microwaves & RF
    Mobile Dev & Design Schematics Find Power Products Military Electronics EE Events Related Resources