SÃO PAULO, Brazil —Banco Bradesco of Brazil has agreed to buy all of HSBC’s operations in the country for about 17.6 billion reais, or $5.2 billion, the companies said.

The price was based on HSBC Brazil’s shareholder equity of 11.6 billion reais at the end of last year, and it will be adjusted in line with changes in that figure between now and the deal’s closing, Bradesco said on Sunday.

The acquisition had been rumored for weeks, but the price was significantly higher than the 12 billion reais often mentioned in the Brazilian news media.

Brazil’s banking sector is proving resilient even as the economy faces a recession, high inflation and a weakening currency. Bradesco announced last week that it earned 4.5 billion reais in the second quarter, its best quarterly results ever.

HSBC has been present in Brazil since 1997. It expanded further in the country in 2003, when it bought Lloyds’ Brazil unit, but it did not manage to make a success of its operations here.

The bank lost $247 million in Brazil last year, even as several of its larger rivals in the country, including Bradesco, reported record profits.

“HSBC has been having problems with consumer debt,” said Andre Riva Gargiulo, a banking sector analyst for GBM, a brokerage firm, in São Paulo. They’ve had high default rates.”

Mr. Gargiulo also said that scale was crucial in Brazil’s retail banking sector, where HSBC had been trying to compete. HSBC’s local operations were the country’s sixth largest, as measured by assets, but it was less than a third the size of the fifth largest, Santander’s Brazil unit.

It was also less than a fifth the size of Bradesco, the country’s fourth-largest bank.

Bradesco’s statement on Sunday cited the “gain in scale” from the deal.

“Bradesco will take over all operations of HSBC in Brazil, including retail, insurance and asset management, as well as all the branches and clients,” the statement said.

HSBC had announced in June that it would sell its operations in Brazil as part of a wider restructuring plan to cut costs, reduce risk and increase its focus on Asia.

“We announced at our investor update on June 9 that we were targeting a series of actions to generate increased value for shareholders,” Stuart T. Gulliver, the chief executive of HSBC,said in a statement on Monday. “I am pleased to be able to announce today a transaction which achieves both a solid financial outcome and swift delivery of one of our stated actions.”

HSBC, which is based in Britain but generates more than half of its earnings in Asia, plans to shed as many as 50,000 jobs and sell several underperforming businesses as part of the overhaul.

HSBC said it planned to maintain a smaller presence in Brazil to “serve large corporate clients with respect to their international needs.”

The deal depends on regulators’ approval. Brazil’s antitrust authorities have generally viewed mergers in the financial sector favorably, so the acquisition is not expected to pose problems.

Bradesco’s investment banking subsidiary, Bradesco BBI, advised its parent on the deal, along with JP Morgan and N. M. Rothschild & Sons. HSBC was advised by its own investment banking operation, HSBC Global Banking and Markets, and Goldman Sachs.