简体   繁体   English

以十六进制表示位字符串的最佳实践(Arduino)

[英]Best practice to represent bit string in hex (Arduino)

I have a string (or cstring) consisted of 24 bits "000101011110011110101110" that should be represented in hex (0x15e7ae). 我有一个由24位“ 000101011110011110101110”组成的字符串(或cstring),应以十六进制(0x15e7ae)表示。

As i understand bit string needs to be splitted on to 6 parts by 4 bits "0001 0101 1110 0111 1010 1110" and then each part converted to hex 据我了解,位串需要通过4位“ 0001 0101 1110 0111 1010 1110”分成6部分,然后将每个部分转换为十六进制

0001 -> 1
0101 -> 5
1110 -> e
0111 -> 7
1010 -> a
1110 -> e

So what are the simplest and cost effective ways to convert it to hex representation: 0x15e7ae? 那么,将其转换为十六进制表示形式的最简单,最具成本效益的方法是什么?0x15e7ae?

There is also dilemma for me which string type is better to use String or char[]. 对于我来说,使用String或char []哪种字符串类型更好也是一个难题。 String can be easily splitted using substring function, but i don't know how to convert string type to hex. 可以使用子字符串函数轻松拆分字符串,但是我不知道如何将字符串类型转换为十六进制。

And contrariwise char[] can be easily converted to hex using strtoul function but i didn't find simple way to split char string. 相反,可以使用strtoul函数将char []轻松转换为十六进制,但是我没有找到拆分char字符串的简单方法。

This is quite literally found in this link: StringConstructors 可以在以下链接中找到它: StringConstructors

 // using an int and a base (hexadecimal):
 stringOne =  String(45, HEX);   
 // prints "2d", which is the hexadecimal version of decimal 45:
 Serial.println(stringOne);   
const char* binary = "000101011110011110101110" ;
char hex[9] = "" ;

uint32_t integer = 0 ;

for( int i = 0; binary[i+1] != '\0'; i++ )
{
    if( binary[i] == '1' )
    {
        integer |= 1 ;
    }
    integer <<= 1 ;
}

sprintf( hex, "0x%06x", integer ) ;

Let's try some simple bit shifting. 让我们尝试一些简单的移位。

std::string sample_str = "000101011110011110101110";
uint32_t result = 0;
for (unsigned int i = 0; i < sample_str.length(); ++i)
{
  result = result << 1;
  result = result | (sample_str[i] & 1);
}

There may be faster methods, but you would have to search the web for "bit twiddling string". 可能有更快的方法,但是您必须在网络上搜索“位旋转字符串”。

Background 背景
This is based on the assumption that the character representation of zero has the least significant bit set to zero. 这是基于以下假设:零字符表示的最低有效位设置为零。 Likewise the character representation of one has the least significant bit set to one. 同样一个的字符表示有至少显著位设置为1。

The algorithm shifts the result left by one to make room for a new bit value. 该算法将结果左移一位,以便为新的位值腾出空间。 Taking the character value and ANDing with 1 results in a value of zero for '0' and one for '1'. 取字符值并与1进行“与”运算,结果'0'的值为零,'1'的值为1。 This result is ORed into the result value, to produce the correct value. 此结果与result值进行或运算,以生成正确的值。

Try single stepping with a debugger to see how it works. 尝试与调试器一起单步执行以了解其工作原理。

In C, this is quite simple. 在C语言中,这非常简单。 Use strtoumax(binary, 0, 2) to convert your binary string to an uintmax_t and then convert that to a hex string with sprintf or fprintf . 使用strtoumax(binary, 0, 2)将您的二进制字符串转换为uintmax_t ,然后使用sprintffprintf将其转换为十六进制字符串。

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

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