Reading a structured binary .dat file - file-read

I'm unable to read a binary .dat file which is in the following structure :
typedef struct {
unsigned char message_code;
unsigned char code;
unsigned char sequence;
unsigned char reserve_1;
float ladle_weight;
float tundish_weight;
unsigned char reserve_2;
unsigned char no_of_measurements;
unsigned int measurement_1;
unsigned int measurement_2;
unsigned int measurement_3;
unsigned int measurement_4;
unsigned int measurement_5;
unsigned int measurement_6;
unsigned int measurement_7;
unsigned int measurement_8;
} message_metal;
typedef struct {
unsigned char message_code;
unsigned char strand_no;
unsigned char steel_code;
unsigned char container_state;
float water_pr_at_mould;
float water_con_at_mould;
float temp_diff_of_water_at_mould;
float water_pr_at_common_intake;
float water_con_at_section_1;
float water_con_at_section_2;
float water_con_at_section_3;
float water_con_at_section_4;
float water_con_at_section_5;
float water_pr_at_section_1;
float water_pr_at_section_2;
float water_pr_at_section_3;
float water_pr_at_section_4;
float water_pr_at_section_5;
float withdrawl_speed;
float metal_level_in_mould;
float bloom_length;
} message_strand;
typedef struct {
message_metal metal;
message_strand strand[4];
unsigned char hour;
unsigned char min;
unsigned char sec;
} machine;
The .dat files cannot be attached.

Related

How to convert hex string to byte array in CAPL?

Considering having, for example, this type of hex string:
char hex_str[100] = "0x01 0x03 0x04 0x0A";
How to get out of this string the byte array representation in CAPL, like:
byte hex_str_as_byte_arr[4] = {0x01, 0x03, 0x04, 0x0A};
EDIT: Only Vector CANoe supported data types/functions are allowed!
Use strtok to split the character array into separate hex strings, then use long strtol( const char *restrict str, char **restrict str_end, int base ) to convert each hex string to an integral value.
Thanks to all...
Actually I've found a solution myself:
char hex_str[100] = "0x01 0x03 0x04 0x0A";
long data[4];
dword pos = 0;
pos = strtol(hex_str, pos, data[0]);
pos = strtol(hex_str, pos, data[1]);
pos = strtol(hex_str, pos, data[2]);
pos = strtol(hex_str, pos, data[3]);
write("0x%02x,0x%02x,0x%02x, 0x%02x", data[0], data[1], data[2], data[3]);
Now it's a simple cast: (byte) data[0]
We can use sscanf() to convert the numbers to unsigned char. In a loop, we'll need to also use a %n conversion to determine the reading position for the next iteration.
Here's a simple example (in real life, you'll need some range checking to make sure you don't overrun the output buffer):
#include <stdio.h>
int main(void)
{
const char hex_str[100] = "0x01, 0x03, 0x04, 0x0A";
unsigned char bytes[4];
{
int position;
unsigned char *b = bytes;
for (const char *input = hex_str; sscanf(input, "%hhi, %n", b, &position) == 1; ++b) {
input += position;
}
}
/* prove we did it */
for (size_t i = 0; i < sizeof bytes; ++i) {
printf("%hhu ", bytes[i]);
}
puts("");
}

fwrite with BMP file returning 0 and segmentation fault

I'm trying to read a simple bmp file then invert the color then save it back to the file system using fread and fwrite binary mode.
So I did
BMP_Image * img;
BMP_Header * hdr;
fread(hdr,sizeof(BMP_Header),1, fptr);
img->width = hdr->width;
img->height = hdr->height;
img->bytes_per_pixel = hdr->bits*BIT_TO_BYTE;
img->header = *hdr;
img->data_size = hdr->size;
to get the header information where BMP_Header is a struct
Then read the image data and put into array
fread(data, sizeof(char), img->data_size - 54, fptr);
Then I subtract each element in my image data by 255
int i = 0;
for(i = 0;i<img->data_size;i++){
data[i] = 255 - data[i];
}
Now when I want to write the file back to the file system, I couldn't get it to work properly. I know you'd have to do fwrite 2 times. First writing the header, then writing the image data.
I have so far:
fwrite(header, 1 ,dataSize, fptr_out);
which gives me sgmentation fault and 0 byte bmp file.
Here are the typedefs of my BMP_Header and BMP_image struct
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef int int32_t;
typedef struct {
uint16_t type; // Magic identifier
uint32_t size; // File size in bytes
uint16_t reserved1; // Not used
uint16_t reserved2; // Not used
uint32_t offset; // Offset to image data in bytes
uint32_t header_size; // Header size in bytes
int32_t width; // Width of the image
int32_t height; // Height of image
uint16_t planes; // Number of color planes
uint16_t bits; // Bits per pixel
uint32_t compression; // Compression type
uint32_t imagesize; // Image size in bytes
int32_t xresolution; // Pixels per meter
int32_t yresolution; // Pixels per meter
uint32_t ncolours; // Number of colors
uint32_t importantcolours; // Important colors
} BMP_Header;
typedef struct {
BMP_Header header;
int data_size;
int width;
int height;
int bytes_per_pixel; // This amount should be equals to number of bits/8
char *data;
} BMP_Image;

Why is OpenMP in a mex file only producing 1 thread?

I am new to OpenMP. I have the following code which compiles fine using Matlab mex configured with MSVS2010. The computer has 8 processors available (which I checked also by using matlabpool).
#include "mex.h"
#include <omp.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//Takes a uint8 input array and uint32 index array and preallocated uint8 array the same
//size as the first one and copies the data over using the indexed mapping
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
{
uint N = mxGetN(prhs[0]);
mexPrintf("n=%i\n", N); mexEvalString("drawnow");
uchar *input = (uchar*)mxGetData(prhs[0]);
uint *index = (uint*)mxGetData(prhs[1]);
uchar *output = (uchar*)mxGetData(prhs[2]);
uint nThreads, tid;
#pragma omp parallel private(tid) shared(input, index, output, N, nThreads) num_threads(8)
{
tid = omp_get_thread_num();
if (tid==0) {
nThreads = omp_get_num_threads();
}
for (int i=tid*N/nThreads;i<tid*N/nThreads+N/nThreads;i++){
output[i]=input[index[i]];
}
}
mexPrintf("nThreads = %i\n",nThreads);mexEvalString("drawnow");
}
The output I get is
n=600000000
nThreads = 1
Why is only one thread being created despite me requesting 8?
Sigh. Typical, spend hours trying and failing and then find the answer 5 minutes after posting to SO.
The file needs to be mexed with openmp support
mex mexIndexedCopy.cpp COMPFLAGS="/openmp $COMPFLAGS"

Xor between 2 NSString gives wrong result

I have this method to make a xor between 2 NSStrings, i´m printing the result on NSLog but it isn´t the expect.
Can´t figure out what i´m doing wrong.
(void)XorSecretKeyDeviceId
{
NSString* secretKey = #"123";//
NSString* deviceId = #"abcdef";//
NSData* stringKey = [secretKey dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringDeviceId = [deviceId dataUsingEncoding:NSUTF8StringEncoding];
unsigned char* pBytesInput = (unsigned char*)[stringKey bytes]; //Bytes
unsigned char* pBytesKey = (unsigned char*)[stringDeviceId bytes];
unsigned int vlen = [secretKey length]; //Keys Length
unsigned int klen = [deviceId length];
unsigned int v;
unsigned int k = vlen % klen;
unsigned char c;
for(v = 0; v < vlen; v++)
{
c = pBytesInput[v] ^ pBytesKey[k];
pBytesInput[v] = c;
NSLog(#"%c", c);
k = (++k < klen ? k : 0);
}
}
Are you setting your pBytesInput and pBytesKey variables correctly? At the moment, you have unsigned char* pBytesInput = (unsigned char*)[stringKey bytes]; (i.e. the input is the "key"), and pBytesKey is the device ID. This seems odd.
Also, be careful using UTF-8 encoding. UTF-8 uses the high bit on any byte in the string to indicate a "continuation" of a multi-byte character into the next byte. Your encoding could plausibly generate invalid UTF-8 by giving the setting the high bit of the final byte in the encryption.
For more than that, you'll have to say what the "wrong result" is.

NSInteger problem

How to use the NSInteger give example and simplecode
NSInteger is defined like this:
#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
It's the same as int on 32Bit or long on 64Bit.
NSInteger a, b, c;
a = 2;
b = 40;
c = a + b;
NSLog(#"THE answer is: %d\n", c);