split strings by 2 ASCII Character

This is a discussion on split strings by 2 ASCII Character within the C Programming forums, part of the General Programming Boards category; Hi guys, I want to split strings that are not separated by tab, space or etc into 2 characters that ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    split strings by 2 ASCII Character

    Hi guys,

    I want to split strings that are not separated by tab, space or etc into 2 characters that will stored in array. The inputs are read from Srecords file.


    example input data

    Data [] = {"121AB2CD"}

    and will be store like this,

    Data [0]: 12 -> 18
    Data [1]: AB -> 26
    Data [2]: 2C -> 178
    Data [3]: CD -> 205

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    
    int main(void)
    {
    
    Char st[10][20];
    int temp[10];
    int i,n=4;
    
    
    printf("Please input ant ASCII no.:");
    for(i=0; i<n; i++)
    gets(st[i]); 
    
    
    for(i=0; i<n; i++)
    {
    sscanf(temp, "%X", st[i]);
    printf("%X\n", st[i]);
    
    }
    
    
    }

    the main purpose i want to split this strings is because i have to Convert 2 ASCII character to be 1 byte of hex data. the problem is i don't know how to split strings coz currently i'm asking user to key in input.. Plz guide me cuz i really need help...

  2. #2
    Registered User
    Join Date
    Jan 2010
    Location
    Germany, Hannover
    Posts
    15
    hm, i didn't know a function for this, so i wrote my own
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* converts string of valid hex chars ('XX' : '0'-'9','A'-'F' )
    to 1 byte char array with corresponding hex values
    */
    void str2hex(char* s, char* d){
      unsigned char c=0;
      int shift=0;
      do {
        c+= (*s>'9')?  *s-55:*s-48; // input must be uppercase !
        if (++shift%2)
          c<<=4;//first shift to left (upper half byte)
        else 
          *d++=c,   c=0; //then move to destination char
      } while(*++s);//unil zero byte of source string is reached
      *d=0; // add 0 byte to dest string
    }
    int main(int argc, char *argv[]) {
      char input[]="414243";
      char output[sizeof(input)/2+1];
      str2hex(input,output);
      printf("%s ",output);
      system("PAUSE");	
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing a circle with ASCII character
    By bnkslo in forum C Programming
    Replies: 15
    Last Post: 04-03-2008, 12:56 PM
  2. Character strings
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 04:26 PM
  3. character strings
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 06-01-2004, 12:24 PM
  4. Moving ASCII Character
    By Ranedhel in forum Game Programming
    Replies: 10
    Last Post: 07-17-2003, 06:58 AM
  5. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM

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