Glad to hear that it works. Don’t forget to free the buffer(s) that the HDF5 library allocated for those strings or you’ll have created a memory leak:
Assert.IsTrue(H5.free_memory(rdata[i]) >= 0); G. From: Hdf-forum [mailto:[email protected]] On Behalf Of Matt Wood Sent: Thursday, March 30, 2017 11:23 AM To: [email protected] Subject: Re: [Hdf-forum] Reading VLEN string attribute values. Many thanks, that worked a treat. I have attached my working method below. Matt /// <summary> /// Helper method that extracts a string array value from a variable length attribute. /// See https://github.com/HDFGroup/HDF.PInvoke/blob/master/UnitTests/H5DTest/H5Dread.cs /// </summary> /// <param name="aid">The id of the attribute.</param> /// <param name="tid">The type id of the attribute.</param> /// <returns>An array of strings.</returns> public string[] GetStrings(long aid, long tid) { long space = H5A.get_space(aid); // TODO close space long count = H5S.get_simple_extent_npoints(space); // Create pointer [] to receive the addresses. IntPtr[] rdata = new IntPtr[count]; // Create array for result. string[] result = new string[count]; GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned); // Call the function with address of pointer []. H5A.read(aid, tid, hnd.AddrOfPinnedObject()); hnd.Free(); // For each pointer extract string. for (int ii = 0; ii < rdata.Length; ++ii) { int len = 0; // Find the end of the string (\0) while (Marshal.ReadByte(rdata[ii], len) != 0) { ++len; } byte[] buffer = new byte[len]; // Copy to buffer Marshal.Copy(rdata[ii], buffer, 0, buffer.Length); // Encode the string. result[ii] = Encoding.UTF8.GetString(buffer); } return result; }
_______________________________________________ Hdf-forum is for HDF software users discussion. [email protected] http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org Twitter: https://twitter.com/hdf5
