Binary Data Hash Issue


#1

I have added a ttf file as binary data in the Projucer and it has created the BinaryData files. The issue is in the getNamedResouce. The " while (*resourceNameUTF8 != 0)" never gets executed since the first data byte is a zero while the case statement has 0x93fe9a1e in it. If I switch the case statement to case on zero, all works fine.


#2

Sorry, I’m a bit confused. What exactly is going wrong?

Is resourceNameUTF8 a nullptr? If not, what characters does it contain?


#3

It is not a null pointer, it points to the binary representation of a png. It is just that the first byte of this binary representation is zero. In the while loop it looks that the value does not equal zero ( while (*resourceNameUTF8 != 0)). In this case the first byte is zero so it bails with a hash code == 0, whereas in the case statement it does not equal zero. For now I am forgoing using this method and just access the variables directly.


#4

The name of your resource is a binary representation of a png? That doesn’t sound right at all…

How are you trying to use getNamedResouce? I expect resourceNameUTF8 to be something like your_image_file_ttf.


#5

It is named as you say. The resourceNameUTF8 is the variable name in the function call:
const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes)


#6

I still feel like we’re talking at cross-purposes.

const char* getNamedResource (const char* resourceNameUTF8, int& numBytes)
{
    unsigned int hash = 0;
    if (resourceNameUTF8 != 0)
        while (*resourceNameUTF8 != 0)
            hash = 31 * hash + (unsigned int) *resourceNameUTF8++;

    switch (hash)
...

If you call

int dataSize;
auto* data = getNamedResource ("your_image_file_ttf", dataSize);

then hash will not be zero.

What am I missing?


#8

This is the first time I have every had this issue, but the hash is zero because the first byte of binary data is zero (you are dereferencing the pointer) so the while loop never runs. However the hash that was automatically put into the switch statement is not zero. hence it always returns a nullptr (for this particular case). Here are the first few bytes:
static const unsigned char temp_binary_data_0[] = { 0,1,0,0,0,17,1,0,0,4,0,16,7


#9

How are you calling getNamedResource in your code? In the example I posted, when we enter the body of getNamedResource then resourceNameUTF8 contains a string used to do the lookup:

const char* resourceNameUTF8 = "your_image_file_ttf";

This will not hash to zero.

It sounds like you are doing something along the lines of

int dataSize;
auto* data = getNamedResource (BinaryData::your_image_file_ttf, dataSize);

which is not how that function should be used! If you already have a pointer to the data stored in BinaryData then you can just use that directly…


#10

You are correct and sorry for all the messages. I was calling it using the const char * My_ttf which points to the data, not the name. Big mistake on my part. Thanks for your patience.