将char数组转换为字符串[和Pebble]

瓦苏

我有一个char数组,我试图将其转换为指向字符串的char指针。我相信这涉及到将指针指向char数组的第一个元素,并在char数组的末尾添加一个空字符。这样做的原因是,我试图将其传递给SimpleMenuItem卵石智能手表的,.title需要在其中获取char*,它指向一个字符串。

虽然我已经能够填充char数组,并且(我认为)添加了空字符并获得了指针,但我仍无法在卵石上看到标题。我不确定这是卵石问题还是我的C理解问题,但是我感到很可能是前者。

卵石代码(C):

void in_received_handler(DictionaryIterator *received, void *context) {
    dataReceived = dict_read_first(received);
    APP_LOG(APP_LOG_LEVEL_DEBUG, "read first");

    while (dataReceived != NULL){

        if (dataReceived->key == 0x20) {

            //original is of the format "# random string", i.e. "4 test name"
            char* original = dataReceived->value->cstring;
            APP_LOG(APP_LOG_LEVEL_DEBUG, original);

            char originalArray[strlen(original)];
            //copy over to originalArray
            for (unsigned int i = 0; i < strlen(original); i++) {
                originalArray[i] = original[i];
            }

            char* keysplit = strtok(originalArray, " ");
            APP_LOG(APP_LOG_LEVEL_DEBUG, keysplit);

            //key
            int key = atoi(keysplit);
            APP_LOG(APP_LOG_LEVEL_DEBUG, "Int Key: %d", key);

            //good until here
            char remainderArray[sizeof(originalArray)-strlen(keysplit) +1];

            //assign rest of string to new array
            for (unsigned int i = 1; i < sizeof(remainderArray)-1; i++){
                APP_LOG(APP_LOG_LEVEL_DEBUG, "Character             : %c", originalArray[i+strlen(keysplit)]);
                remainderArray[i] = originalArray[i+strlen(keysplit)];
                APP_LOG(APP_LOG_LEVEL_DEBUG, "Character in new Array: %c", remainderArray[i]);
            }

            remainderArray[sizeof(remainderArray)-1] = '\0';

            //data is sucesfully placed into remainderArray
            char* ptr = remainderArray;
            strncpy(ptr, remainderArray, sizeof(remainderArray)+1);
            ptr[sizeof(remainderArray)+1] = '\0';

            chats[key] = (SimpleMenuItem){
                // You should give each menu item a title and callback
                .title = &remainderArray[0],
                .callback = selected_chat,
            };

        }

        dataReceived = dict_read_next(received);
        APP_LOG(APP_LOG_LEVEL_DEBUG, "read again");

    }

    layer_mark_dirty((Layer *)voice_chats);
}

如果有人对为什么小卵石不显示分配给它的数据有任何建议.title,我希望听到他们的建议。

谢谢!

萨尔法塔

解决方案实际上比这简单得多。

首先,请注意,char数组和指向字符串的指针实际上是相同的。它们都是指向第一个字节地址的指针。在这两种情况下,系统函数都将搜索空字符以标识字符串的结尾(strlen,strcpy,printf等)。char*并且char[]可以互换。

当您在中接收数据时in_received_handler(),您获得的指针(dataReceived->value->cstring)指向蓝牙接收缓冲区,您需要将该字符串复制到其他位置。这样,每次需要重绘屏幕时,这些字符都将可用。

由于要获取的项目数量是动态的,因此必须使用来动态分配内存malloc()您应该记得稍后再分配内存(使用free())。

这将编译并应执行您想要的操作:

void in_received_handler(DictionaryIterator *received, void *context) {
  Tuple *dataReceived = dict_read_first(received);

  while (dataReceived != NULL){
    if (dataReceived->key == 0x20) {
      //original is of the format "# random string", i.e. "4 test name"
      char* original = dataReceived->value->cstring;
      APP_LOG(APP_LOG_LEVEL_DEBUG, original);

      char* keysplit = strtok(original, " ");
      APP_LOG(APP_LOG_LEVEL_DEBUG, keysplit);

      //key
      int key = atoi(keysplit);
      APP_LOG(APP_LOG_LEVEL_DEBUG, "Int Key: %d", key);

      // This will return the second part of the string.
      char *message = strtok(NULL, " ");
      // Allocate some memory to hold a copy of that message
      char *copyOfMessage = malloc(strlen(message));
      // And copy the message from the bluetooth buffer to the new memory
      strcpy(copyOfMessage, message);
      APP_LOG(APP_LOG_LEVEL_DEBUG, "Message: %d", key);

      chats[key] = (SimpleMenuItem){
        // You should give each menu item a title and callback
        .title = copyOfMessage,
        .callback = selected_chat,
      };
    }

    dataReceived = dict_read_next(received);
  }
  layer_mark_dirty((Layer *)voice_chats);
}

顺便说一句,我建议不要使用另一个带有键和消息的字符串,并在客户端的C语言中对其进行解析,而建议您使用另一个应用程序消息键索引来传输您的“键”。例如,您可能有:

{
  0x1: 33,                 // the key
  0x20: "message"           // the actual message
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章