473,326 Members | 2,095 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,326 software developers and data experts.

Stripping Hex and then ASCII

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
Nov 14 '05 #1
6 1966
In 'comp.lang.c', es****@my-deja.com (Colin Green) wrote:
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

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #2
Colin Green wrote:
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.
Nov 14 '05 #3
In 'comp.lang.c', Drew MacDonald <dr*********@hotmail.com> wrote:
Hi this is for Serial Com.
<...> 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.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #4
Emmanuel Delahaye wrote:
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.
Nov 14 '05 #5
Drew MacDonald <dr*********@hotmail.com> writes:
Emmanuel Delahaye wrote:
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)?


The sizes of types vary from one implementation to another, as
does the padding inserted between and after structure and union
members.
--
Just another C hacker.
Nov 14 '05 #6
In 'comp.lang.c', Ben Pfaff <bl*@cs.stanford.edu> wrote:
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)?


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...

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7

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

Similar topics

3
by: Robert Oschler | last post by:
Hello, I am using the following function to try and strip both carraige returns and line feeds, ASCII 13 and 10 respectively, from a string. It doesn't seem to be working: x = filter(lambda...
2
by: Patrick | last post by:
Hello, after learning that I was taking a class in VB.NET, I have been drafted to solve all my companies VB/scripting problems - hey, I should know everything; I've already taken 6 classes ;) I...
2
by: David Pratt | last post by:
I am working with a text format that advises to strip any ascii control characters (0 - 30) as part of parsing data and also the ascii pipe character (124) from the data. I think many of these...
10
by: S | last post by:
I need to strip some ASCII from a string. I am used to the VB ASC and CHR methods but not sure in C#, Any help?
4
by: Lu | last post by:
Hi, i am currently working on ASP.Net v1.0 and is encountering the following problem. In javascript, I'm passing in: "somepage.aspx?QSParameter=<RowID>Chèques</RowID>" as part of the query...
7
by: Edward Elliott | last post by:
I'm looking for the "best" way to strip a large set of chars from a filename string (my definition of best usually means succinct and readable). I only want to allow alphanumeric chars, dashes,...
7
by: FFMG | last post by:
Hi, I have a form that allows users to comment, add entries and so on. But what a lot of them do is copy and paste directly from MS Word to my forms. almost all browsers will accept the post...
11
by: Lo'oris | last post by:
I'd like to have a set of "allowed characters", and strip a string from everything besides those. I've tried and tried but so far every time I enter strings containing unicode, it goes mad and...
4
by: Tim Cook | last post by:
Hi All, I just ran into an issue with the rstrip method when using it on path strings. When executing a function I have a need to strip off a portion of the current working directory and add...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.