Javatpoint Logo
Javatpoint Logo

C++ Manipulator setbase

C++ manipulator setbase is used to convert the basefield of one numeric value into another base. Possible values are:

dec: decimal base (base = 10)

hex: hexadecimal base (base = 16)

oct: octal base (base = 8)

zero: If base is any other value.

In addition to the base conversion the setbase manipulator is also used to define the base of the numeral value of a variable.

The original default of stream input and output is dec.

Syntax

Parameter

base: Numerical radix to be used as given below:

8 - oct

10- dec

16- hex

Any other - resetiosflags(ios_base:: basefield)

Return value

Return Value

This function returns an object of unspecified type. setbase function should only be used as a stream manipulator.

Data Races

The stream object on which it is inserted or extracted is modified and concurrent access to the same stream object may introduce data races.

Exceptions

Object is in a valid state, if any exception is thrown.

Example 1

Let's see the simple example to demonstrate the use of setbase:

Output:

6e

Example 2

Let's see another simple example:

Output:

Parsing string "10 0x10 010"
hexadecimal parse: 16 16 16
prefix-dependent parse: 10 16 8
hex output: 0xa 0x10 0x8

Example 3

Let's see another simple example to show the base of a numeric value of a variable using hex, oct, dec manipulator:

Output:

Enter number 11
Decimal base = 11
Hexadecimal base = b
Octal base = 13

Example 4

Let's see another simple example to show the base of a numeric value of a variable using setbase manipulator function:

Output:

Enter number 11
Decimal base = 11
Hexadecimal base = b
Octal base = 13
Next TopicC++ Manipulator





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA