itoa() help

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

itoa() help

2,305件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sojiro on Wed Sep 01 07:32:33 MST 2010
Hello
I am trying to implement itoa() function 
i use the one wikipedia offers
http://en.wikipedia.org/wiki/Itoa

I used breakpoints and a Hard Fault shows at the
while ((n /= 10) > 0); 


Have no idea why but i need to make the conversion run

Any thoughts?
0 件の賞賛
返信
11 返答(返信)

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by igorsk on Tue Nov 22 11:47:49 MST 2011
Try strtoul().
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by BPC on Tue Nov 22 10:08:34 MST 2011
Hello Everyone,

I am trying to read an unsigned int from an EEPROM I have connected to my LPC1343 via I2C.  Using atoi(), I can't seem to get a number greater than 2^31 (which makes sense as atoi returns an int, not an unsigned int.

unsigned int my_num  = atoi((const char *)I2CSlaveBuffer);

With the above code, if the number in I2CSlaveBuffer is greater than 2^31, it gets cut off to 2^31.

What is the best way to get an unsigned int from my I2C slave buffer?  It seems atoi() isn't working for me?  Or is there a typecasting I can do to use atoi()?

Thanks again.
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sojiro on Mon Sep 06 14:46:03 MST 2010
yeap you are right my friend
I should stop thinking as i did at 8bit mC


:)
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Luis Digital on Sun Sep 05 07:09:37 MST 2010
Hello,

I am a newbie like you, but I think that:

90 x 90 = 8100 (int32/long)

long = 4 bytes

8100 x 4 = 32400 bytes

Define array1 using smaller type or make a smaller array, if possible for your application.
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sojiro on Sun Sep 05 01:39:27 MST 2010
It turns out that, that was the problem(buffer) and when i changed it it worked.

On more thing where can i read at the users manual about memories?
If i use an
int array1 [90][90] 
the bss overflows(>32KB)
if i comment the array1 out the bss goes down to 100.

It seems a little crazy to me so if you can point me at the correct direction

0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Sep 01 14:04:11 MST 2010
[FONT=Arial]1. Your buffer is no buffer, it's a char :confused: !!! Itoa() is trying to convert an integer to an ascii string :D[/FONT]

[FONT=Arial]This is a working sample, so insert your UARTSend function and it will work with [/FONT]
[FONT=Arial]this test integer[/FONT]

[LEFT][FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]int[/COLOR][/COLOR][/B] test = 123; [COLOR=#3f7f5f][COLOR=#3f7f5f]//test integer[/COLOR][/COLOR][/FONT][/LEFT]
[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]char [/COLOR][/COLOR][/B]buffer[64]; [COLOR=#3f7f5f][COLOR=#3f7f5f]//64 byte buffer[/COLOR][/COLOR][/FONT]
 
[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]#include [/COLOR][/COLOR][/B][COLOR=#2a00ff][COLOR=#2a00ff]<string.h>[/COLOR][/COLOR][/FONT]
[LEFT][COLOR=#3f7f5f][FONT=Arial][COLOR=#3f7f5f]// reverse: reverse string s in place[/COLOR][/FONT][/COLOR][/LEFT]
[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]void[/COLOR][/COLOR][/B] [B]reverse[/B]([B][COLOR=#7f0055][COLOR=#7f0055]char[/COLOR][/COLOR][/B] s[])[/FONT]
 
[LEFT][FONT=Arial]{[/FONT][/LEFT]
 
[LEFT][FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]int[/COLOR][/COLOR][/B] i, j;[/FONT]

[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]char[/COLOR][/COLOR][/B] c;[/FONT][/LEFT]

[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]for[/COLOR][/COLOR][/B] (i = 0, j = [B][COLOR=#642880][COLOR=#642880]strlen[/COLOR][/COLOR][/B](s)-1; i<j; i++, j--) {[/FONT]
[LEFT][FONT=Arial]c = s;[/FONT][/LEFT]
[FONT=Arial]s = s[j];[/FONT]
[FONT=Arial]s[j] = c;[/FONT]

[LEFT][FONT=Arial]}[/FONT]
[LEFT][FONT=Arial]}[/FONT][/LEFT]


[COLOR=#3f7f5f][FONT=Arial][COLOR=#3f7f5f]// [U]itoa[/U]: convert n to characters in s[/COLOR][/FONT][/COLOR][/LEFT]
 
[LEFT][FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]void[/COLOR][/COLOR][/B] [B]itoa[/B]([B][COLOR=#7f0055][COLOR=#7f0055]int[/COLOR][/COLOR][/B] n, [B][COLOR=#7f0055][COLOR=#7f0055]char[/COLOR][/COLOR][/B] s[])[/FONT][/LEFT]
[FONT=Arial]{[/FONT]

[LEFT][FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]int[/COLOR][/COLOR][/B] i, sign;[/FONT]

[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]if[/COLOR][/COLOR][/B] ((sign = n) < 0) [COLOR=#3f7f5f][COLOR=#3f7f5f]// record sign[/COLOR][/COLOR][/FONT]
[FONT=Arial]n = -n; [/FONT][/LEFT]
[COLOR=#3f7f5f][FONT=Arial][COLOR=#3f7f5f]// make n positive[/COLOR][/FONT][/COLOR]
[FONT=Arial]i = 0;[/FONT]
[FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]do[/COLOR][/COLOR][/B] { [COLOR=#3f7f5f][COLOR=#3f7f5f]// generate digits in reverse order[/COLOR][/COLOR][/FONT]
[FONT=Arial]s[i++] = n % 10 + [/FONT][FONT=Arial][COLOR=#2a00ff][COLOR=#2a00ff]'0'[/COLOR][/COLOR]; [COLOR=#3f7f5f][COLOR=#3f7f5f]// get next digit[/COLOR][/COLOR][/FONT]
[FONT=Arial]} [/FONT]
[LEFT][FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]while[/COLOR][/COLOR][/B] ((n /= 10) > 0); [COLOR=#3f7f5f][COLOR=#3f7f5f]// delete it[/COLOR][/COLOR][/FONT][/LEFT]
 
[LEFT][FONT=Arial][B][COLOR=#7f0055][COLOR=#7f0055]if[/COLOR][/COLOR][/B] (sign < 0)[/FONT][/LEFT]
[FONT=Arial]s[i++] = [COLOR=#2a00ff][COLOR=#2a00ff]'-'[/COLOR][/COLOR];[/FONT]
[FONT=Arial]s = [COLOR=#2a00ff][COLOR=#2a00ff]'\0'[/COLOR][/COLOR];[/FONT]
[FONT=Arial]reverse(s);[/FONT]

[LEFT][FONT=Arial]}[/FONT]

[FONT=Arial][/FONT] 
[FONT=Arial].....[/FONT][/LEFT]
 
[LEFT][FONT=Arial]in main loop:[/FONT]
[FONT=Arial]test++;[/FONT][/LEFT]
[FONT=Arial]itoa(test,buffer);[/FONT]
[FONT=Arial]UARTSend(buffer,[B][COLOR=#642880][COLOR=#642880]strlen[/COLOR][/COLOR][/B](buffer));[/FONT]
 
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sojiro on Wed Sep 01 13:44:21 MST 2010
The entire code is a bit large so i ll show only the necessary for my problem
MCU: lpc1768
int pinakas[75][75];
char buff;

#define Vsync  (LPC_GPIO0->FIOPIN >> 9) & 0x01
#define PCLK   (LPC_GPIO0->FIOPIN >> 10) & 0x01

void image (void){

     while(Vsync==0);
            for(y=0; y<74; y++){
              for(r=0; r<74; r++){
               while(PCLK==0);
                pinakas[r][y]=(LPC_GPIO0->FIOPIN >> 2) & 0x3F;
                                  }
                                }
 ////////////////////////////////////////////////////////////////////////
            for(y=0; y<74; y++){
                for(r=0; r<74; r++){
itoa(pinakas[r][y],buff);
    UARTSend(3, buff, strlen(buff) );
    UARTSend(3, "\r\n", 2 );
                }
            }
}
int main(void)
LPC_GPIO2->FIODIR &= ~(1<<12);//other inputs are configured too and 
                                                            //not shown here
value = (LPC_GPIO2->FIOPIN >> 12) & 0x01;
if (value == 0){//when i press the button  get photo
image();
}
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Wed Sep 01 13:14:42 MST 2010
1. Use this itoa sample, it's working.

2. What are you trying to do with

data=(LPC_GPIO0->FIOPIN >> 2) & 0x06:confused:;//0_2 to 0_7 are the inputs

reading upper 6 bits should be:

data=(LPC_GPIO0->FIOPIN >> 2) & 0x3F; //0_2 to 0_7 are the inputs

3. Post your complete code, otherwise we are all guessing.
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sojiro on Wed Sep 01 12:12:28 MST 2010
As it seems i am doing something wrong...
No code works as it should
I have done the exact same thing with an 8bit AVR so i know what the results should be

I read a sensor (6 pins) and i want to output these at msTerminal

data=(LPC_GPIO0->FIOPIN >> 2) & 0x06;//0_2 to 0_7 are the inputs
buff = toAscii(data);
UARTSend(3, buff, strlen(buff) );


Am i doing something wrong? It outputs crazy things
CodeRedSupport how can i check what you said?
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed Sep 01 09:30:34 MST 2010
Well the code from wikipedia works for me !

With reference to your hard fault, I would imagine that the most likely reason is that you are corrupting the contents of the stack. For example if the character buffer you are passing to itoa() is a local, and is of insufficient size to store the number in, then you will probably corrupt the return address from your function.

Regards,
CodeRedSupport.
0 件の賞賛
返信

2,183件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Wed Sep 01 07:38:27 MST 2010
I found this function on the net some time ago:

int toAscii (char c)
{
   int n;

   if ((c >= '0') && (c <= '9'))
      n = c - '0';
   else if ((c >= 'A') && (c <= 'F'))
      n = c - 'A' + 10;
   else if ((c >= 'a') && (c <= 'f'))
      n = c - 'a' + 10;
   else
      n = 0;
   return n;
}


Renan
0 件の賞賛
返信