473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
Nov 14 '05 #1
2 1476
LinuxSunday wrote:
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

Nov 14 '05 #2
"LinuxSunday" <hi*******@yahoo.com> wrote:
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?

--
Simon.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leon Jollans | last post by:
Hi. I'm reading a 5 meg (or so) file from the response stream of a WebRequest, and it's pitifully slow. I mean *really* slow, in the order of 15 minutes to download. Now obviously I'm caching this...
1
by: BH | last post by:
I'm trying a simple object serialization and deserialization, and keep getting this error: System.Runtime.Serialization.SerializationException: Binary stream does not contain a valid...
4
by: Scott, Killer of all Ninjas | last post by:
It seems incredulous to me that it is so difficult to write the contents of a memory stream to a file. I'm certain that I'm missing something simple. I am retrieving a memory stream from a DIME...
5
by: ad | last post by:
I used use SharpZipLib to compress files in disk. But now I want to compress stream into another stream in memory(the stream not associated with disk file) My pseudo is: Stream...
7
by: Jeffrey Spoon | last post by:
Hello, I'm a bit stuck trying to convert a text file which contains extended ASCII text and changing the ASCII values so they become readable. I do this by subtracting 127 from the ASCII value....
3
by: Brian Bagnall | last post by:
I'm writing some code that passes values from a Java program to a C++ program using an output stream. The C++ program requires a UBYTE, which is an unsigned byte (values 0 to 255). Java doesn't...
3
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
2
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: In regards to reading a stream, I know we use the Read() method. One could implement reading a stream using a while-loop. while (stream.Read(buffer, 0, buffer.Length) !=...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.