簡體   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