Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gets the string representation for a given SMCVal_t struct (as NSString).
/**
* Gets the string representation for a given SMCVal_t struct (as NSString).
*/
-(BOOL) stringRepresentationFromVal:(SMCVal_t)val toString:(NSString**)abri
{
if (val.dataSize > 0) {
if ((strcmp(val.dataType, DATATYPE_UINT8) == 0) ||
(strcmp(val.dataType, DATATYPE_UINT16) == 0) ||
(strcmp(val.dataType, DATATYPE_UINT32) == 0))
*abri = [[NSString alloc] initWithFormat:@"%u", (unsigned int)[self _strtoul:(char *)val.bytes forSize:val.dataSize inBase:10]];
else if (strcmp(val.dataType, DATATYPE_FP1F) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.5f", (ntohs(*(UInt16*)val.bytes) / 32768.0)];
else if (strcmp(val.dataType, DATATYPE_FP4C) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.5f", (ntohs(*(UInt16*)val.bytes) / 4096.0)];
else if (strcmp(val.dataType, DATATYPE_FP5B) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.5f", (ntohs(*(UInt16*)val.bytes) / 2048.0)];
else if (strcmp(val.dataType, DATATYPE_FP6A) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.4f", (ntohs(*(UInt16*)val.bytes) / 1024.0)];
else if (strcmp(val.dataType, DATATYPE_FP79) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.4f", (ntohs(*(UInt16*)val.bytes) / 512.0)];
else if (strcmp(val.dataType, DATATYPE_FP88) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.3f", (ntohs(*(UInt16*)val.bytes) / 256.0)];
else if (strcmp(val.dataType, DATATYPE_FPA6) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.2f", (ntohs(*(UInt16*)val.bytes) / 64.0)];
else if (strcmp(val.dataType, DATATYPE_FPC4) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.2f", (ntohs(*(UInt16*)val.bytes) / 16.0)];
else if (strcmp(val.dataType, DATATYPE_FPE2) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.2f", (ntohs(*(UInt16*)val.bytes) / 4.0)];
else if (strcmp(val.dataType, DATATYPE_SP1E) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.5f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 16384.0)];
else if (strcmp(val.dataType, DATATYPE_SP3C) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.5f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 4096.0)];
else if (strcmp(val.dataType, DATATYPE_SP4B) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.4f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 2048.0)];
else if (strcmp(val.dataType, DATATYPE_SP5A) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.4f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 1024.0)];
else if (strcmp(val.dataType, DATATYPE_SP69) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.3f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 512.0)];
else if (strcmp(val.dataType, DATATYPE_SP78) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.3f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 256.0)];
else if (strcmp(val.dataType, DATATYPE_SP87) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.3f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 128.0)];
else if (strcmp(val.dataType, DATATYPE_SP96) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.2f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 64.0)];
else if (strcmp(val.dataType, DATATYPE_SPB4) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.2f", (((SInt16)ntohs(*(UInt16*)val.bytes)) / 16.0)];
else if (strcmp(val.dataType, DATATYPE_SPF0) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.0f", ((float)ntohs(*(UInt16*)val.bytes))];
else if (strcmp(val.dataType, DATATYPE_SI8) == 0 && val.dataSize == 1)
*abri = [[NSString alloc] initWithFormat:@"%d", ((signed char)*val.bytes)];
else if (strcmp(val.dataType, DATATYPE_SI16) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%d", (ntohs(*(SInt16*)val.bytes))];
else if (strcmp(val.dataType, DATATYPE_PWM) == 0 && val.dataSize == 2)
*abri = [[NSString alloc] initWithFormat:@"%.1f%%", (ntohs(*(UInt16*)val.bytes) * 100 / 65536.0)];
else if (strcmp(val.dataType, DATATYPE_CHARSTAR) == 0) {
*abri = [[NSString alloc] initWithFormat:@"%s", val.bytes];
}
else if (strcmp(val.dataType, DATATYPE_FLAG) == 0)
*abri = [[NSString alloc] initWithFormat:@"%s", val.bytes[0] ? "TRUE" : "FALSE"];
else {
int i;
char tempAb[64];
for (i = 0; i < val.dataSize; i++) {
snprintf(tempAb+strlen(tempAb), 8, "%02x ", (unsigned char) val.bytes[i]);
}
*abri = [[NSString alloc] initWithFormat:@"%s", tempAb];
}
} else {
return TRUE;
}
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.