【问题标题】:Unsigned Char bit access, to determine if char consist of 1s and 0s无符号字符位访问,以确定字符是否由 1 和 0 组成
【发布时间】:2012-02-28 13:37:07
【问题描述】:

嘿,我需要一些关于无符号字符位访问的帮助。我需要测试以确保作为无符号字符(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;
    }
}
}

【问题讨论】:

  • 请不要使用goto。你可以重写和if (strlen(channel) == 6) break;一样的东西
  • 你真的想在每次循环中都覆盖你的输入吗?
  • 是的,我必须提示 6 次进入我在主函数中调用该函数 6 次

标签: c char unsigned


【解决方案1】:

循环检查每个数字。

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

您也可以只使用strtol(3) 并检查endptr 中的结果,看看您是否得到一个6 位数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 2011-11-04
    相关资源
    最近更新 更多