简体   繁体   English

如何使用C语言将十六进制数组转换为十进制

[英]How to convert hexadecimal array to decimal using C language

Hello I want to know how to convert a hexadecimal array to a decimal here's my block 您好我想知道如何将十六进制数组转换为十进制这是我的block

uint8_t block[8] = {0xb,0xb,0xb,0xa,0xa,0xa,0xa,0xa}; here's my code 这是我的代码

#include "stdint.h"

void main() {

   uint8_t block[8] = {0xb,0xb,0xb,0xa,0xa,0xa,0xa,0xa};

   uint8_t key[16]  = {0xa,0xa,0xa,0xb,0xb,0xb,0xb,0xb,
                       0xa,0xa,0xa,0xa,0xa,0xa,0xa,0xa};

   UART1_Init(9600);  // Initialisation de l’UART1 à 9600 bps
   UART1_Write_Text("message:");
   UART1_Write_Text(block);
   UART1_Write_Text(" TEA Encryption:");
   TEA_Enc(block, key);
   UART1_Write_Text(block);

   UART1_Write_Text("TEA Decryption:");
   TEA_Dec(block, key);
   UART1_Write_Text(block);
} 

For example if i display block i want to have as result like 123456 thank you in advance 例如,如果我显示block我希望得到像123456这样的结果,谢谢

As Eric and Eugene has said in the comments, there is no way to convert a hexidecimal number to a decimal number . 正如Eric和Eugene在评论中所说, 无法将十六进制数转换为十进制数 Hexidecimal or decimal only exist in the code you write. 十六进制或十进制仅存在于您编写的代码中。 Once a compiler sees a number it will store it in memory in it's own representation. 一旦编译器看到一个数字,它将以其自己的表示形式将其存储在内存中。

Here's an example of how you can write numbers in different representations in your code. 这是一个示例,说明如何在代码中以不同的表示形式写数字。 All the numbers I write in the following code are the same. 我在以下代码中写的所有数字都相同。 The program does not change, no matter which one you use. 该程序不会更改,无论您使用哪个。

int x = 0x10;
// stored in memory as:
// |0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|

int x = 16;
// stored in memory as:
// |0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|

int x = 0b00000000000000000000000000010000;
// stored in memory as:
// |0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|

int x = 020;
// stored in memory as:
// |0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|

So for your example you block can be represented in either of these ways 因此,在您的示例中,您可以使用以下两种方式来代表您的block

uint8_t block_hex[8] = {0xb,0xb,0xb,0xa,0xa,0xa,0xa,0xa};
uint8_t block_dec[8] = {11,11,11,10,10,10,10,10};

Changing between hexidecimal and decimal will not change the behavior of your program. 在十六进制和十进制之间进行更改不会更改程序的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM