?
Solved

cast an integer to be a string

Posted on 2003-03-24
16
Medium Priority
?
545 Views
Last Modified: 2013-12-14
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
Comment
Question by:oddbin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +3
16 Comments
 
LVL 11

Accepted Solution

by:
KurtVon earned 75 total points
ID: 8196386
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
 

Author Comment

by:oddbin
ID: 8196672
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
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8196725
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:KurtVon
ID: 8197151
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
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8197304
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
 

Expert Comment

by:mirko_germany
ID: 8198553
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
 

Author Comment

by:oddbin
ID: 8201651
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
 
LVL 11

Expert Comment

by:KurtVon
ID: 8202797
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
 

Author Comment

by:oddbin
ID: 8204038
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
 
LVL 11

Expert Comment

by:KurtVon
ID: 8204579
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
 

Expert Comment

by:Terrance
ID: 8208244
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
 
LVL 2

Expert Comment

by:jonnin
ID: 8209969
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
 

Expert Comment

by:mirko_germany
ID: 8219189
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
 

Expert Comment

by:Terrance
ID: 8221941
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
 

Expert Comment

by:mirko_germany
ID: 8226655
"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
 

Author Comment

by:oddbin
ID: 8238015
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question