Home > front end >  Converting a string containing both char's and int's into an integer in C
Converting a string containing both char's and int's into an integer in C

Time:10-15

Say I have the string (represented as a char pointer) given from a strSHA2 hash of a file:

"f731d405b522b69d79f2495f0963e48d534027cc1852dd99fa84ef1f5f3387ee"

How could I effectively turn it into an integer? Is there any way to cast it? atoi() terminates as soon as it reaches a char.

Would iterating through and converting char's using arithmetic such as letter - 'a' be the best way?

I intend to use it as an index for a hash table, thus need an integer.

Length of the integer would be the standard 32bit for C

CodePudding user response:

Say I have the string (represented as a char pointer) given from a strSHA2 hash of a file:

That is then a hexadecimal representation of a 256 bit integer.

Your computer doesn't have a 256 bit integer type, so you can't cast that, possibly.

Instead, you'll want to use a different function from your hashing library that doesn't give you a printable string, but just 32 bytes of raw hash data. You can then use, say, the upper 2 bytes as hash table indices.

Using a 32 byte (256 bit) hash table index makes no sense – no computer in this world has enough memory for a table with 2²⁵⁶ entries.

Honestly, however, if you want a hash table, use an existing hash table instead of building your own.

CodePudding user response:

That's just too long to be stored as an integer

CodePudding user response:

In order to convert a hexadecimal string to an unsigned int data type, you can use the function strtoul.

However, a 32-bit unsigned int is only able to represent numbers up to 232-1, which is insufficient in your example of a 256-bit number.

Therefore, it would only be possible to convert this number into eight integers of type unsigned int.

However, as pointed out in one of the other answers, it does not make sense to use a 256-bit index into a hash table. Since you can probably assume that all of the bits of a SHA-2 hash are sufficiently uniformly distributed for your use-case, it should be sufficient to simply take the first 12 or 16 bits of the SHA-2 hash and use them as an index into your hash table. That way, your hash table would have a length between 32 KiB or 512 KiB, assuming 8 bytes per hash table entry.

  • Related