Hexadecimal conversion

本文详细介绍了C++中常用的进制转换方法,包括strtol函数如何将不同进制的字符串转换为10进制整数,itoa函数如何将10进制整数转换为指定进制的字符串,以及标准库提供的std::bitset、std::oct、std::dec和std::hex等转换方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

strtol 函数:

它的功能是将一个任意1-36进制数转化为10进制数,返回是long int型。

函数为long int strtol(const char *nptr, char **endptr, int base)

base是要转化的数的进制,非法字符会赋值给endptr,nptr是要转化的字符,例如:

[cpp]  view plain  copy
  1. char buffer[20]="10379cend$3";  
  2. char *stop;  
  3. printf("%d\n",strtol(buffer, &stop, 8));  
  4. printf("%s\n", stop);  

输出结果:
543
9cend$3

将一个8进制转化为10进制,读取1037,其他后面的为非法字符,转化结果以int型输出来。

另外,如果base为0,且字符串不是以0x(或者0X)开头,则按十进制进行转化。如果base为0或者16,并且字符串以0x(或者0X)开头,那么,x(或者X)被忽略,字符串按16进制转化。如果base不等于0和16,并且字符串以0x(或者0X)开头,那么x被视为非法字符。

最后,需要说明的是,对于nptr指向的字符串,其开头和结尾处的空格被忽视,字符串中间的空格被视为非法字符。


itoa函数:

它的功能是将一个10进制的数转化为n进制的值、其返回值为char型。(和上面的strtol效果相反)

例如:itoa(num, str, 2); num是一个int型的,是要转化的10进制数,str是转化结果,后面的值为目标进制。

[cpp]  view plain  copy
  1. #include<cstdlib>  
  2. #include<cstdio>  
  3. int main()  
  4. {  
  5. int num = 10;  
  6. char str[100];  
  7. itoa(num, str, 2);  
  8. printf("%s\n", str);  
  9. return 0;  
  10. }  

输出结果:

1010


除了上面两个,c++中还有一些定向的转换:

std::bitset(转2进制),std::oct(转8进制),std::dec (转10进制),std::hex(转16进制)。

[cpp]  view plain  copy
  1. #include <bitset>  
  2. #include <iostream>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     cout << "36的8进制:" << std::oct << 36 << endl;  
  7.     cout << "36的10进制" << std::dec << 36 << endl;  
  8.     cout << "36的16进制:" << std::hex << 36 << endl;  
  9.     cout << "36的2进制: " << bitset<8>(36) << endl;  
  10.     return 0;  
  11. }  

(这里bitset后面尖括号里代表输出的位数)



function
<stdlib.h>

itoa

char *  itoa ( int value, char * str, int base );
Convert integer to string (non-standard function)
Converts an integer  value to a null-terminated string using the specified  base and stores the result in the array given by  str parameter.

If  base is 10 and  value is negative, the resulting string is preceded with a minus sign ( -). With any other  basevalue is always considered unsigned.

str should be an array long enough to contain any possible value:  (sizeof(int)*8+1) for  radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.

Parameters

value
Value to be converted to a string.
str
Array in memory where to store the resulting null-terminated string.
base
Numerical base used to represent the  value as a string, between  2 and  36, where  10 means decimal base,  16hexadecimal,  8 octal, and  2 binary.

Return Value

A pointer to the resulting null-terminated string, same as parameter  str.

Portability

This function is  not defined in ANSI-C and is  not part of C++, but is supported by some compilers.

A standard-compliant alternative for some cases may be  sprintf:
  • sprintf(str,"%d",value) converts to decimal base.
  • sprintf(str,"%x",value) converts to hexadecimal base.
  • sprintf(str,"%o",value) converts to octal base.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}


Output:
Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110

See also



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

看个人简介有交流群(付费)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值