代码之家  ›  专栏  ›  技术社区  ›  topcat

如何从strlen返回中减去十六进制,然后添加到十六进制地址?

  •  0
  • topcat  · 技术社区  · 6 年前

    我想检查字符串长度,然后减去0x3(十六进制);然后添加到存储为十六进制的地址;然后将结果放入十六进制地址中。 地址存储在无符号长整型中。

    struct node { unsigned long int address;  char operand_1[12];};
    

    它是这样工作的:

    (0x)seekNP->address = (0x)seek->address +(( (0x)strlen(seek->operand_1)) - 0x3);
    

    或者像这样:

    seekNP->address = seek->address +((strlen(seek->operand_1)) - 0xF);
    

    我知道所有数字最后都是以二进制形式存储的,也许我的问题还应该是,当我在十六进制和十进制大小之间执行算术时,返回类型甚至十进制会发生什么?

    它们是否都自动转换为二进制,然后在二进制上执行操作?

    char Operand_1 = "1000"; 我把它储存在 unsigned long address 通过 seek->address = strtol(seek->operand_1,NULL,16); 添加strlen返回值会正确增加十六进制变量(地址)值吗?

    unsigned long int address; is-> unsigned long int MIKE;

    2 回复  |  直到 6 年前
        1
  •  1
  •   ryyker    6 年前

    首先,符号 (0x)seekNP->address 无效C。您可能正在考虑 typecast ,其中一个变量的类型转换为另一个变量。无论如何:

    seekNP->address = seek->address +((strlen(seek->operand_1)) - 0xF);
    

    是正确的陈述。(假设确实存在 seek->address 它在语法上是正确的。否则,(假设所示结构)这一点更可能是您的意思:

    struct  { 
        unsigned long int address;  
        char operand_1[12];
    }seekNP;
    ....    
    
    //  interger    =         integer         - integer    
    seekNP->address = strlen(seekNP->operand_1) - 0xF; //seems more correct given 
                                                     //the variable you are showing.  
    

    为了澄清,如评论中所述,所有形式的 int decimal ,则, hexadecimal octal 对存储在内存中的值没有影响。因此,C语句可以混合和匹配其语句中各种形式的整数,而不用担心内存中的结果值会受到影响。

    int sum = 103 + 1003 + 10003;
    int sum = 0x67 + 0x3eb + 0x2713
    

    两者都会导致 11109 0x2B65

        2
  •  0
  •   ron    6 年前

    这里有一个简单的加法例子,减法也是这样。

    请注意,当您让编译器完成这项工作并使用数据类型和指针时,算法会为您提供帮助+1或-1到您的 指针 是移动到下一个项目,您不必担心数据是否在一个字节、2个字节或8个以上字节中。

    看起来您自己在做所有的工作,将地址保存在变量中 unsigned long 您认为这是理所当然的64位寻址{或者如果您只是 unsigned int 这是32位寻址,如果有超过2^32字节的可寻址空间},希望您能认识到问题所在。因此,我的建议是使用指针,尤其是在处理超过1字节的数据时。

    unsigned long *pAddress; 意味着pAddress保存一个作为地址的值,因为它被定义为一个指针 * 它指向内存中类型为无符号长整数的数据。内存中的字节数是 无符号长 这取决于你的系统,是在系统层面上处理的事情,也是你通常不必或应该担心的事情。但在今天的计算机上,它可能是64位或长整数。

    我认为,一旦您了解了这一点,就会变得很容易:

    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
       char a[8];
       short int b[8];
       int c[8];
       long int d[8];
       float e[8];
       double f[8];
    
       char *pa;
       short int *pb;
       int *pc;
       long int *pd;
       float *pe;
       double *pf;
    
       pa = a;
       pb = b;
       pc = c;
       pd = d;
       pe = e;
       pf = f;
    
       printf(" address of a is   %p\n", pa );
       printf(" address of b is   %p\n", pb );
       printf(" address of c is   %p\n", pc );
       printf(" address of d is   %p\n", pd );
       printf(" address of e is   %p\n", pe );
       printf(" address of f is   %p\n", pf );
       printf("\n");
       printf(" address of a+1 is %p\n", pa+1 );
       printf(" address of b+1 is %p\n", pb+1 );
       printf(" address of c+1 is %p\n", pc+1 );
       printf(" address of d+1 is %p\n", pd+1 );
       printf(" address of e+1 is %p\n", pe+1 );
       printf(" address of f+1 is %p\n", pf+1 );
       printf("\n");
       printf(" sizeof( char )      is %ld\n", sizeof( char ) );
       printf(" sizeof( short int ) is %ld\n", sizeof( short int ) );
       printf(" sizeof( int)        is %ld\n", sizeof( int ) );
       printf(" sizeof( long int )  is %ld\n", sizeof( long int ) );
       printf(" sizeof( float )     is %ld\n", sizeof( float ) );
       printf(" sizeof( double )    is %ld\n", sizeof( double ) );
    
       printf("\n");
    

    }