[英]Convert Char* Variable To uint32_t in C
I've got an Sqlite database and i'm reading values back out using this loop: 我有一个Sqlite数据库,并且正在使用此循环读取值:
int i;
for(i=0; i<argc; i++) // [0] = IP, [1] = seq, [2] = count
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
if (atoi(argv[2]) > stealthBeta)
{
confirmSEQ((uint32_t)argv[1]);
}
The value in argv[1]
is stored as an UNSIGNED INT
in the database and is being written out to the console properly but when I call the function I'm getting the wrong number. argv[1]
的值作为UNSIGNED INT
存储在数据库中,并已正确写到控制台中,但是当我调用该函数时,我得到了错误的数字。
I've tried casting it but it's not exactly working. 我已经尝试过投放,但无法正常工作。 What's the proper way to get it from the
char*
to uint32_t
? 将其从
char*
到uint32_t
的正确方法是什么?
To convert a string to the number it represents, use strtol()
or strtoul()
from <stdlib.h>
. 要将字符串转换为它表示的数字,请使用
<stdlib.h>
strtol()
或strtoul()
。 strtoul()
parses an unsigned long
, guaranteed to be at least 32 bit wide. strtoul()
解析一个unsigned long
,保证至少为32位宽。 Passing 0
as the base
parameter allows conversion of hexadecimal representation prefixed with 0x
. 传递
0
作为base
参数可以转换以0x
为前缀的十六进制表示形式。 Values prefixed with just 0
will be parsed as octal, otherwise parsing is done in decimal. 前缀为
0
值将被解析为八进制,否则解析将以十进制完成。
#include <stdlib.h>
...
if (strtol(argv[2], NULL, 0) > stealthBeta) {
confirmSEQ((uint32_t)strtoul(argv[1], NULL, 0));
}
Use atoi()
for conversion to int
. 使用
atoi()
转换为int
。
Use (unsigned)atoi()
for conversion to unsigned
. 使用
(unsigned)atoi()
转换为unsigned
。
For example, in your case: 例如,在您的情况下:
if (atoi(argv[2]) > stealthBeta)
{
confirmSEQ((uint32_t)atoi(argv[1]));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.