Split a Byte Array

This is a discussion on Split a Byte Array within the C Programming forums, part of the General Programming Boards category; Hi, I have a datapacket that is a byte array, which I want to split into different strings and then ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    Question Split a Byte Array

    Hi,

    I have a datapacket that is a byte array,
    which I want to split into different strings and then convert to float or integer values.

    Here is the sample of a packet 178.50|125|1070 where | is the delimiter.

    This is for ARM compiler.

    Can anybody help please.

    Thanks in advance

    Thomas George

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    You could possibly use sscanf() to process this byte array.

    Jim

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    man page strtod section 3
    man page strtol section 3
    Will naturally stop at any delimiter, and also update a pointer to where it stopped converting the string.

    This makes it very easy to walk the length of a string without any prior need to tokenise the string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you don't know the order of the data, you can't use sscanf or Salem's method.

    I thought this would be a good way to get my brain working this morning, so I wrote a little "state machine" parser. It might seem like overkill or it might be something with some potential depending on the details of the task.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    enum {
    	ERR,
    	INT,
    	FLOAT
    };
    
    typedef struct {
    	union {
    		int i;
    		float f;
    	} value;
    	int type;
    } Number;
    
    #define MAX 8
    
    void getValue (char *raw, int type, Number *data) {
    	if (type == INT) data->value.i = atoi(raw);
    	else if (type == FLOAT) data->value.f = strtof(raw, NULL);
    	data->type = type;
    }
    
    int main(void) {
    	char in[] = "178.50|125|1070", *p = in, *cur = NULL;
    	int state = INT, count = 0, i;
    	Number data[MAX];
    
    	while (*p && count < MAX) {
    		if (*p == '|') {
    			*p = '\0';
    			if (cur) {
    				getValue (cur, state, &data[count++]);
    				cur = NULL;
    				state = INT;
    			}
    		} else if (*p == '.') {
    			if (state == FLOAT || state == ERR) state = ERR;
    			else state = FLOAT;
    		} else if (*p >= '0' && *p <= '9') {
    			if (!cur) cur = p;
    		} else state = ERR;
    		p++;
    	}
    	if (cur  && count < MAX) getValue (cur, state, &data[count++]);
    
    // output
    	for (i = 0; i < count; i++) {
    		switch (data[i].type) {
    			case ERR:
    				printf("Bad value in packet!\n");
    				break;
    			case INT:
    				printf("Integer: %d\n", data[i].value.i);
    				break;
    			case FLOAT:
    				printf("Float: %f\n", data[i].value.f);
    				break;
    			default: 
    				printf("WTF?\n");
    		}
    	}
    
    	return 0;
    }
    Last edited by MK27; 08-28-2011 at 08:56 AM. Reason: bounds check line 47
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Thank you all. it worked. MK27 your code very useful for ref.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Split a textfile in an array
    By peterderijp in forum C Programming
    Replies: 15
    Last Post: 10-11-2010, 01:59 PM
  2. Howto? split and use a char array
    By djnicovski in forum C++ Programming
    Replies: 18
    Last Post: 07-23-2010, 11:18 PM
  3. Split up an Array
    By shanem in forum C Programming
    Replies: 6
    Last Post: 01-22-2009, 11:05 AM
  4. Sorting an array alphabetically - split
    By wurzull in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 11:17 PM
  5. Replies: 10
    Last Post: 10-11-2004, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21