• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

cast an integer to be a string

Hello ,

I am currently reading a port value in embedded programing. The port value may be eg: 29 decimal . This is an integer value . I want to use SBUF to transmit this value down the serial port . I have all the code set up nfor this :

ie code :  

int i = 29 ; // ie : port value
 
TI=1;  // enable transmission

SBUF = i ; // transmit 'i'

TI=0;  // disable transmission

Transmission is at 4800 baud . i am reading the serial port with a VB application and i keep getting the 'character represenation of the integer' ( ie : ASCii table )

Is there any way i can get SBUF toi transmit the integer value '29' as a string and not an ascii representation. NOTE that i want this to be less code , fast speed , no large lookup tables ..

Thanks
0
oddbin
Asked:
oddbin
  • 4
  • 4
  • 3
  • +3
1 Solution
 
KurtVonCommented:
Well, you can use sprintf(SBUF, "%d", i) to get the numeric representation as a string.  You can also use the itoa function which is less flexible, but faster.

Basically, casting just converts the underlying data, so an integer can't be "cast" to a string.  Well, it can, but then it would be a four character string consisting of four 0 character and one character of value 29 (the order of which is dependent on your machine).
0
 
oddbinAuthor Commented:
How can you possibly use sprintf in embedded code ?? I have tried

char str[10];
int val = 12345;
itoa( val, str, 10 );

Does'nt work

0
 
honey_hamsterCommented:
Write your own 8bitinteger-to-string conversion routine:
(ascii buffer needs to be at least 4 bytes long)

void UnsignedByteToDecimalAscii( UINT8 data, char *ascii )
{
  UINT8 temp = 0;
 
  while( data >= 100 )
  {
    temp++;
    data -= 100;
  }
  *ascii++ = '0' + temp;   /* 100s digit */

  temp = 0;
  while( data >= 10 )
  {
    temp++;
    data -= 10;
  }
  *ascii++ = '0' + temp;  /* 10s digit */
  *ascii++ = '0' + data;  /* 1s digit */
  *ascii = 0;             /* termination */
}

Will convert 255 to "255", 87 to "087", 9 to "009".
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
KurtVonCommented:
A quick search of the web found several references to sprintf and embedded systems.  There is even source for sprintf so you can include it yourself.

And what do you mean by itoa didn't work.  It wasn't in the libraries or headers, or it just didn't produce valid output?
 
0
 
honey_hamsterCommented:
sprintf() can be very expensive (real-time and code-space) in embedded systems.  Some embedded compilers do not come with sprintf (or other variances of printf) support in their libraries.
'less code and fast speed' are part of the original requirements.
0
 
mirko_germanyCommented:
try this:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   int number = 12345;
   char string[25];

   itoa(number, string, 10);
   printf("integer = %d string = %s\n", number, string);
   return 0;
}

Borland C++ 5.0 Programmierhandbuch
0
 
oddbinAuthor Commented:
the code :

  int number = 12345;
  char string[25];

  itoa(number, string, 10);
  printf("integer = %d string = %s\n", number, string);

will work in a normal envirnoment , the problem been that in my embedded code , i need the data ( integer ) to be sent using the serial port. The only way of doing this is using the SBUF . Therefore the above code is no good. I have tried it on the embedded system and i am still receiving the ascii representation of the value .

0
 
KurtVonCommented:
Ah, so what type is SBUF?

If "SBUF = i;" sends teh value in i, why not just get a string and do

count = 0;
while (char[count] != 0)
  SBUF = char[count];

Is SBUF a variable representing a port address?
0
 
oddbinAuthor Commented:
SBUF is a parameter on the reg51.h header file used as part of the Keil development application. To send anything out the serial port , you must use SBUF to send it.
0
 
KurtVonCommented:
So then looping through the string setting SBUF to each character would work:

count = 0;
while (chrs[count] != 0)
{
    while (!TI_1);
    TI_1 = 0;
    SBUF = chrs[count++];
}

The Keil website also claims that sprintf is functional.  Perhaps you need to download a newer library?
0
 
TerranceCommented:
You are tranmitting '29' that mean in phyical layer
0001 1101, So your PC you received this and compare your ascii code and get the value. But you want to get the value of 2 and 9

then in your embbeded program change yuor int to char string

 a[0] = 2+'0'; 0011 0010
 a[1] = 9+'0'; 0011 1001
In PC get this two value 2 and 9 then put togetehr

0
 
jonninCommented:
transmit in bytes, leave it as an integer if you can.
123456 is 6 bytes of transmission in ascii, 7 if you have an end of string. its only a 4 byte int as binary. Much faster...  if you control the reciever, it can turn that into text to display if needed... if you dont control the reciever, depending on how complex it is consider writing your own reciever if you need the speed and time / space this will save. If you are stuck with a reciever that wants this format, see other's posts ...






0
 
mirko_germanyCommented:
at first i've a question: what means embedded? perhaps i know the german synonym, but i now don't understand what you mean by it.

what exactly do you want to send? strings or characters? if you want to send strings, use what i postet on 03/24/2003 at 02:17PM. if you want to send characters just get out the characters of the string:

#include <stdlib.h>
#include <stdio.h>

#define max 50 // how long could the string be?

void main()
{
  int number = 29, i;
  char string[max], c;

  itoa(number, string, 10);
// use this just to see the string:
//  printf("integer = %d string = %s\n", number, string);
  for(i=0;i<max;i++)
  {
    if (string[i]=='\0')
    {
      break;
    }
    c = string[i];
    // send byte
  }
}

instead of for... (till the end) you could also use:

{
  //...
  i=0;
  while(string[i]!='\0')
  {
    c = string[i];
    // send byte
    i++;
  }
}

two codes - one meaning - choose what you prefer!

mirko
0
 
TerranceCommented:
Hi Mirko
First of all embedded mean. The system running on microcontroller without the operating system.

You can send the data what ever the way but your protocol must define such a way that both trasmit and received party should able to understand what they are sending and reciving.

Now in byte you can define unto 255 or 0xFF, if you send 29 decimal receing site must identify what is the meaning of 29 is it number 29 or something else define for 29. To avaoid this confusion,The software engineers(Firmware) engineers have soem standard way of pratics that if it is number send as a single digit number and assemble to gether

so he must send 2,9 finally terminating charcter.

what is teh meaning of terminating charcter is nothing but just 0 (0000 0000)

Now you may ask another qustion how to send 0 through the cable so ASCIII was intrduce and say it define different value for each character acooring to that
 0  == 0x30(ASCII)
 1  == 0x31


9  == 0x39

NULL(terminating character)  0x00.

So any transmission between two machine better to use AScii, it is not complussary to use ascci but it is easy to use ascii.
       
0
 
mirko_germanyCommented:
"that both trasmit and received party should able to understand"

obviously his reciever cannot understand integers sent over the com-port. i think you don't link to pcs, do you? if you really link to pcs ofcourse you also could send the integers, but i understood, that you want to link a pc with another (selfbuilt) hardware.

@oddbin: have you tested my code? i think it will split your 29 to '2' (in ascii: hex:32=dez:050=bin:00110010) and '9'(39=057=00111001). and this are to lonly bytes (characters) you can send to your hardware. then the following bits will leave your computer: 00110010 00111001 + stopbits, paritybits etc.

please answer, when you tried it

mirko
0
 
oddbinAuthor Commented:
Thank sfor everyone's answers , points must go to " KurtVon " because he was correct on "printf". I just needed the stdio header file for its operation.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now