C for循环中的整数每行增加2,语句与之没有任何关系
我目前在我的C程序中遇到了奇怪的结果,在for循环中,变量hex_索引每行增加2。这与时间无关。尽管如此,它只是每行增加一次,即使语句与它无关 ascii.cC for循环中的整数每行增加2,语句与之没有任何关系,c,function,for-loop,pointers,C,Function,For Loop,Pointers,我目前在我的C程序中遇到了奇怪的结果,在for循环中,变量hex_索引每行增加2。这与时间无关。尽管如此,它只是每行增加一次,即使语句与它无关 ascii.c #include <stdio.h> #include "types.h" int main () { char *hex = "68747541"; char output[4]; hex_to_ascii(hex, output); printf("%s&
#include <stdio.h>
#include "types.h"
int
main ()
{
char *hex = "68747541";
char output[4];
hex_to_ascii(hex, output);
printf("%s", output);
return 0;
}
#包括
#包括“types.h”
int
主要()
{
char*hex=“68747541”;
字符输出[4];
十六进制到ascii(十六进制,输出);
printf(“%s”,输出);
返回0;
}
长度c
#include <stddef.h>
/// Returns the length of the passed string based on checking the content
/// with NULL or @\0 which end a string
int get_str_len(char *string)
{
int len = 0;
for (int i = 0; 1; i++)
{
if (string[i] == '\0' || string[i] == NULL) return len;
len++;
}
}
/// Returns the length of the passed hex string
int get_hex_len(char *hex)
{
return get_str_len(hex);
}
#包括
///根据检查内容返回传递字符串的长度
///以NULL或@\0结尾的字符串
int get_str_len(字符*字符串)
{
int len=0;
对于(int i=0;1;i++)
{
if(string[i]='\0'| | string[i]==NULL)返回len;
len++;
}
}
///返回传递的十六进制字符串的长度
int get_hex_len(字符*hex)
{
返回get_str_len(十六进制);
}
c类
#include "types.h"
#include "length.h"
/// Removes inside a string all control chars and overwrites the passed string
void remove_control_chars(char* string)
{
int len = get_str_len(string);
char new_string[len];
int string_index = 0;
int i;
for (i = 0; i < len; i++, string_index++)
{
if (string[i] < 127 && string[i] > 31)
{
new_string[i] = string[string_index];
}
else string_index++;
}
len = get_str_len(new_string);
for (i = 0; i < len; i++)
{
string[i] = new_string[i];
}
string[i] = '\0';
}
/// Converts an hex number to an int
/// If an invalid character is inside the string it will return -1 (Control chars will be automatically removed)
/// @param str The string that should be converted
int hex_to_int(char *str)
{
remove_control_chars(str);
int str_len = get_str_len(str);
int sum = 0;
int multiplier = 1;
// Calculating the multiplier for the biggest number (num at index 0)
for (int i = 1; i < str_len; i++) multiplier *= 16;
// Adding the multiplied value to the sum and then diving by the base (16)
for (int i = 0; i < str_len; i++, multiplier /= 16)
{
char byte = str[i];
if (byte >= '0' && byte <= '9') sum += (byte - '0') * multiplier;
else if (byte >= 'A' && byte <='F') sum += (byte - 'A' + 10) * multiplier;
else if (byte >= 'a' && byte <='f') sum += (byte - 'a' + 10) * multiplier;
// Avoiding control characters
else if (byte < 31) return sum;
else return -1;
}
return sum;
}
bool valid_hex_char(char byte)
{
return byte >= '0' && byte <= '9' || byte >= 'A' && byte <= 'F' || byte >= 'a' && byte <= 'f';
}
/// Converts an hex number to an dual ascii character and returns it
/// @param hex The string that should be converted
char dual_hex_to_ascii(char *hex)
{
int value = hex_to_int(hex);
return (char) value;
}
/// Converts an hex number to an ascii string
/// @param hex The string that should be converted
/// @param output The initialised string which will be overwritten
void hex_to_ascii(char *hex, char *output)
{
char storage[2];
int len = get_hex_len(hex);
int i = 0;
for (int hex_index = 0; hex_index < len; hex_index += 2)
{
if (valid_hex_char(hex[hex_index]))
{
storage[0] = hex[hex_index];
if (valid_hex_char(hex[hex_index+1]))
{
storage[1] = hex[hex_index+1];
}
else
{
storage[1] = '\0';
}
output[i] = dual_hex_to_ascii(storage);
i++;
}
else
{
return;
}
}
}
#包括“types.h”
#包括“长度.h”
///删除字符串中的所有控制字符并覆盖传递的字符串
作废删除控制字符(字符*字符串)
{
int len=get_str_len(字符串);
字符新_字符串[len];
int string_index=0;
int i;
对于(i=0;i31)
{
新字符串[i]=字符串[string_index];
}
else字符串_index++;
}
len=get_str_len(新字符串);
对于(i=0;i 如果(byte>='0'&&byte='A'&&byte='A'&&byte='0'&&byte='A'&&byte='A'&&byte我发现了导致这一问题的原因。调试器在每一行都会重新评估调用每一行增量的值。因此代码很好,但我在调试器会话中犯了一个错误。节点NULL
是null指针,而不是字符串空终止符(仅为'\0'
)。还请注意for
循环中的所有三个表达式都是可选的,如果省略一个条件,它将被视为“始终为真”。因此,您可以使用for(int i=0;1;i++)而不是
。不确定,但您认为hex_index+=2
表达式在每个循环迭代的末尾做了什么?代码中的某些地方未定义。这不是一个最小化的片段。请不要使用(例如string[i]<127&&string[i]>31
,什么是127
和31
?),并了解。请学习如何使用调试器在监视变量及其值的同时逐条检查代码语句。这是解决此类问题的常用方法。