简体   繁体   中英

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

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

As Eric and Eugene has said in the comments, there is no way to convert a hexidecimal number to a decimal number . 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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