比较十六进制值中的 n 个字符序列

发布于 2024-11-07 15:11:17 字数 869 浏览 1 评论 0原文

我基本上是在尝试将令牌(字符串)与十六进制数据类型进行比较!

示例:

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

int main(int argc, char *argv[]) {

    char* token ="Hello";
    int hex = 0x2F;


    if(strncmp(token, "Hil", 3)==0){
        printf("Token founde"); //works!
    }else{
        printf("sorry seems to be the hardest word");
    }

}

当然,这确实有效。 我正在比较至少有 n 个字符的 2 个字符串......没问题。

但现在我想用十六进制值而不是字符串做同样的事情! 像这样的事情:

 int hex = 0x2F;


    if(strncmp((char*)hex, "0x", 2)==0){
                printf("Token founde"); //works!
            }else{
                printf("sorry seems to be the hardest word");
            }

        }

由于 strncmp 仅比较字符串,我尝试将十六进制值转换为 char*... 它根本不起作用。

我到处搜索过......甚至在我的“编程语言C”书中......但我不知道如何解决这个问题。

在java中,我会使用startsWith等东西...但我不知道c中是否有一个熟悉的函数可以具有相同的效果。

I'm basicly trying to compare a token(string) with a hex-datatype!

Example:

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

int main(int argc, char *argv[]) {

    char* token ="Hello";
    int hex = 0x2F;


    if(strncmp(token, "Hil", 3)==0){
        printf("Token founde"); //works!
    }else{
        printf("sorry seems to be the hardest word");
    }

}

This actually works, of course.
I'm comparing 2 strings with at least n charachters...no problem.

But now I want to do the same thing with a hex-value instead of a string!
Something like this:

 int hex = 0x2F;


    if(strncmp((char*)hex, "0x", 2)==0){
                printf("Token founde"); //works!
            }else{
                printf("sorry seems to be the hardest word");
            }

        }

Since strncmp compares strings only, I tried to cast the hex-value to a char*...
It doesn't work at all.

I've searched everywhere... even in my "The programming language C" book...but I can't figure out how to solve this problem.

In java, I'd use something like startsWith etc... but I don't know if there is a familiar function in c that could have the same effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˇ宁静的妩媚 2024-11-14 15:11:17

嗯,我不确定这是否是您想要的,但现在您正在将十六进制的转换为(肯定是无效的)地址。要执行您想做的操作,您必须获取地址:strncmp((char*)&hex, "0x", 2) 但我不建议这样做。

我建议你先将十六进制转换为字符串,然后再进行比较:

char buf[256];
snprintf(buf, 256, "0x%x", hex);
if(strncmp(buf, "0x", 2)==0){

Hm, I am not sure that this is what you want, but right now you are converting the value of hex to an (most certainly invalid) address. To do what you want to do you have to take the address: strncmp((char*)&hex, "0x", 2) but I cannot recommend that.

I suggest that you transform the hex into a string first and then compare:

char buf[256];
snprintf(buf, 256, "0x%x", hex);
if(strncmp(buf, "0x", 2)==0){
死开点丶别碍眼 2024-11-14 15:11:17

您可以使用 sprintf

char hexVal[3];
sprintf(hexVal, "%02X", hex);
if(strncmp(token, hexVal, 3)==0){

You can use sprintf:

char hexVal[3];
sprintf(hexVal, "%02X", hex);
if(strncmp(token, hexVal, 3)==0){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文