C语言 如何将 long unsigned 转换为 unsigned char*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16537069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:19:31  来源:igfitidea点击:

How to cast long unsigned to unsigned char*?

ccasting

提问by James

I am trying to hash an unsigned longvalue, 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?

有没有办法实现我的目标,也许是在两者之间进行转换?

回答by user1764961

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 longon your system.

确保通过p或系统上的任何长度使用所有 4 个字节unsigned long

回答by Michael Foukarakis

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)(如果适用于您的架构),请注意您可能会越界访问(未定义的行为)或仅散列对象的一部分。

回答by zakinster

As other said, you can easily read an intor any other object as a chararray :

正如其他人所说,您可以轻松地将一个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 0xdeadstored in a shortor a longwon't have the same hash.

但请注意,0xdead存储在 ashort或 a 中long的 hash 不会相同

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;
}

回答by Vorsprung

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);
}

回答by tangrs

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;
}

回答by dizzer

You should convert it using ultoa_s

您应该使用ultoa_s转换它