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
  • 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.

Reply
  • 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.

Children
No data