This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

type conversion problem

I want to convert characters recieved from MatLab into unsigned or signed integers in Keil... how do I do that?

Parents
  • "My fellow group members will not send me characters but they will send me integer values"

    The 8051 serial interface (and a PC's COM: port) handles data a byte at a time. Therefore, you need to know:

    1. How many bytes make up an "integer?!

    2. If your "integers" are more than 1 byte, in what order are those bytes sent?

Reply
  • "My fellow group members will not send me characters but they will send me integer values"

    The 8051 serial interface (and a PC's COM: port) handles data a byte at a time. Therefore, you need to know:

    1. How many bytes make up an "integer?!

    2. If your "integers" are more than 1 byte, in what order are those bytes sent?

Children
  • scanf() converts ASCII to binary. If the producer program is sending you binary integers, you do not need scanf(). Instead, you would just receive the data directly into your destination integer.

    You might use a C union, with a byte array overlaying an integer. Or just use pointer arithmetic.

        U16 myInt;
        U8* nextByte;
    
        len = 0;
        nextByte = &myInt;
        while (len < sizeof(myInt))
            {
            *nextByte++ = WaitForSerialChar();
            ++len;
            }
    

    You're likely to want the ability to abort the routine on a timeout, and other such improvements for a serious application.

    C51 stores integers in big-endian format. If the transmitter sends little-endian, you'll have to reverse the order in which you fill the destination.