2

I'm using Booksleeve to communicate with Redis from a C# code. The code first retrieves all the fields and values from a hash:

var vals = await Redis.Hashes.GetAll(0, redisKey);

The hash contains fields which have values of type long.

The call to Hashes.GetAll returns the field values as byte arrays. The question is, how can I convert this byte array to C# long?

For example, if I use Hashes.GetInt64 to retrieve the value, I get back 9684, which is correct. But I'm not able to convert the retrieved byte array to long so that it would match this value:

BitConverter.ToInt64(redisHashValue, 0) throws ArgumentException

BitConverter.ToInt32(redisHashValue, 0) returns 876099129

BitConverter.ToInt16(redisHashValue, 0) returns 13881
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63

1 Answers1

1

Well, converting the byte array first to string seems to do the trick:

long.Parse(Encoding.UTF8.GetString(redisHashValue));

But is there a better solution?

Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
  • 2
    Making an API that can support all the different *return types* is... fundamentally tricky; I need to re-engineer the API for cluster, and I'm very tempted to do something a bit more like returning each as a `struct RedisValue`, which internally **has** a byte-array, but which offers implicit conversion operators to `string`, `int`, `long`, `float`, `double` and `byte[]`. I can't think of anything more usable than that, but I'm open to suggestions... – Marc Gravell Nov 27 '13 at 11:14