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 support@ccsinfo.com

String Ascii to int

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

String Ascii to int
PostPosted: Tue Jan 29, 2013 4:50 pm     Reply with quote

Hi, some one can help me...

I have a string ascii:
Code:
Buffer[0] = 0x31;
Buffer[1] = 0x31;
Buffer[2] = 0x33;
Buffer[3] = 0x39;


I should I do to get the equivalent: 4409 decimal?, I need this to do math
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 6:14 pm     Reply with quote

There are 'C' functions for this, ATOI(), ATOL(), ATOI32.

Take your pick.

Mike

EDIT Your ASCII values convert to 1139 !
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 6:17 pm     Reply with quote

Embarassed Maybe I misspoke, I'm getting for the Uart the Ascii characters 1,1,3 and 9, representing a hexadecimal number 0x1139, these characters must be equivalent to 4409 in decimal value, my problem is that I have the any idea how to make their equivalence ... :
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 6:30 pm     Reply with quote

To do it by hand:-

1) Convert first digit from HEX to decimal. //1
2) Multiply by 16. // 16
3) Convert second digit to HEX. // 1
4) Add results from 2 and 3 together. // 17
5) Multiply new result by 16. // 272
6) Continue until you run out of digits.


Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 7:03 pm     Reply with quote

Here is a 16-bit version of a function that I used to use for this:
Code:

#include <18F4520.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#include <ctype.h>

//-------------------------------------------------
// Ascii to unsigned long -
// Converts a string of 1 to 4 ascii hex characters to an
// unsigned long (16-bit) value.

int16 axtoul(char *ptr)
{
char i, temp;
int16 result;

result = 0;

for(i = 0; i < 4; i++)           // Convert a maximum of 4 hex digits
   {
    temp = toupper(*ptr++);      // Get the char.  Convert to upper case
    if(isxdigit(temp) == FALSE)  // Is ascii char a hex digit ?
       break;         // Break if not
    temp -= '0';      // Convert the char to binary
    if(temp > 9)
       temp -= 7;
    result <<= 4;      // shift existing value left 1 nybble
    result |= temp;    // Then combine it with new nybble
   }

return(result);
}

//================================
void main(void)
{
int16 result;
int8 buffer[] = "1139";   // Hex value

result = axtoul(buffer);
printf("result = %lu \r", result);


while(1);
}
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Tue Jan 29, 2013 7:16 pm     Reply with quote

Very Happy Thank you....
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Jan 30, 2013 7:39 am     Reply with quote

not to be a nit picker but if it is really a string , i would include
Code:

Buffer[4] = 0;


the way you want to handle it, "buffer " does technically NOT contain a "string" at all, unless every instance is limited to 4 bytes by code.

just my 2 cents
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