简体   繁体   English

在 c 中给出 rgb(1,2,3) 时,rgb 到十六进制返回错误值

[英]rgb to hex return wrong value when giving rgb(1,2,3) in c

i am still a beginner with c and am writing a converter RGB to HEX for "code wars" challenge which would for eg return 010203 when i give rgb(1,2,3) but it returns 030303 i have checked my converting logic and its correct (i guess)我仍然是 c 的初学者,并且正在为“代码大战”挑战编写一个 RGB 到 HEX 的转换器,例如当我给rgb(1,2,3)时返回010203但它返回030303我检查了我的转换逻辑及其正确(我猜)
this is my code:这是我的代码:

#include <string.h>
#include <stdio.h>

char* convert(int num){
    int rem;
    char deg[17]="0123456789ABCDEF";
    static char ret[3];
    rem=num%16;
    num=num/16;
    ret[0]=deg[num];
    ret[1]=deg[rem];
    return ret;
}

int rgb(int r, int g, int b, char *output)
{
    snprintf(output,sizeof(output),"%s%s%s",convert(r),convert(g),convert(b));
    return 0; 
}

i have tried also replacing snpritf by strcat but then insted of returning 00FF7D it return 37D when giving rgb(0,255,125)我也尝试过用strcat替换snpritf但随后返回00FF7D它在给rgb(0,255,125)时返回 37D
this is how replaced snpritf by strcat :这就是用strcat替换snpritf的方式:

  strcat(output,convert(r));
  strcat(output,convert(g));
  strcat(output,convert(b));

i am really confused any help is appreciated.我真的很困惑任何帮助表示赞赏。

Because you return the address of a static array of char, your three calls to convert() all return the same address.因为您返回一个static char 数组的地址,所以您对convert()的三个调用都返回相同的地址。

Each call overwrites the array then replaces what was done by the previous calls.每次调用都会覆盖数组,然后替换之前调用所做的事情。

You should create three distinct arrays in rgb() and pass one of each as a parameter to each of the calls to convert() .您应该在rgb()中创建三个不同的 arrays 并将每个中的一个作为参数传递给对convert()的每个调用。

Another solution is to simplify a bit as below.另一种解决方案是简化如下。 Because we know that convert() must write two characters, simple pointer arithmetic with step 2 can do the trick.因为我们知道convert()必须写入两个字符,所以使用第 2 步的简单指针运算就可以解决问题。 By the way, don't forget the string termination ( \0 ) in convert() (with your static array it was implicit, but now you have to explicitly initialise with 0).顺便说一句,不要忘记convert()中的字符串终止符( \0 )(对于您的 static 数组,它是隐式的,但现在您必须使用 0 显式初始化)。

/**
  gcc -std=c99 -o prog_c prog_c.c \
      -pedantic -Wall -Wextra -Wconversion \
      -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla \
      -g -O0 -UNDEBUG -fsanitize=address,undefined
**/

#include <stdio.h>

void
convert(int num,
        char *result)
{
  const char *deg="0123456789ABCDEF";
  result[0]=deg[num/16];
  result[1]=deg[num%16];
  result[2]='\0';
}

int
rgb(int r, int g, int b,
    char *output, size_t capacity)
{
  if(capacity<7)
  {
    return -1;
  }
  convert(r, output+0);
  convert(g, output+2);
  convert(b, output+4);
  return 0; 
}

int
main(void)
{
  char txt[20];
  rgb(1, 2, 3, txt, sizeof(txt));
  printf("%s\n", txt);
  return 0;
}

Your second attempt was almost right, but you need to use strcpy() for the first string, unless output is initialized to an empty string.您的第二次尝试几乎是正确的,但是您需要将strcpy()用于第一个字符串,除非output被初始化为空字符串。

void rgb(int r, int g, int b, char *output)
{
    strcpy(output,convert(r));
    strcat(output,convert(g));
    strcat(output,convert(b));
    return; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM