Stripping Hex and then ASCII

C

Colin Green

Hi this is for Serial Com.. On the 'send side'. I will be sending, for
example "how are you?". It will be then coupled with the Header
Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
what is actually sent is 34 486F772061726520796F753F 0C.

My questions are to write them in C
1) How to strip the Header and Checksum from the actual message?
2) How to convert the 486F772061726520796F753F into ASCII again? (that
means on the receiving end I should see "how are you?"

I need to store them on the buffer

Any code that woudl help will be appreciated

yours
Green
 
E

Emmanuel Delahaye

In said:
Hi this is for Serial Com.. On the 'send side'. I will be sending, for
example "how are you?". It will be then coupled with the Header
Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
what is actually sent is 34 486F772061726520796F753F 0C.

My questions are to write them in C
1) How to strip the Header and Checksum from the actual message?
2) How to convert the 486F772061726520796F753F into ASCII again? (that
means on the receiving end I should see "how are you?"

I need to store them on the buffer

Any code that woudl help will be appreciated

You want sprintf() (encode) and strncat() + strtol() (decode). But we are not
going to write your assignment for you. It's your job.

That said, you can get some ideas here:

http://mapage.noos.fr/emdel/clib.htm
Module S
 
D

Drew MacDonald

Colin said:
Hi this is for Serial Com.. On the 'send side'. I will be sending, for
example "how are you?". It will be then coupled with the Header
Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
what is actually sent is 34 486F772061726520796F753F 0C.

My questions are to write them in C
1) How to strip the Header and Checksum from the actual message?
2) How to convert the 486F772061726520796F753F into ASCII again? (that
means on the receiving end I should see "how are you?"

I need to store them on the buffer

Any code that woudl help will be appreciated

yours
Green

Try using a union:

union
{
unsigned char data[13];

struct
{
unsigned char header;
char message[11];
unsigned char checksum;
} values;
} packet;

On the send end:

packet.values.header = 0x34;
packet.values.checksum = 0x0C;
strncpy(packet.values.message, "how are you?", 11);

Then send the packet something like:
write(serialPortHandle, packet.data, 13);

On the receive side, just read like:
read(serialPortHandle, packet.data, 13);

Then extract the values from the union.

char receivedMessage[11];

header = packet.values.header;
header = packet.values.checksum;
strncpy(receivedMessage, packet.values.message, 11);

If you're going to use the receivedMessage as a string, you
need to remember to add a terminating null, with will require
that receivedMessage be at least 1 byte longer than the maximum
size of the message you expect.
 
E

Emmanuel Delahaye

Try using a union:

union
{
unsigned char data[13];

struct
{
unsigned char header;
char message[11];
unsigned char checksum;
} values;
} packet;

On the send end:

packet.values.header = 0x34;
packet.values.checksum = 0x0C;
strncpy(packet.values.message, "how are you?", 11);

Then send the packet something like:
write(serialPortHandle, packet.data, 13);

On the receive side, just read like:
read(serialPortHandle, packet.data, 13);

Then extract the values from the union.

char receivedMessage[11];

header = packet.values.header;
header = packet.values.checksum;
strncpy(receivedMessage, packet.values.message, 11);

If you're going to use the receivedMessage as a string, you
need to remember to add a terminating null, with will require
that receivedMessage be at least 1 byte longer than the maximum
size of the message you expect.

Structures and unions are not portable and should not be used to implement
physical interfaces.

BTW, read() and write() and system functions. They don't belong to the
standard C language library.
 
D

Drew MacDonald

Emmanuel said:
Structures and unions are not portable and should not be used to implement
physical interfaces.

I know I'm about to ask a question that will potentially label me as an
idiot, but how are structures and unions NOT portable (as long as you
aren't using some system specific types)?
BTW, read() and write() and system functions. They don't belong to the
standard C language library.

I know, I was just using them as a simple example.

Drew.
 
B

Ben Pfaff

Drew MacDonald said:
I know I'm about to ask a question that will potentially label me as
an idiot, but how are structures and unions NOT portable (as long as
you aren't using some system specific types)?

The sizes of types vary from one implementation to another, as
does the padding inserted between and after structure and union
members.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
The sizes of types vary from one implementation to another, as
does the padding inserted between and after structure and union
members.

Not to mention endianness issues...
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top