CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Invert order/position of char on string??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

Invert order/position of char on string??
PostPosted: Mon Jul 04, 2005 10:01 pm     Reply with quote

Hi

I need invert the char´s order on a string, like this:

Code:

string_1[]="ABCDEFGHIJ"


i need invert it to

Code:

string_2[]="BADCFEHGJI"



I dont know how make it ..

Thank you


Last edited by densimitre on Wed Jul 06, 2005 4:02 pm; edited 1 time in total
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Jul 04, 2005 10:10 pm     Reply with quote

Code:

int i,j;
j = strlen(string_1);
string_2[j--] = 0;
for (i = 0; i < strlen(string_1); i++, j--)
        string_2[j] = string_1[i];

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 05, 2005 1:09 am     Reply with quote

asmallri wrote:
Code:

int i,j;
j = strlen(string_1);
string_2[j--] = 0;
for (i = 0; i < strlen(string_1); i++, j--)
        string_2[j] = string_1[i];
This routine will invert the whole string to 'JIHGFEDCBA', it will not reverse just the pairs of characters as indicated by Densimitre.

I suggest something like:
Code:
int8 i, len;

i = 0;
len = strlen(string_1);
while (i < len)
{
  string_2[i]   = string_1[i+1];
  string_2[i+1] = string_1[i];
  i += 2;
}
if (len % 2)   // Special case handling for string with odd number of characters
{
  string_2[len - 1] := string_1[len - 1];
}
string_2[len] = 0;
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Tue Jul 05, 2005 4:40 pm     Reply with quote

Thank you very much

If i have something like:

Code:

string_1[]="12345678]


and i want take 2 char together (12 , 34 , 56 , 78) and invert them to (21, 43,65,87) and grouping them but using only 1 byte in HEX format for each pair of numbers??


Thank you
valemike
Guest







PostPosted: Tue Jul 05, 2005 6:07 pm     Reply with quote

Starting at x=0,

look at char[x];
subtract 0x30 from it to get the units value.
look at char[x+1];
subtract 0x30 from it to get the tens value, and multiply that by 10.

This is your two-digit value. Add the two previous numbers you just finished computing.

Keep doing this til you get to the end of the array. And don't forget to increment your iterator x by 2 each time.

You could use some of those bcd or atoi or viceversa functions, but i forgot how to use them.
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Tue Jul 05, 2005 11:02 pm     Reply with quote

Hi
Mmmmmm.....

Considering than ascii chart from 1 to 9 are 0x01 to 0x09 in hex format, I think something like this:

Code:


char_source[]="1,2,3,4,5,6,7,8";  // I need only numbers (does char_source contain a NULL char at end ????
char_dest[4];                     // I need pack 2 ascii digit into 1 hex byte
int i=0;
while(i<=strlen(char_source) {

char_dest[i]= (char_source[i+1]<<4) || char_source[i]);
i=i+2;

}



the result must be:

char_source[]={0x21,0x43,0x65,0x87}

after this, i need printf char_source in hex format...


Thank you very much...
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Wed Jul 06, 2005 11:28 am     Reply with quote

Hi

I add this code to print resukt in hex format:

Code:
for (i=0;i<4;i++) {
printf("%X",char_dest[i]);
putc('\r');
putc('\n');
}


But the result is:

B9
B2
39
79


Mi code above ist not working....why?

Thankz

The result
valemike
Guest







PostPosted: Wed Jul 06, 2005 12:01 pm     Reply with quote

I wrote the code in a Unix machine. Use it as a reference. It's up to you to port it to the PIC, and up to you to code a loop to go thru to the end of the string:

Code:

"ccs.c" 20 lines, 408 characters
hawk:/usr2/student/v/valemic1>!gcc
gcc ccs.c -o ccs
ccs.c: In function `main':
ccs.c:5: warning: return type of `main' is not `int'
hawk:/usr2/student/v/valemic1>./ccs
1
2
my_number = 21
hawk:/usr2/student/v/valemic1>cat ccs.c
#include <stdio.h>
#include <unistd.h>

void main(void)
{
    char mike[] = "123456789";
    char *mike_ptr;
    char *mike_ptr2;
    int my_number;

    mike_ptr = &mike[0];
    mike_ptr2 = &mike[1];

    printf ("%c\n", *mike_ptr);
    printf ("%c\n", *mike_ptr2);

    // Now generate an int.
    my_number = (*mike_ptr2 - 0x30) * 10 + (*mike_ptr - 0x30);
    printf ("my_number = %d\n", my_number);
}
hawk:/usr2/student/v/valemic1>./ccs
1
2
my_number = 21
hawk:/usr2/student/v/valemic1>
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jul 06, 2005 4:39 pm     Reply with quote

densimitre wrote:
Hi

I add this code to print resukt in hex format:

Code:
for (i=0;i<4;i++) {
printf("%X",char_dest[i]);
putc('\r');
putc('\n');
}


But the result is:

B9
B2
39
79


Mi code above ist not working....why?

Thankz
Several reasons:
1)
Quote:
Considering than ascii chart from 1 to 9 are 0x01 to 0x09 in hex format, I think something like this:
The numbers 1 to 9 in the ASCII chart have a hex value of 0x30 to 0x39. Convert the ASCII digit to an integer digit by subtracting 0x30, or for better readability subtract by '0'.

2)
Code:
char_source[]="1,2,3,4,5,6,7,8";  // I need only numbers (does char_source contain a NULL char at end ????
This sequence also includes the ',' characters, you don't want to convert those.
Yes, the string contains a NULL character as well (but the returnvalue of strlen() does not include this).

3)
Code:
while(i<=strlen(char_source)
This loop is iterating one time too many and is dangerous because it makes your code overwrite RAM after the end of the Dest[] array.
Also, calling strlen() in the while loop is not very efficient. The length of the string doesn't change, so you get a better performance by calling it just once before entering the loop.

4)
Code:
char_dest[i]= (char_source[i+1]<<4) || char_source[i]);
i=i+2;
You are increasing pointer i by 2, but for the destination string the pointer should be increased by just 1. This can be solved by introducing a second pointer.


This is how I changed your code:
Code:
#include <string.h>
void main(void)
{
  char Source[] = "12345678";       // I need only numbers (does char_source contain a NULL char at end ????
  char Dest[4];                     // I need pack 2 ascii digit into 1 hex byte
  int8 i,j;
  int8 Len;
 
  i = 0;
  j = 0;
  Len = strlen(Source);
  while (i<Len)
  {
    Dest[j] = ((Source[i+1] - '0') * 10) + (Source[i] - '0');
    i += 2;
    j++;
  }


  for (i=0; i<4; i++)
    printf("%X\r\n", Dest[i]);

  while(1);      // Prevent the program from executing the sleep
                 // instruction at the program end or the printf
                 // will not get enough time to output all data.
}


And it will print:
0x15 (decimal 21)
0x2B (decimal 43)
0x41 (decimal 65)
0x57 (decimal 87)

Is this the output you expected? Or did you intend the input string to be hexadecimal instead of decimal?
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Wed Jul 06, 2005 8:03 pm     Reply with quote

Hi

mi new code:

Code:

#include <16f648a.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOLVP
#use delay(clock=4000000)
#use RS232(BAUD=9600, BITS=8, PARITY=N, XMIT=PIN_B2, RCV=PIN_B1, stream=r520,errors)
#include <string.h>

char char_source[]="12345678";
char char_dest[4];

void main(void) {

int i=0, j=0;

printf("String_Source: %s\n\r",char_source);

while(i<4) {

//while(i<=strlen(char_source)) {


char_dest[i]=  (char_source[j+1] -0x30)*10 + (char_source[j] -0x30) ;
printf("%d",char_dest[i]);
i=i+1;
j+=2;

}
}




The result is:

Code:

Source_dest: 21436587

MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Wed Jul 06, 2005 9:39 pm     Reply with quote

Are you asking if it is correct or not? Question

Yes and no.
Yes: The computed results are correct.
No: You forgot to put the "\r\n". Of course it's going to be a big number Rolling Eyes

Out of curiosity, what application is this for?
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Wed Jul 06, 2005 10:38 pm     Reply with quote

Ok

Well, first thank to all of you for great help...WOW !!!!!

My project is send a SMS in PDU mode, with CSA =0x00 (CSA data storen in phone).....In the destination number, if type number is 0xA1 (national/idsn), the leng of number is 8, but showed in 4 byte in hex format: here is my issue....the number is inverted.....92347583 must be: 29435738.....I can put it in the frame in that order, but the idea is a changing dest number by choices.......

I dont still try in my PIC, but a will stay inform toy you about it..

Thank you
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Thu Jul 07, 2005 9:38 pm     Reply with quote

Hi

I found this code

Code:

void hexout(byte daten)
{
   byte help;

   help=daten>>4;            // das höherwertige Nibble

   if(help > 9)
      help+=7;

   printf("%c", help + '0');

   help=daten & 0x0F;         // das niederwertige Nibble

   if(help > 9)
      help+=7;

   printf("%c", help + '0');
}




I need use it with a string as argument, in order to hexout a full string, to past it in the user data field of sms

Bye
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 2:11 am     Reply with quote

Quote:
Code:

void hexout(byte daten)
{
   byte help;

   help=daten>>4;            // das höherwertige Nibble

   if(help > 9)
      help+=7;

   printf("%c", help + '0');

   help=daten & 0x0F;         // das niederwertige Nibble

   if(help > 9)
      help+=7;

   printf("%c", help + '0');
}

This whole function can be simplified to:
Code:
void hexout(byte data)
{
  printf("%2X", data);
}


Quote:
I need use it with a string as argument, in order to hexout a full string, to past it in the user data field of sms
I don't understand this, can you be a bit more specific? What is your question?

Higher up in this thread you already created a function for integer_string[8] --> bcd[4].
The function you are giving us now is doing the reverse for a single byte. Is that what you want, but now for a whole string?
Is the string zero terminated?
Has the string a fixed length of 4 like in your previous code?
Do the digits have to be swapped like in your previous code?

The binary format you are converting to is commonly known as BCD (Binary Coded Digit), searching this forum I'm sure you will find several conversion routines.
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 8:09 am     Reply with quote

ckielstra wrote:
Quote:
Code:

void hexout(byte daten)
{
   byte help;

   help=daten>>4;            // das höherwertige Nibble

   if(help > 9)
      help+=7;

   printf("%c", help + '0');

   help=daten & 0x0F;         // das niederwertige Nibble

   if(help > 9)
      help+=7;

   printf("%c", help + '0');
}

This whole function can be simplified to:
Code:
void hexout(byte data)
{
  printf("%2X", data);
}


Quote:
I need use it with a string as argument, in order to hexout a full string, to past it in the user data field of sms
I don't understand this, can you be a bit more specific? What is your question?

Higher up in this thread you already created a function for integer_string[8] --> bcd[4].
The function you are giving us now is doing the reverse for a single byte. Is that what you want, but now for a whole string?
Is the string zero terminated?
Has the string a fixed length of 4 like in your previous code?
Do the digits have to be swapped like in your previous code?

The binary format you are converting to is commonly known as BCD (Binary Coded Digit), searching this forum I'm sure you will find several conversion routines.




ckielstra, master, you are right:

I made this code, and ist working for me:

Code:

char char_text[]="hellohello"
int len;i;
void main() {
len=strlen(char_text);
printf("%d\n\r",len);    //10 fot this test
for (i=0;i<len;i++) {
printf("%2X",char_text[i]);
}
}


The output is: 68656C6C6F68656C6F6F.

Thats is i need, thank you so much....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group