c - for 循环内的整数在每行增加 2,语句与它没有任何关系
问题描述
我目前在我的 C 程序中遇到奇怪的结果,在一个 for 循环中,变量 hex_index 在每一行增加 2。它与时间无关或任何东西。尽管如此,即使语句与它无关,它也只会增加每一行。
ascii.c
#include <stdio.h>
#include "types.h"
int
main ()
{
char *hex = "68747541";
char output[4];
hex_to_ascii(hex, output);
printf("%s", output);
return 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);
}
类型.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;
}
}
}
问题视频(观看 hex_index,会随机增加)
解决方案
我找到了导致这个的问题。调试器留下了手表,在每一行重新评估调用每行增量的值。所以代码很好,但我在调试器会话中犯了一个错误。
推荐阅读
- angular - 单元测试角度如果条件使按钮禁用
- c# - 当我添加分数时, scoreText 只是快速显示添加到自身的分数
- javascript - 当我单击导航栏上的锚点时,它会将我带到那里,但部分内容被导航栏本身隐藏
- c# - 我的链表 DeleteAt 函数有什么问题?
- flutter - 我在运行我的颤振应用程序时遇到错误
- html - 当元标记告诉更多关于文件的信息时,为什么我们使用 lang 属性作为 html 属性?
- java - android java sqlite中的空指针异常
- java - android中glide的缓存时间段
- javascript - FormData 到对象的格式与节点 bodyparser 相同
- wordpress - 无法在 Elementor 中创建标题