cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive a 32 bit value in HEX Format in STM32H750VB processor?

nanotronicsnss
Associate II

I am doing AES Encryption of the given data in a processor and transferring it to another processor where the original data is obtained by decryption. From the help from our community member(@Community member​ i am able to obtain the output.. Thanks for your support. Now whenever i am transferring the 128 bit encrypted data using (HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X", Decryptedtext[0]), 20);) this function data can be transferred.But at the receiver side the array is received a string array and from that array i cannot get the corresponding 32 bit hexadecimal values.SO for decryption i cannot obtain the correct value.. so kindly give suggestions so that i can proceed with my work...

For example: I am representing as 32 bit

sent data=0x12ab3456

received data= 12ab3456( as a string) and also i am receiving the data byte by byte and on applying shift operation for combining the values in array changes and decryption cant be done..

But the required output is 0x12ab3456 (as hexadecimal) to be stored in a 32 bit array...

9 REPLIES 9
Ozone
Lead

> received data= 12ab3456( as a string) ...

So, why not using the string functions of the standard C library ?

Like strtol() for instance, which can be parametrized to accept only hexadecimal.

Even writing you own decoder isn't really difficult.

nanotronicsnss
Associate II

Received data is received in bytes and it is stored in an array and total number of characters received is 32 digits for example

"2ABCD1349876543212345678AC23DF45" stored in receiver array rxBuff..i.e., rxBuf[1]=2 rxBuf[2]=A etc... where rxBuf is char uint8_t each digit representing 8 bits

Next i have to segregate these digits into 32 bit array each a having 8 digits i.e., Hexarray[0]=0x2ABCD134; ..where Hexarray is uint32_t each digit representing 4 bits

so for the above datatype conversion how can we apply strtol() ....

I am new to STM 32 programming ..so kindly guide me to develop my own decoder...

Thank you

Received data is received in bytes and it is stored in an array and total number of characters received is 32 digits for example

"2ABCD1349876543212345678AC23DF45" stored in receiver array rxBuff..i.e., rxBuf[1]=2 rxBuf[2]=A etc... where rxBuf is char uint8_t each digit representing 8 bits

Next i have to segregate these digits into 32 bit array each a having 8 digits i.e., Hexarray[0]=0x2ABCD134; ..where Hexarray is uint32_t each digit representing 4 bits

so for the above datatype conversion how can we apply strtol() ....

I am new to STM 32 programming ..so kindly guide me to develop my own decoder...

Thank you

Write a function which decode one character into the four-bit nibble - if it's digit, subtract '0'; if it's a character between 'a' and 'f' subtract 'a' and add 10, if between 'A' to 'F' subtract 'A' and add 10.

Then, for the required number of characters, repeatedly call this function for individual characters, shift an accumulating variable to the left by 4 bits and add the result of character decoding to it.

This has NOTHING to do with STM32, this is basic programming.

JW

Figure out how to manage data representation, this has little to do with STM32, or advanced topics like encryption.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

If you can reconstruct the bytes on the PC side you can send the 32-bit value as 4-bytes

HAL_UART_Transmit(&huart4, (uint8_t*)&Decryptedtext[0], 4, 100);

Or just the whole array as bytes

HAL_UART_Transmit(&huart4, (uint8_t*)Decryptedtext, sizeof(Decryptedtext), 500);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I am here to ask for ur help.. i am having individual data received as characters each of length 8 bits... 2ABCD1349876543212345678AC23DF45

  1. First i need to separate the data into array of length each having 8 digits(64 bits) 2ABCD134
  2. Second i need to convert the array into hex value having 8 digits(32 bits) 0x2ABCD134

I am not in need of separating a single 8 bit char into two 4 bit nibbles .....

as my level of understanding goes and based on the lines used in my program

i)For TRANSMITTING 128 bit DATA from processor I

{

HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[0]), 20);

HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[1]), 20);

HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[2]), 20);

HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[3]), 20);

}

ii) RECEIVING DATA using Interrupt at processor II

{

void UART4_IRQHandler(void)

{

 /* USER CODE BEGIN UART4_IRQn 0 */

 /* USER CODE END UART4_IRQn 0 */

 HAL_UART_IRQHandler(&huart4);

 /* USER CODE BEGIN UART4_IRQn 1 */

HAL_UART_Receive_IT(&huart4,&data,1);

 //inc++;

if(data=='*'){receive_bit=1;inc=0;}

 

if(receive_bit==1){rxBuf[inc]=data;inc+=1;} //rxBuf id 8 bit char array

if(data=='#'){receive_bit=2;}

 /* USER CODE END UART4_IRQn 1 */

}

}

iii) for decrypting received data at processor II

{

HAL_CRYP_Decrypt(&hcryp,Encryptedtext,16,Decryptedtext,100); // where Encrypted text is 32 bit is unsigned int which must be formed from rx_Buf

which is my problem

}

Is my understanding about the problem ok or is my understanding of the problem is wrong?...

TDK
Guru

Lots of hits for this in Google:

https://stackoverflow.com/questions/10156409/convert-hex-string-char-to-int

Seems silly to convert bytes to a string just to convert that string back to bytes, especially if you can't manage the last part.

If you feel a post has answered your question, please click "Accept as Solution".
Piranha
Chief II

The only safe way to convert strings to integers with C standard libraries is described there:

https://wiki.sei.cmu.edu/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number

But of course using some simple binary protocol is much more effective and simpler.