How can I make my C++ app convert an integer number to a string? How can I put an Int into the Text property of a component? What is the IntToStr method and what does it do? Can we use printf() method with int in modern C++? Let’s answer these questions.
Table of Contents
What is the difference in a C++ app between a String and a Unicode string
In a Modern C++ app, we use the Unicode string format, Visual components of C++ Builder have a lot of text properties which are String but this actually means they are a UnicodeString. Unicode strings allow your C++ app to store a much wider variety of strings suitable for the modern world of internet connections, emoticons and the need to display special characters such as trademarks, copyright symbols and similar non alphanumeric characters. Generally, if you want to display an integer or float number nowadays you must convert your number value to a text value, usually to a String. C++ Builder has a lot of specific methods in its libraries, and the VCL and FMX libraries have a lot of specific methods that can be used easily in your C++ applications. We can easily convert int, unsigned int, long int, and long long int integer numbers to a modern Unicode string with IntToStr, IntToStrF or printf method of UnicodeStrings in modern C++.
What are Integer Numbers in a C++ app?
The Fundamental Data Types are listed officially here. Char, short, int, and long, together with their unsigned variants, are all considered integer data types. The following table shows the integer type specifiers, with synonyms listed on the same line. Here are the Fundamental Integer Data Types,
char, signed char | Synonyms if default char set to signed. |
unsigned char | |
char, unsigned char | Synonyms if default char set to unsigned. |
signed char | |
int, signed int | |
unsigned, unsigned int | |
short, short int, signed short int | |
unsigned short, unsigned short int | |
long, long int, signed long int | |
unsigned long, unsigned long int | |
signed long long, long long int (ISO C99, C++11) | |
unsigned long long, unsigned long long int (ISO C99, C++11) |
The Extended Integer Data Types are listed here. Here is the list of Extended Integer Data Types used in C++
Type | Suffix in constants | Example | Storage |
---|---|---|---|
__int8 | i8 | __int8 c = 127i8; | 8 bits |
unsigned __int8 | ui8 | unsigned __int8 c = 240ui8; | 8 bits |
__int16 | i16 | __int16 s = 32767i16; | 16 bits |
unsigned __int16 | ui16 | unsigned __int16 s = 64532ui16; | 16 bits |
__int32 | i32 | __int32 i = 123456789i32; | 32 bits |
unsigned __int32 | ui32 | unsigned __int32 i = 223456789ui32; | 32 bits |
__int64 | i64 | __int64 big = 12345654321i64; | 64 bits |
unsigned __int64 | ui64 | unsigned __int64 hugeInt = 1234567887654321ui64; | 64 bits |
If you wonder differences between 32bit and 64bit data types, 64bit Windows Data Types are compared with 32bit Windows Data Types are compared here.
How can we get our C++ app to Convert an integer number to a String
Simply we can use IntToStr() method to convert a floating number to a string. Here is the syntax of IntoStr() method,
Syntax:
Is there a simple C++ example of using the IntToStr() method?
Simply we can convert a an integer number to a string as below,
in the 64bit applications we can use this method with Int64 data types.
in the latest RAD Studio versions this is same as below,
IntToStr method can be directly used to set Text properties (which are UnicodeString) of a component. For example we can put integer number to Text property of Edit (TEdit) component as below,
Here is a full 64bit C++ example of using the IntToStr() method in a C++ app
We can use FloatStr method in our VCL and FMX applications. Here is a C++ builder FMX example,
The C++ printf() function is the answer to all sorts of questions
In C and C++ programming language, the printf() function, is a printing function with a formatted text form to stdout and variables are listed as other parameters of this function. printf functions writes the string pointed by format to the standard an output, generally it is stdout. It’s formatted text form is formatted with format specifiers ( with %), and the other arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.
printf() function is defined in <stdio.h> library, and in std:: namespace. Simplified syntax of this function can be displayed as,
or in a more generic syntax,
printf Format Specifiers and scanf Format Specifiers are explained well in DocWiki of Embarcadero. All Format Specifiers together are listed and explained well here with examples.
Let’s give some basic examples to most used data types,
You can use Printf() in your C++ app to display Integer numbers
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.