How to convert a XZ compression output to a NSString? - nsstring

I've successfully set up a small XZ compressor which returns a std::string that contains the compressed output. To process the result I need to "convert" the std::string to a NSString. Unfortunately there are (encoding?) problems:
Even though "abc" is not a good example it shows the difficulty pretty good:
NSString *text = [_text string]; // Length is 3
std::string content = xz_compress([text UTF8String]); // Length is 60
NSString *convertedContent = [NSString stringWithCString:content.c_str() encoding:NSUTF8StringEncoding];
// convertedContent is (null) and 0 characters long
Using NSUnicodeStringEncoding makes the NSString at least 2 characters long but they don't match the ones from std::string.
My questions:
Is this way possible / the preferable way (already unsure about that)?
If so: What encoding do I need to use?
Thanks in advance!
Paul

I assume that xz_compress output is binary data. So, why don't you try and use NSData dataWithBytes:length: method? Possibly you could also try with string::data() instead of string::c_str for the same reason:
NSData* convertedContent = [NSData dataWithBytes:content.data() length:content.length()];

Related

How can i split a wchar_t / TCHAR / WCHAR / LPTSTR into a QStringList?

While working with the Win32API, the function i must use returns its results by writing them to buffer of type LPTSTR as well as the individual number of characters that were written.enter code here
As this buffer is a string, and the function can return multiple values, the actual result data look something like this:
Value1\0Value2\0Value3\0\0
What is the best way to get this into a QStringList?
LPTSTR = Long Pointer to TCHAR. On modern systems (those with unicode support) this is synonymous with a WCHAR array.
Since your output buffer will contain characters where each is two bytes it is thus compatible with UTF16.
QString has a fromUtf16 static method which requires a simple cast to satisfy the compiler.
In this case, we MUST also specify the total length of the entire string. Failure to do this results in QString only reading the input data up until the first null character, ignoring any other result data.
Once we actually have a QString to work with, splitting it is simple. Call QString's split() method specifying a null character wrapped in a QChar.
Optionally, and required in my case, specifying SplitBehavior as SkipEmptyParts ensures that no empty strings (the result of parsing the null character) end up in my desired result (the QStringList of values).
Example:
// The data returned by the API call.
WCHAR *rawResultData = L"Value1\0Value2\0Value3\0";
// The number of individual characters returned.
quint64 numberOfWrittenCharacters = 22;
// Create a QString from the returned data specifying
// the size.
QString rString =
QString::fromUtf16((const ushort *)rawResultData, numberOfWrittenCharacters);
// Finally, split the string into a QStringList
// ignoring empty results.
QStringList results =
rString.split(QChar(L'\0'), QString::SkipEmptyParts);

How to convert double* data to const char* or QByteArray efficiently

I am trying to use the network programming APIs in Qt in my project. One part of my code requires me to convert double* data to QByteArray or a const char*.
I searched through the stackoverflow questions and could find many people suggesting this code :
QByteArray array(reinterpret_cast<const char*>(data), sizeof(double));
or, for an array of double :
QByteArray::fromRawData(reinterpret_cast<const char*>(data),s*sizeof(double));
When I use them in my function, It does notgive me the desired result. The output seems to be random characters.
Please Suggest an efficient way to implement it in Qt. Thank you very much for your time.
Regards
Alok
If you just need to encode and decode a double into a byte array, this works:
double value = 3.14159275;
// Encode the value into the byte array
QByteArray byteArray(reinterpret_cast<const char*>(&value), sizeof(double));
// Decode the value
double outValue;
// Copy the data from the byte array into the double
memcpy(&outValue, byteArray.data(), sizeof(double));
printf("%f", outValue);
However, that is not the best way to send data over the network, as it will depend on the platform specifics of how the machines encode the double type. I would recommend you look at the QDataStream class, which allows you to do this:
double value = 3.14159275;
// Encode the value into the byte array
QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream << value;
// Decode the value
double outValue;
QDataStream readStream(&byteArray, QIODevice::ReadOnly);
readStream >> outValue;
printf("%f", outValue);
This is now platform independent, and the stream operators make it very convenient and easy to read.
Assuming that you want to create a human readable string:
double d = 3.141459;
QString s = QString::number(d); // method has options for format and precision, see docs
or if you need localization where locale is a QLocale object:
s = locale.toString(d); // method has options for format and precision, see docs
You can easily convert the string into a QByteArray using s.toUtf8() or s.toLatin1() if really necessary. If speed is important there also is:
QByteArray ba = QByteArray::number(d); // method has options for format and precision, see docs

NetCDF-Java: how to write unsigned attribute?

I'm trying to write an unsigned value to a valid_range attribute in an nc4 file using NetCDF-Java 4.3, but it is always written as signed. This makes viewing applications display the data incorrectly. Sample code:
Array array = Array.factory(DataType.byte, new int[] { 2 });
array.setUnsigned(true);
array.setObject(0, 0);
array.setObject(1, 255);
Attribute attr = new Attribute("valid_range", array);
log.debug("{}", attr);
This prints out:
valid_range = 0UB, -1UB
But the CDL for that attribute in the resulting file is:
colour1:valid_range = 0b, -1b ;
When I look at that variable in ToolsUI 4.3, it's all blank because everything is outside of the false range. I tried using shorts to represent unsigned bytes; that works for valid_range but fails for the _FillValue attribute:
WARN Nc4Iosp - _FillValue type must agree with var = colour2 type short!=byte
How should unsigned types be written to attributes?
Netcdf-Java stable version is 4.6, and 4.3 is pretty out of date, esp for writing to netcdf4. So you should see how/if 4.6 works.
Netcdf-Java version 5 has added unsigned data types to make this easier, though thats not yet stable.
Widening valid_range is a good solution. You must leave _FillValue as the same type as the variable, though as you noted.
Why do you want to set valid_range? IMO, its not a good practice, because it forces client software to check for validity. Dont use it unless you really need it. Documenting the actual_range is more useful.
If the variable is unsigned the valid_range values should be widened and stored as unsigned integers.
Array array = Array.factory(DataType.byte, new int[] { 2 });
Change that to...
Array array = Array.factory(DataType.INT, new int[] { 2 });
This came from Unidata's website.
https://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html

Binary representation of NSString

I need to someone to send me binary data via NSStream and I will convert it into NSString on my end. What's the binary representation that they need to know (byte size, byte order) in order for me to convert the data successfully back into the same string in NSString?
First get the data and convert it into NSData
Then convert NSData to NSString
NSString* newStr = [[[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding] autorelease];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
(If you have ARC enabled, remove the -autorelease call.)

QByteArray to integer

As you may have figured out from the title, I'm having problems converting a QByteArray to an integer.
QByteArray buffer = server->read(8192);
QByteArray q_size = buffer.mid(0, 2);
int size = q_size.toInt();
However, size is 0. The buffer doesn't receive any ASCII character and I believe the toInt() function won't work if it's not an ASCII character. The int size should be 37 (0x25), but - as I have said - it's 0.
The q_size is 0x2500 (or the other endianness order - 0x0025).
What's the problem here ? I'm pretty sure q_size holds the data I need.
Something like this should work, using a data stream to read from the buffer:
QDataStream ds(buffer);
short size; // Since the size you're trying to read appears to be 2 bytes
ds >> size;
// You can continue reading more data from the stream here
The toInt method parses a int if the QByteArray contains a string with digits. You want to interpret the raw bits as an integer. I don't think there is a method for that in QByteArray, so you'll have to construct the value yourself from the single bytes. Probably something like this will work:
int size = (static_cast<unsigned int>(q_size[0]) & 0xFF) << 8
+ (static_cast<unsigned int>(q_size[1]) & 0xFF);
(Or the other way around, depending on Endianness)
I haven't tried this myself to see if it works but it looks from the Qt docs like you want a QDataStream. This supports extracting all the basic C++ types and can be created wth a QByteArray as input.
bool ok;
q_size.toHex().toInt(&ok, 16);
works for me
I had great problems in converting serial data (QByteArray) to integer which was meant to be used as the value for a Progress Bar, but solved it in a very simple way:
QByteArray data = serial->readall();
QString data2 = tr(data); //converted the byte array to a string
ui->QProgressBar->setValue(data2.toUInt()); //converted the string to an unmarked integer..
This works for me:
QByteArray array2;
array2.reserve(4);
array2[0] = data[1];
array2[1] = data[2];
array2[2] = data[3];
array2[3] = data[4];
memcpy(&blockSize, array2, sizeof(int));
data is a qbytearray, from index = 1 to 4 are array integer.
Create a QDataStream that operates on your QByteArray. Documentation is here
Try toInt(bool *ok = Q_NULLPTR, int base = 10) const method of QByteArray Class.
QByteArray Documentatio: http://doc.qt.io/qt-5/QByteArray.html

Resources