简体   繁体   English

将十六进制拆分为 2 个十六进制值

[英]Spliting an hex into 2 hex values

I have a problem I have an Hexadecimal like 0x6002 and I have to split it into 0x60 and 0x02 .我有一个问题,我有一个像0x6002这样的十六进制,我必须把它分成0x600x02 how do I do that?我怎么做?

I progam in cpp and have to communicate with a network port.我在 cpp 中编程并且必须与网络端口通信。 For that I have an address which I have to split in the middle my issue is that I store the hex in a Uint8 wich converts the number to a decimal.为此,我有一个地址,我必须在中间拆分我的问题是我将十六进制存储在Uint8将数字转换为十进制。 how can i solve my problem?我该如何解决我的问题?

You can use masking to extract the low byte and a shift to extract the high byte:您可以使用掩码来提取低字节并使用移位来提取高字节:

uint16_t a = 0x6002;
uint8_t a0 = a & 0xff;  // a0 = 0x02
uint8_t a1 = a >> 8;    // a1 = 0x60

Note that the & 0xff is not strictly necessary for a narrowing unsigned conversion such as that above, but there are other cases where it might be necessary (eg when signed conversion is involved, or when the destination type is wider than 8 bits), so I'll leave it in for illustrative purposes.请注意, & 0xff对于像上面那样的缩小无符号转换并不是绝对必要的,但在其他情况下可能是必要的(例如,当涉及有符号转换时,或者当目标类型宽于 8 位时),所以出于说明目的,我将保留它。

uint16_t port = 0x6002;
uint8_t high = (port >> 8) & 0xFF;
uint8_t low = port & 0xFF;

Pointers are MUCH FASTER than shifts and require no processor math.指针比移位快得多,并且不需要处理器数学。 You create your 2 byte variable and use Pointers to change each byte separately.您创建 2 字节变量并使用指针分别更改每个字节。

Here is an example:下面是一个例子:

uint16_t myInt; // 2 byte variable

uint8_t *LowByte = (uint8_t*)myInt; // Point LowByte to the same memory address as
              // myInt, but as a uint8 (1 byte) instead of a uint16 (2 bytes)
              // Note that low bytes come first in memory


uint8_t *HighByte = (uint8_t*)myInt + 1; // 1 byte offset from myInt start
// which works the same way as:
uint8_t *HighByte = LowByte + 1; // 1 byte offset from LowByte

In some compilers the pointer syntax is a little different (such as Microsoft C++ ):在某些编译器中,指针语法略有不同(例如Microsoft C++ ):

uint16_t myInt; // 2 byte variable

uint8_t *LowByte = (uint8_t*)&myInt; // Point LowByte to the same memory address as
              // myInt, but as a uint8 (1 byte) instead of a uint16 (2 bytes)
              // Note that low bytes come first in memory

uint8_t *HighByte = (uint8_t*)&myInt + 1; // 1 byte offset from myInt start

The * char in type definition indicates you are creating a pointer, not a variable.类型定义中的* char 表示您正在创建一个指针,而不是一个变量。 The pointer points to a memory address instead of it´s value.指针指向内存地址而不是它的值。 To write or read the value of the variable pointed to, you add * to the variable name.要写入或读取指向的变量的值,请将*添加到变量名称。

So, to manipulate the full 2 bytes together, or separately, here are some examples:因此,要一起或单独操作完整的 2 个字节,以下是一些示例:

myInt = 0x1234; // write 0x1234 to full int

*LowByte = 0x34; // write 0x34 to low byte

*HighByte = 0x12; // write 0x12 to high byte


uint8_t x = *LowByte; // read low byte

uint8_t y = *HighByte; // read high byte

uint16_t z = myInt; // reads full int

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

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