How to get the values parsed from the following stream of bytes

L

LinuxSunday

After giving some system cmd, I will be getting the o/p which will be
exactly like below. ie., 16 bytes in each line. Here you see 4 lines
of 16bytes each. The o/p given here is Hex coded ascii values.
I need to parse their values and convert into their string values. The
o/p given by the system is as follows.
3e 53 31 37 50 37 35 37 36 59 4d 31 30 4d 5a 33
38 53 30 30 37 31 37 50 37 35 37 36 20 48 38 33
32 32 39 20 50 57 52 2f 46 41 4e 20 53 45 4e 53
45 20 43 41 52 44 20 00 00 00 00 00 00 00 00 33

The starting byte 3e is the 0th byte will tell you how many bytes are
necessary to read or present. here 0x3e is 62(decimal) number of
bytes. so it starts from 0th byte to 61 bytes. This information may
not be necessary, but just FYI.

I need to do the following.
from 1th to 20th byte represents the NameOfProduct[25] which is
53 31 37 50 37 35 37 36 59 4d 31 30 4d 5a 33 38 53 30 30 37 - Hex
coded Ascii

21th - 28th byte is NumberOfProduct[10] which is 31 37 50 37
35 37 36 20 - Ascii

29th - 35th byte is serialNumber[10] 48 38 33 32 32 39 20 - Ascii
Help me in getting these values in an efficient way and stored in to
the string of characters.
Thanks,
LinuxSunday
 
C

Case -

LinuxSunday said:
After giving some system cmd, I will be getting the o/p which will be
exactly like below. ie., 16 bytes in each line. Here you see 4 lines
of 16bytes each. The o/p given here is Hex coded ascii values.
I need to parse their values and convert into their string values. The
o/p given by the system is as follows.
3e 53 31 37 50 37 35 37 36 59 4d 31 30 4d 5a 33
38 53 30 30 37 31 37 50 37 35 37 36 20 48 38 33
32 32 39 20 50 57 52 2f 46 41 4e 20 53 45 4e 53
45 20 43 41 52 44 20 00 00 00 00 00 00 00 00 33

The starting byte 3e is the 0th byte will tell you how many bytes are
necessary to read or present. here 0x3e is 62(decimal) number of
bytes. so it starts from 0th byte to 61 bytes. This information may
not be necessary, but just FYI.

I need to do the following.
from 1th to 20th byte represents the NameOfProduct[25] which is
53 31 37 50 37 35 37 36 59 4d 31 30 4d 5a 33 38 53 30 30 37 - Hex
coded Ascii

21th - 28th byte is NumberOfProduct[10] which is 31 37 50 37
35 37 36 20 - Ascii

29th - 35th byte is serialNumber[10] 48 38 33 32 32 39 20 - Ascii
Help me in getting these values in an efficient way and stored in to
the string of characters.

If 'char xstr[256];' and 'filep' is 'FILE*' to your 'fopen()'ed
file, then use 'fscanf("%s", xstr);' and 'ch = strtol(xstr, 0, 16);'
in a couple of loops. 'char ch;' of course.

Or else, let's talk about an hourly rate for solving your problems
that have nothing to do with the C language.

Case
 
R

Ralmin

LinuxSunday said:
After giving some system cmd, I will be getting the o/p which will
be exactly like below. ie., 16 bytes in each line. Here you see 4
lines of 16bytes each. The o/p given here is Hex coded ascii
values. I need to parse their values and convert into their string
values. The o/p given by the system is as follows.
3e 53 31 37 50 37 35 37 36 59 4d 31 30 4d 5a 33
38 53 30 30 37 31 37 50 37 35 37 36 20 48 38 33
32 32 39 20 50 57 52 2f 46 41 4e 20 53 45 4e 53
45 20 43 41 52 44 20 00 00 00 00 00 00 00 00 33

The starting byte 3e is the 0th byte will tell you how many bytes
are necessary to read or present. here 0x3e is 62(decimal) number
of bytes. so it starts from 0th byte to 61 bytes. This information
may not be necessary, but just FYI.

I need to do the following.
from 1th to 20th byte represents the NameOfProduct[25] which is
53 31 37 50 37 35 37 36 59 4d 31 30 4d 5a 33 38 53 30 30 37 - Hex
coded Ascii

21th - 28th byte is NumberOfProduct[10] which is 31 37 50
37 35 37 36 20 - Ascii

29th - 35th byte is serialNumber[10] 48 38 33 32 32 39 20 - Ascii
Help me in getting these values in an efficient way and stored in
to the string of characters.

I tried this program:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned int i, numBytes;
char NameOfProduct[25] = {0};
char NumberOfProduct[10] = {0};
char serialNumber[10] = {0};

FILE *fp = fopen("readhexstr.in", "r");
if(fp == NULL)
{
printf("Unable to open input file\n");
exit(EXIT_FAILURE);
}
fscanf(fp, "%x", &numBytes);
for(i = 0; i < 20; i++)
{
fscanf(fp, "%hhx", (unsigned char *)(NameOfProduct + i));
}
for(i = 0; i < 8; i++)
{
fscanf(fp, "%hhx", (unsigned char *)(NumberOfProduct + i));
}
for(i = 0; i < 7; i++)
{
fscanf(fp, "%hhx", (unsigned char *)(serialNumber + i));
}
printf("numBytes = %u\n", numBytes);
printf("NameOfProduct = \"%s\"\n", NameOfProduct);
printf("NumberOfProduct = \"%s\"\n", NumberOfProduct);
printf("serialNumber = \"%s\"\n", serialNumber);
fclose(fp);
return 0;
}

But it didn't give me very nice results:

numBytes = 62
NameOfProduct = "S17P7576YM10MZ38S007"
NumberOfProduct = "17P7576 "
serialNumber = "H83229 "

Perhaps there's a bug in my program, or perhaps there's a bug in your
specification?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top