Neophile
Neophile

Reputation: 5880

How to read in only a particular number of characters

I have a small query regarding reading a set of characters from a structure. For example: A particular variable contains a value "3242C976*32" (char - type). How can I get only the first 8 bits of this variable. Kindly help.

Thanks.

Edit: I'm trying to read in a signal:

For Ex: $ASWEER,2,X:3242C976*32

into this structure:

struct pg
    {
        char command[7];  // saves as $ASWEER,2,X:3242C976*32
        char comma1[1];  // saves as ,2,X:3242C976*32
        char groupID[1]; // saves as 2,X:3242C976*32
        char comma2[1]; // etc
        char handle[2]; // this is the problem, need it to save specifically each part, buts its not
        char canID[8];
        char checksum[3];
    }m_pg;

... When memcopying buffer into a structure, it works but because there is no carriage returns it saves the rest of the signal in each char variable. So, there is always garbage at the end.

Upvotes: 1

Views: 287

Answers (5)

chmeee
chmeee

Reputation: 3638

It looks to me like you want to split at the comma, and save up to there. This can be done with strtok(), to split the string into tokens based on the comma, or strchr() to find the comma, and strcpy() to copy the string up to the comma.

Upvotes: 1

Michael
Michael

Reputation: 688

you could.. convert your hex value in canID to float(depending on how you want to display it), e.g.

float value1 = HexToFloat(m_pg.canID); // find a conversion script for HexToFloat

CString val;
val.Format("0.3f",value1);

the garbage values aren't actually being stored in the structure, it only displays it as so, as there is no carriage return, so format the message however you want to and display it using the CString val;

Upvotes: 3

pmg
pmg

Reputation: 108986

After the edit (original answer below)

Just copy by parts. In C, something like this should work (could also work in C++ but may not be idiomatic)

strncpy(m_pg.command, value, 7); // m.pg_command[7] = 0; // oops
strncpy(m_pg.comma, value+7, 1); // m.pg_comma[1] = 0; // oops
strncpy(m_pg.groupID, value+8, 1); // m.pg_groupID[1] = 0; // oops
strncpy(m_pg.comma2, value+9, 1); // m.pg_comma2[1] = 0; // oops
// etc

Also, you don't have space for the string terminator in the members of the structure (therefore the oopses above). They are NOT strings. Do not printf them!


Don't read more than 8 characters. In C, something like

char value[9]; /* 8 characters and a 0 terminator */
int ch;
scanf("%8s", value);

/* optionally ignore further input */
while (((ch = getchar()) != '\n') && (ch != EOF)) /* void */;
/* input terminated with ch (either '\n' or EOF) */

I believe the above code also "works" in C++, but it may not be idiomatic in that language

Upvotes: 2

Daniel
Daniel

Reputation: 6775

If you have a char pointer, you can just set str[8] = '\0'; Be careful though, because if the buffer is less than 8 (EDIT: 9) bytes, this could cause problems.

(I'm just assuming that the name of the variable that already is holding the string is called str. Substitute the name of your variable.)

Upvotes: 1

John Humphreys
John Humphreys

Reputation: 39354

If "3242C976*3F" is a c-string or std::string, you can just do:

char* str = "3242C976*3F";
char first_byte = str[0];

Or with an arbitrary memory block you can do:

SomeStruct memoryBlock;
char firstByte;
memcpy(&firstByte, &memoryBlock, 1);

Both copy the first 8bits or 1 byte from the string or arbitrary memory block just as well.

Upvotes: 2

Related Questions