将argv [1]转换为十六进制 [英] convert argv[1] to hex
问题描述
我有一个程序可以检查从
命令行传入的十六进制值。
>
假设用户输入c:\demo.exe 3c
,在程序中,我#define SOME_CMD 0x3c。
所以如何将argv [1]转换为未编译的char,以便我可以比较
如下:
if(ucCMD == SOME_CMD)
{
//做点什么
}
ucCMD被声明为unsigned char。
Hi,
I have a program that check for the hex value that passes in from the
command line.
Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.
So how can I convert the argv[1] to unsgined char so that I can compare
it like this:
if ( ucCMD == SOME_CMD )
{
//do something
}
ucCMD is declared as unsigned char.
推荐答案
cybernerdsx2写道:
cybernerdsx2 wrote:
我有一个程序可以检查从
命令行传入的十六进制值。
假设用户输入c:\demo.exe 3c
,在程序中,我#define SOME_CMD 0x3c。
那么我怎样才能转换argv [1]到ungined char,这样我可以比较
就像这样:
if(ucCMD == SOME_CMD)
{
//做点什么
}
ucCMD被声明为unsigned char。
Hi,
I have a program that check for the hex value that passes in from the
command line.
Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.
So how can I convert the argv[1] to unsgined char so that I can compare
it like this:
if ( ucCMD == SOME_CMD )
{
//do something
}
ucCMD is declared as unsigned char.
一个明显的解决方案是编写一个函数,扫描argv [1]中的
字符(从右到左)并添加连续的权力
16来产生你的无符号整数。
An obvious solution would be to write a function that scans through the
characters in argv[1] (right to left) and adds up successive powers of
16 to result in your unsigned integer.
bwray .. 。@ gmail.com写道:
bwray...@gmail.com wrote:
cybernerdsx2写道:
cybernerdsx2 wrote:
我有一个程序检查从
命令行传入的十六进制值。
假设用户输入c :\ demo.exe 3c
在程序中,我#define SOME_CMD 0x3c。
那么如何将argv [1]转换为unsgined char这样我可以比较
就像这样:
if(ucCMD == SOME_CMD)
{
//做点什么
}
ucCMD被声明为unsigned char。
Hi,
I have a program that check for the hex value that passes in from the
command line.
Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.
So how can I convert the argv[1] to unsgined char so that I can compare
it like this:
if ( ucCMD == SOME_CMD )
{
//do something
}
ucCMD is declared as unsigned char.
一个明显的解决方案是编写一个函数,扫描argv [1]中的
字符(从右到左)并添加连续的权力
16导致你的无符号整数。
An obvious solution would be to write a function that scans through the
characters in argv[1] (right to left) and adds up successive powers of
16 to result in your unsigned integer.
PS:您还可以在自己喜欢的
标准C库书中查找名为sscanf()的函数。
PS: You could also look up a function called sscanf() in your favorite
standard C library book.
bw******@gmail.com 写道:
bw******@gmail.com writes:
cybernerdsx2写道:
cybernerdsx2 wrote:
>那么如何将argv [1]转换为unsgined char这样我可以比较
它是这样的:
>So how can I convert the argv[1] to unsgined char so that I can compare
it like this:
一个明显的解决方案是编写一个扫描
$ b $的函数b argv [1]中的字符(从右到左)并将
16的连续幂相加,得到无符号整数。
An obvious solution would be to write a function that scans through the
characters in argv[1] (right to left) and adds up successive powers of
16 to result in your unsigned integer.
为什么你要从右到左做这个?从左到右
转换至少同样容易。为什么你想要不必要地重塑strtol()?
-
"你的修正是100%正确且0%有帮助。干得好!"
- 理查德希思菲尔德
Why would you want to do this right-to-left? Left-to-right
conversion is at least as easy. And why would you want to
reinvent strtol() unnecessarily?
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
这篇关于将argv [1]转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!