繁体   English   中英

如何在Arduino上的C / C ++中将HexBytes数组转换为字符串?

[英]How to convert a HexBytes Array to a String in C/C++ on Arduino?

我意识到将String转换为hexarray现在需要将新数组转换为新字符串,因为函数Sha256.write需要一个char,这是这样吗?

    char hexstring[] = "020000009ecb752aac3e6d2101163b36f3e6bd67d0c95be402918f2f00000000000000001036e4ee6f31bc9a690053320286d84fbfe4e5ee0594b4ab72339366b3ff1734270061536c89001900000000";
    int i;
    int n;

    uint8_t  bytearray[80];
    Serial.println("Starting...");
    char tmp[3];
    tmp[2] = '\0';
    int j = 0;

    //GET ARRAY
    for(i=0;i<strlen(hexstring);i+=2) {
        tmp[0] = hexstring[i];
        tmp[1] = hexstring[i+1];
        bytearray[j] = strtol(tmp,0,16);
        j+=1;
     }

    for(i=0;i<80;i+=1) {
       Serial.println( bytearray[i]);
     }


      int _batchSize;
      unsigned char hash[32];
      SHA256_CTX ctx;
      int idx;
      Serial.println("SHA256...");
      for(_batchSize = 100000; _batchSize > 0; _batchSize--){
         bytearray[76] = nonce;
          // Sha256.write(bytearray);
         sha256_init(&ctx);
         sha256_update(&ctx,bytearray,80);
         sha256_final(&ctx,hash); //
         sha256_init(&ctx);
         sha256_update(&ctx,hash,32);
         sha256_final(&ctx,hash); //are this corrent? i'm need in bytes too        
       //  print_hash(hash);
         int zeroBytes = 0;
         for (int i = 31; i >= 28; i--, zeroBytes++)
             if(hash[i] > 0)
                break;
             if(zeroBytes == 4){ // SOLUTION TRUE, NOW I'M NEED THIS IN STRING
                printf("0x");
                for (n = 0; n < 32; n++)
                   Serial.println(printf("%02x", hash[n]));  //ERROR :(       
              }
         //increase
         if(++nonce == 4294967295)
            nonce = 0;

      }      

  }


}   

串行端口上的输出阵列:

2
0
0
0
158
203
117
42
172
62
109
33
1
22
59
54
243
230
189
103
208
201
91
228
2
145
143
47
0
0
0
0
0
0
0
0
16
54
228
238
111
49
188
154
105
0
83
50
2
134
216
79
191
228
229
238
5
148
180
171
114
51
147
102
179
255
23
52
39
0
97
83
108
137
0
25
0
0
0
0

如何将其转换为十六进制字符回来?

更新

这个解决方案对我有用,谢谢大家!

void printHash(uint8_t* hash) {
  int id;
  for (id=0; id<32; id++) {
    Serial.print("0123456789abcdef"[hash[id]>>4]);
    Serial.print("0123456789abcdef"[hash[id]&0xf]);
  }
  Serial.println();
}

跳到 底部的“ 寻址代码... ”部分 以获取最相关的内容
(这里的东西几乎不是有用的blither)

您的功能目的:

Sha256.write((char *)bytearray);

我相信是要向正在运行的哈希写入更多数据。 由此 )因此,在您的问题中,我不确定如何将其转换为十六进制字符char? 这与您使用它的方式有何关系。

为了说明如何将整数数组返回为“十六进制字符串”形式, 我提供另一种方法

从这里

这是一个代码片段,它将计算字符串“ abc”的摘要

   SHA256_CTX ctx;
   u_int8_t results[SHA256_DIGEST_LENGTH];
   char *buf;
   int n;

   buf = "abc";
   n = strlen(buf);
   SHA256_Init(&ctx);
   SHA256_Update(&ctx, (u_int8_t *)buf, n);
   SHA256_Final(results, &ctx);

   /* Print the digest as one long hex value */
   printf("0x");
   for (n = 0; n < SHA256_DIGEST_LENGTH; n++)
           printf("%02x", results[n]);
   putchar('\n'); 

导致:

"0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".   

在此示例中,我相信您想要的数组包含在u_int8_t results

您的帖子中没有足够的描述,以确保这将对您有所帮助,请在评论中告诉我,我将尝试解决其他问题。

编辑后添加

继续上面的示例 ,要将results的数组内容放回字符串中,可以执行以下操作:

char *newString;
newString = malloc(sizeof(char)*SHA256_DIGEST_LENGTH*2);
memset(newString, 0, sizeof(char)*SHA256_DIGEST_LENGTH*2);
strcat(newString, "0x");
for(i=0;i<SHA256_DIGEST_LENGTH;i++)
{
    sprintf(newString, "%s%02x\n", newString, results[i]);
}
//use newString for stuff...
free(newString);  

解决您的代码,直接提出问题:

您的代码块:

  for(_batchSize = 100000; _batchSize > 0; _batchSize--){
     bytearray[76] = _batchSize;
     Sha256.write((char *)bytearray); //here are the error
  }    

如果您要做的就是int数组转换为“十六进制字符串”,则没有必要

您的int数组,定义为:

int bytearray[80];  

如您在最新编辑中所说明的,此时已包含所有必需的值。 如果要将此数据返回为“十六进制字符串”形式,则将为您完成此操作:(将result替换为bytearray

char *newString;
newString = malloc(sizeof(char)*SHA256_DIGEST_LENGTH*2);//if these defines are not in your environment
memset(newString, 0, sizeof(char)*SHA256_DIGEST_LENGTH*2);//then replace them with appropriate value for length
strcat(newString, "0x");
for(i=0;i<sizeof(bytearray)/sizeof(bytearray[0]);i++)
{
    sprintf(newString, "%s%02x\n", newString, bytearray[i]);
}
//use newString for stuff...
free(newString);  

暂无
暂无

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

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