简体   繁体   English

如何将long unsigned转换为unsigned char *?

[英]How to cast long unsigned to unsigned char*?

I am trying to hash an unsigned long value, but the hash function takes an unsigned char * , as seen in the implementation below: 我试图散列unsigned long值,但散列函数采用unsigned char * ,如下面的实现中所示:

unsigned long djb2(unsigned char *key, int n)
{
    unsigned long hash = 5381;
    int i = 0;
    while (i < n-8) {
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
        hash = hash * 33 + key[i++];
    }
    while (i < n)
        hash = hash * 33 + key[i++];
    return hash;
}

Is there a way I can achieve my goal, perhaps with a cast between the two? 有没有办法实现我的目标,也许是两者之间的演员?

unsigned long x;

unsigned char * p = (unsigned char*)&x;

Make sure you use all 4 bytes through the p , or whatever is the length of unsigned long on your system. 确保在p使用全部4个字节,或者在系统上使用unsigned long的长度。

Technically you can achieve it with: 从技术上讲,你可以实现它:

unsigned long value = 58281;
djb2((unsigned char *) &value, sizeof(value));

Mind the usual pitfalls, however: 但要注意常见的陷阱:

  • The hash function in question was originally meant for strings (hence the prototype), so make sure it fits your needs (# of collisions, avalanching, etc.) 有问题的哈希函数最初用于字符串(因此是原型),因此请确保它符合您的需求(碰撞次数,雪崩等)
  • If at some point you want to hash very large objects for which sizeof(object) > (int) sizeof(object) (if applicable on your architecture(s)), note you might get out of bounds accesses (undefined behaviour) or only part of your object hashed. 如果在某些时候你想要散列sizeof(object) > (int) sizeof(object)非常大的对象(如果适用于你的体系结构),请注意你可能会超出边界访问(未定义的行为)或仅你的对象的一部分哈希。

As other said, you can easily read an int or any other object as a char array : 正如其他人所说,您可以轻松地将int或任何其他对象读取为char数组:

unsigned char value = 0xde;
unsigned short value = 0xdead;
unsigned long value = 0xdeadbeef;
double value = 1./3;

djb2((unsigned char*)&value, sizeof value);

But note that 0xdead stored in a short or a long won't have the same hash . 但请注意,存储在shortlong中的0xdead 将不会具有相同的哈希值

Also note that your hash function could be better unrolled using a Duff's device : 另请注意,使用Duff的设备可以更好地展开哈希函数:

unsigned long djb2(unsigned char *k, int size)
{
    unsigned long h = 5381;
    int i = 0;
    switch(size % 8) {
      case 0: while(i < size) { 
                  h = h*33 + k[i++];
      case 7:     h = h*33 + k[i++];
      case 6:     h = h*33 + k[i++];
      case 5:     h = h*33 + k[i++];
      case 4:     h = h*33 + k[i++];
      case 3:     h = h*33 + k[i++];
      case 2:     h = h*33 + k[i++];
      case 1:     h = h*33 + k[i++];
              }
    }
    return h;
}

This shows a cast working. 这显示了演员阵容。 Note that in this case the "ABC" string will be null terminated but this may require more care in real world cases 请注意,在这种情况下,“ABC”字符串将被空终止,但这可能需要在现实世界中更加小心

#include <stdio.h>

int main() {
    unsigned long x=0x414243;  #0x414243 is ABC
    unsigned char *s=(unsigned char *)&x;
    printf("%s", s);
}

Since you've posted your code now, you'd want to use something similar to this: 由于您现在已经发布了代码,因此您需要使用与此类似的内容:

#include <stdio.h>


int main() {
    unsigned long result, x = 0xdeadbeef;
    x = convert_endian(x);

    result = djb2((unsigned char*)&x, sizeof(x));
    do_something(result);
    return 0;
}

你应该使用ultoa_s转换它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM