Analysis of C ++ formatted string-related applications

Source: Internet
Author: User
Tags 04x

String operations in the C ++ programming language are a widely used basic technology. We will introduce the implementation methods of C ++ formatted strings in detail here, and hope you can get some help in understanding string operations.

When constructing various types of data into strings, the powerful features of sprintf seldom disappoint you. Since sprintf and printf have almost the same usage, but the printing destination is different, the former is printed into the string, and the latter is directly output on the command line. This also makes sprintf much more useful than printf.

Sprintf is a variable parameter function, which is defined as follows:

 
 
  1. int sprintf( char *buffer, const char *format 
    [, argument] ... ); 

In addition to the fixed types of the first two parameters, you can take over multiple parameters later. Its essence is obviously on the second parameter: C ++ formatted string.

  • Introduction to unsuitable use of C ++ inline functions
  • Detailed description of the use of the C ++ comma Operator
  • C ++ inheritance Basic Concepts
  • C ++ Enumeration type usage and definition
  • Analysis of C ++ enumerative subcorrelation types

Both printf and sprintf use a C ++ formatted string to specify the string format, and some format specifiers starting with "%" are used inside the format string to occupy a location, the variable is provided in the variable parameter list, and the function will replace the specifier with the variable at the corresponding position to generate the string that the caller wants.

One of the most common applications of sprintf is to print Integers to strings. Therefore, spritnf can replace itoa in most cases.

For example:

 
 
  1. // Print the integer 123 into a string and save it in s.
  2. Sprintf (s, "% d", 123); // generate "123", you can specify the width, left fill space:
  3. Sprintf (s, "% 8d % 8d", 123,456 7); // generate: "123 4567", of course, it can be left aligned:
  4. Sprintf (s, "%-8d % 8d", 123,456 7); // generate: "123 4567"

You can also print the data in hexadecimal format:
 

 
 
  1. Sprintf (s, "% 8x", 4567); // lowercase hexadecimal notation, with 8 width positions and right alignment
  2. Sprintf (s, "%-8X", 4568); // in hexadecimal notation, the width occupies 8 positions and is left aligned.

In this way, the hexadecimal string of an integer is easy to obtain, but when printing the hexadecimal content, we usually want an equal-width format with 0 on the left, what should we do? Simply add 0 to the number that represents the width.

 
 
  1. Sprintf (s, "% 08X", 4567); // generate: "201711d7"

You can also use this left-side 0 fill method to print the 10-in-hexadecimal format in "% d.

Pay attention to a symbol extension problem: for example, if we want to print a short integer short)-1 memory hexadecimal representation, on the Win32 platform, A short type occupies 2 bytes, So we naturally want to print it with 4 hexadecimal numbers:

 
 
  1. short si = -1;  
  2. sprintf(s, "%04X", si); 

Production student "FFFFFFFF", what's going on? Because spritnf is a Variable Parameter Function, except the first two parameters, the following parameters are not of type security, there is no way for a function to know whether a 4-byte integer or a 2-byte short integer is pressed in the parameter stack before the function call through a "% X "., therefore, a 4-byte processing method is adopted, which leads to the symbol extension during parameter pressure stack, which is a 32-bit integer-1. When printing, the four locations are insufficient, print out the 8-bit 16 hexadecimal values of 32-bit integer-1.

If you want to see the original form of si, you should let the compiler perform 0 extension instead of 0 on the left of the binary instead of the sign bit during symbol extension ):

 
 
  1. sprintf(s, "%04X", (unsigned short)si); 

You can. Or:

 
 
  1. unsigned short si = -1;  
  2. sprintf(s, "%04X", si);  

Sprintf and printf can also print integer strings in octal format, using "% o ". Note that both the octal and hexadecimal formats do not print negative numbers. They are all unsigned. In fact, they are the direct hexadecimal or octal representation of the variable's internal code.

The specific application method of C ++ to format strings is described here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.