c编程十六进制数据输出格式_使用C编程语言处理十六进制值

c编程十六进制数据输出格式

Hexadecimal value has 16 alphanumeric values from 0 to 9 and A to F, with the base 16. (Read more about Computer number systems), here we will learn how to work with hexadecimal values in c programming language?

十六进制值具有从0到9和A到F的16个字母数字值,以16为底。(阅读有关计算机数字系统的更多信息),这里我们将学习如何在c编程语言中使用十六进制值?

在C编程中十六进制数的表示 (Representation of Hexadecimal numbers in C programming)

In C programming language, a Hexadecimal number is represented by preceding with "0x" or "0X", thus the value in Hexadecimal can be written as "0x64" (which is equivalent to 100 in Decimal).

在C编程语言中,十六进制数以“ 0x”“ 0X ”开头 ,因此十六进制中的值可以写为“ 0x64” (相当于Decimal中的100 )。

在变量中分配十六进制数 (Assigning the Hexadecimal number in a variable)

There is no special type of data type to store Hexadecimal values in C programming, Hexadecimal number is an integer value and you can store it in the integral type of data types (char, short or int).

在C编程中,没有特殊的数据类型来存储十六进制值,十六进制数是整数值 ,您可以将其存储在数据类型的整数类型( char , short或int )中。

Let suppose, we have two values in Hexadecimal "64" (100 in Decimal) and "FAFA" (64250 in Decimal).

假设,我们在十六进制“ 64” (十进制为100)和“ FAFA” (十进制64250 )中具有两个值。

We are storing "64" in an unsigned char variable (64 is small value and can be stored with in a Byte) and "FAFA" in the int variable.

我们将“ 64”存储在一个无符号的char变量中(64是一个很小的值,可以用一个字节存储),而“ FAFA”存储在int变量中。

Consider the following statements

考虑以下语句

unsigned char a=0x64;
unsigned char b=0xFAFA;

以十六进制格式打印数字 (Printing the number in Hexadecimal format)

To print integer number in Hexadecimal format, "%x" or "%X" is used as format specifier in printf() statement.

要以十六进制格式打印整数,请在printf()语句中将“%x”或“%X”用作格式说明符。

"%x" prints the value in Hexadecimal format with alphabets in lowercase (a-f).

“%x”以十六进制格式输出值,并使用小写字母(af)。

"%X" prints the value in Hexadecimal format with alphabets in uppercase (A-F).

“%X”以十六进制格式打印值,并使用大写字母(AF)。

Consider the code, which is printing the values of a and b using both formats

考虑一下代码,该代码使用两种格式打印ab的值

int main()
{
	unsigned char a=0x64;
	int b=0xFAFA;
	
	printf("value of a: %X [%x]\n",a,a);
	printf("value of b: %X [%x]\n",b,b);
	
	return 0;
}

Output

输出量

    value of a: 64 [64] 
    value of b: FAFA [fafa] 

以十六进制格式读取值 (Reading value in Hexadecimal format)

"%x" or "%X" is used with scanf() statement to read the value from the user.

scanf()语句使用“%x”或“%X”从用户读取值。

Consider the following code

考虑以下代码

#include <stdio.h>
int main()
{
	unsigned char a;
	int b;
	
	printf("Enter value of a: ");
	scanf("%x",&a);
	printf("Enter value of b: ");
	scanf("%x",&b);
	
	printf("Value of a: Hex: %X, Decimal: %d\n",a,a);
	printf("Value of b: Hex: %X, Decimal: %d\n",b,b);
	
	return 0;
}


Output

输出量

    Enter value of a: 64
    Enter value of b: FAFA
    Value of a: Hex: 64, Decimal: 100 
    Value of b: Hex: FAFA, Decimal: 64250

通过分配十六进制值来声明整数数组 (Declaring integer array by assigning hexadecimal values)

Consider the following example, where integer array is declaring with the Hexadecimal values and printing in both formats Decimal and Hexadecimal.

考虑下面的示例,其中整数数组声明为十六进制值,并以Decimal和Hexadecimal两种格式打印。

#include <stdio.h>
int main()
{
	int arr[]={0x64, 0xAB0, 0xA0A0, 0xFAFA, 0x100};
	int i;
	
	printf("Array elements are\n");
	for(i=0;i<5;i++)
		printf("Decimal: %d, Hex: %X\n",arr[i],arr[i]);
	
	return 0;
}

Output

输出量

    Array elements are
    Decimal: 100, Hex: 64 
    Decimal: 2736, Hex: AB0 
    Decimal: 41120, Hex: A0A0 
    Decimal: 64250, Hex: FAFA 
    Decimal: 256, Hex: 100

Recommended posts

推荐的帖子

Read more...

阅读更多...

翻译自: https://www.includehelp.com/c/working-with-hexadecimal-values-in-c-programming-language.aspx

c编程十六进制数据输出格式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值