Skip to content

Implementation of a hex string conversion #257

@lamium2

Description

@lamium2

Hello, I wanted to store xxhash64 strings as integer64 and was surprised that there is no function that can convert hex strings directly to integer64.

As a workaround, I wrote a Rcpp function:

cppFunction('
NumericVector hex_to_int64_cpp(CharacterVector hex_strings) {
  int n = hex_strings.size();
  NumericVector out(n);
  
  for(int i = 0; i < n; ++i) {
    if (CharacterVector::is_na(hex_strings[i])) {
      out[i] = NA_REAL;
    } else {
      const char* s = hex_strings[i];
      char* endptr;
      unsigned long long val = std::strtoull(s, &endptr, 16);
      ((unsigned long long*)out.begin())[i] = val;
    }
  }
  out.attr("class") = "integer64";
  return out;
}
')

In my opinion, such a function would be quite practical for efficiently storing 64-bit hashes created with digest. Perhaps this could be an idea for a new feature. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions