gpt4 book ai didi

c - 如何修剪字符串数组并将其传递到函数中?

转载 作者:行者123 更新时间:2023-11-30 20:28:22 25 4
gpt4 key购买 nike

如何将字符串修剪成 N 个字符的片段,然后将它们作为字符串数组传递到函数中?

这是我程序中转换二进制<->十六进制的部分。

我尝试用字符串做同样的事情,但没有成功。

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <String.h>

#define MAXDIGITS 8 // 8bits


int main()
{
int y;

printf("Binary-Hex convertor\n");
printf("Enter the Binary value : ");
scanf("%d", &y);

int i = MAXDIGITS - 1;
int array[MAXDIGITS];

while(y > 0)
{
array[i--] = y % 10;
y /= 10;
}

printf("%s", "-----------------\n");
printf("%s", "HEX:");

int x = array[0];
int x1 = array[1];
int x2 = array[2];
int x3 = array[3];
int x4 = array[4];
int x5 = array[5];
int x6 = array[6];
int x7 = array[7];

char buffer[50];
char buffer2[50];
char buffer3[50];
}

最佳答案

如果它只是从字符串的二进制到十六进制,那么这会容易得多......

char *input_string = "1001010101001010";
int count = 0;
int value = 0;

while ( *input_string != '\0' )
{
// Might be worth checking for only 0 and 1 in input string
value <<= 1;
value |= (int)((*input_string--) - '0');

if ( ++count == 8 || *input_string == '\0' )
{
// USE value to print etc, if you want to display use
// the following else you could store this in an array etc.
printf("%x ", value);
count = 0;
value = 0;
}
}

关于c - 如何修剪字符串数组并将其传递到函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7862642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com