C++Introduction to C++Language FeatureLearn C++

How To Convert An Integer To A String In A C++ App

How To Convert An Integer To A String In A C++ App

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.

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. Charshortint, 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 charSynonyms if default char set to signed.
unsigned char
char, unsigned charSynonyms 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++

TypeSuffix in constantsExampleStorage
__int8i8__int8 c = 127i8;8 bits
unsigned __int8ui8unsigned __int8 c = 240ui8;8 bits
__int16i16__int16 s = 32767i16;16 bits
unsigned __int16ui16unsigned __int16 s = 64532ui16;16 bits
__int32i32__int32 i = 123456789i32;32 bits
unsigned __int32ui32unsigned __int32 i = 223456789ui32;32 bits
__int64i64__int64 big = 12345654321i64;64 bits
unsigned __int64ui64unsigned __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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

How To Use WString find_first_of() In A Modern C++ App

C++Learn C++

How To Read An XML File In A C Program Or C++ App

C++Learn C++

Solve A 3 Diagonal Matrix Using Successive Over Relaxation

C++C++11C++14C++17Introduction to C++Learn C++

How To Make Use Of Wide String Properties In C++ Software