Unsigned Char 位访问,确定 char 是否由 1 和 0 组成

发布于 2025-01-03 07:25:19 字数 405 浏览 3 评论 0原文

嘿,我需要一些有关无符号字符位访问的帮助。我需要测试以确保“通道”(无符号字符(UCHAR))的长度为 6 位(我有),并且输入的数字实际上是二进制数(又名 1 或 0)。我不知道如何访问它。任何帮助都会很棒!

void binEnter(void *channel){
int i;
for (i=0; i<6; i++) {
redo:
    printf("Enter binary value for Channel %d: ",i);
    scanf("%s",(UCHAR *)channel);
    if (strlen(channel)!=6) {
        printf("Error entry must be six digits!\n");
        goto redo;
    }
}
}

Hey I need a little help with unsigned char bit access. I need to test to ensure that "channel" which is an unsigned char (UCHAR) is 6 digits long (i have that) and that the the number enter is infact a binary number (aka 1s or 0s). I at a loss for how to access it. Any help would be great!

void binEnter(void *channel){
int i;
for (i=0; i<6; i++) {
redo:
    printf("Enter binary value for Channel %d: ",i);
    scanf("%s",(UCHAR *)channel);
    if (strlen(channel)!=6) {
        printf("Error entry must be six digits!\n");
        goto redo;
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2025-01-10 07:25:19

做一个循环来检查每个数字。

char *string = channel;
for (j = 0; j < 6; j++)
{
  if ((string[j] != '0') && (string[j] != '1'))
  {
    // error
  }
}

您也可以使用 strtol(3) 并检查结果 endptr 来查看是否有 6 位数字。

Make a loop to check each digit.

char *string = channel;
for (j = 0; j < 6; j++)
{
  if ((string[j] != '0') && (string[j] != '1'))
  {
    // error
  }
}

You could also just use strtol(3) and check the result in endptr to see if you got a 6-digit number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文