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?



Reader Comments

i need sample sourse code vc++ please s.o.s

behzad -June 04, 2009

i am need sourse cod vc++ please

taimaz -June 04, 2009

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