简体   繁体   English

如何将u32_t中的此十六进制值转换为其对应的char / ascii值?

[英]How do I convert this hex value in a u32_t to its corresponding char/ascii value?

I'm trying to read data from a TCP stack and using the data to control external interfaces etc. The data is in u32_t so if I write for example "test" to the interface the hex value corresponds to 0x74657374. 我正在尝试从TCP堆栈读取数据并使用数据来控制外部接口等。数据在u32_t中,因此如果我将“test”写入接口,则十六进制值对应于0x74657374。 I want to convert this data to their corresponding characters so it is easier to use the data. 我想将此数据转换为相应的字符,以便更容易使用数据。 How do I convert hexadecimal values in a u32_t to its char string? 如何将u32_t中的十六进制值转换为其char字符串?

I've tried print the data directly via the %c format specifier but then it only shows the first character of the string. 我试过直接通过%c格式说明符打印数据但是它只显示字符串的第一个字符。

/* indicate that the packet has been received */
tcp_recved(tpcb, p->len);
// Put actual data in 32 bit unsigned integer.
tempPtr = (u32_t*)p->payload;
// Print length of the actual data.
xil_printf("Received package. Length = %d \r\n", p->len);
// Reverse the data so it corresponds to the data sent.
u32_t reversedTemp = byte_reverse_32(*tempPtr);

// Prints hex value of data
xil_printf("Data: %08x \r\n",reversedTemp);

if (reversedTemp == 0x6C656431) { /* Read "led1" */
    xil_printf("Data Read: led1");
} else if (reversedTemp == 0x74657374) { /* Read "test */
    xil_printf("Data Read: test");
}

So where I'm using the full hex value in the if statement I only want to use the string value. 所以我在if语句中使用完整的十六进制值我只想使用字符串值。 So for checking test it should be == "test" instead of == 0x74657374. 因此,对于检查测试,它应该是==“test”而不是== 0x74657374。

The easiest solution is to get the address of the variable then cast that to a char* . 最简单的解决方案是获取变量的地址,然后将其转换为char*

if (memcmp("test", tempPtr), 4) {
    // The input was "test"
}

You might run into issues related to endianness . 您可能会遇到与字节序相关的问题。 This answer has a soltion to that. 这个答案有一个解决方案。

You have a bug here: tempPtr = (u32_t*)p->payload; 你有一个错误: tempPtr = (u32_t*)p->payload; ... byte_reverse_32(*tempPtr) . ... byte_reverse_32(*tempPtr) This is a strict aliasing violation and quite likely also a misaligned access. 这是严格的别名违规,很可能也是一个错位的访问。 Don't do this. 不要这样做。

There's no apparent need to swap the bytes around to compensate for endianess since you intend to convert them to a string anyway(?). 由于您打算将它们转换为字符串(?),因此没有明显需要交换字节来补偿字节顺序。 If so, you can do this instead (assuming network big endian, CPU little endian): 如果是这样,你可以这样做(假设网络大端,CPU小端):

tcp_recved(tpcb, p->len);
char str[4+1] =
{
  [0] = p->payload[3];
  [1] = p->payload[2];
  [2] = p->payload[1];
  [3] = p->payload[0];
  [4] = '\0'
};

Given : 鉴于:

uint32_t num = 0x74657374 ;

Then: 然后:

char str[5] = {0} ;
str[0] = num >> 24 ;
str[1] = (num >> 16) & 0xff ;
str[2] = (num >> 8) & 0xff ;
str[3] = num & 0xff ;

The byte reversal may be unnecessary, you can simply reverse the unpacking to get the string in one step when the first character is in the LSB: 字节反转可能是不必要的,当第一个字符在LSB中时,您可以简单地反转解包以一步获取字符串:

char str[5] = {0} ;
str[0] = num & 0xff ;
str[1] = (num >> 8) & 0xff ;
str[2] = (num >> 16) & 0xff ;
str[3] = num >> 24 ;

For the specific cases of either: 对于以下任一情况的具体情况:

  • a big-endian target and the first character in the MSB, or 一个大端目标和MSB中的第一个字符,或
  • a little-endian target and the first character in the LSB, 一个小端目标和LSB中的第一个字符,

then just: 然后就是:

char str[5] = {0} ;
memcpy( str, &num, 4 ) ;

All that said in this case your existing solution is more efficient than string or memory compare. 在这种情况下,所有这些说明您现有的解决方案比字符串或内存比较更有效。 But again the byte reversal is unnecessary - just reverse the byte order in the integer, and avoid the "magic numbers": 但是,字节反转再次是不必要的 - 只需反转整数中的字节顺序,并避免“幻数”:

#define TEST 0x74736574u ;  // "tset" ("test" reversed)

...

uint32_t temp = *((u32_t*)p->payload) ;
if( temp == TEST ) ...

The string conversion perhaps remains useful is you want to output the strings for debug or human presentation. 字符串转换可能仍然有用,您希望输出字符串以进行调试或人工演示。

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

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