Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h5write truncates UTF-8 strings #111

Closed
LTLA opened this issue Jun 7, 2022 · 1 comment
Closed

h5write truncates UTF-8 strings #111

LTLA opened this issue Jun 7, 2022 · 1 comment
Assignees

Comments

@LTLA
Copy link
Contributor

LTLA commented Jun 7, 2022

library(rhdf5)
h5createFile("ex_hdf5file.h5")
h5write("α ≤ 0.1", "ex_hdf5file.h5", "WHEE")
h5read("ex_hdf5file.h5", "WHEE")
## [1] "α ≤ "

This is because nchar should have been called with type="byte" to get the actual byte length, not character length.

While we're at it, if encoding = NULL, you can choose "UTF-8" based on the value of Encoding(obj). This would be more faithful to the in-memory representation of the string in R.

@grimbough
Copy link
Owner

This is hopefully fixed in version 2.41.1

packageVersion("rhdf5")
#> [1] ‘2.41.1’
library(rhdf5)
h5createFile("ex_hdf5file.h5")
h5write("α ≤ 0.1", "ex_hdf5file.h5", "WHEE")
output <- h5read("ex_hdf5file.h5", "WHEE")
output
#> [1] "α ≤ 0.1"

It also sets the encoding to UTF-8 in R, if that's what the HDF5 dataset uses.

Encoding(output)
#> [1] "UTF-8"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants