Thread: Casting a 32-bit int as a char array

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    Casting a 32-bit int as a char array

    Hi all,

    I am writing C code for an embedded Atmel ATmega128 cpu using WinAVR 20090313 and Ethernut 4.8.5. These provide most of C99 as far as I know.

    I have a function that sends data to an SPI bus:

    Code:
    int spiTransmit(char *buf, int len) {
         ......
    }
    where *buf points to a string I wish to transmit, and len is the number of characters to send.

    I have a 32-bit int:
    Code:
    uint32_t my_int = 0xf0e32967;
    I want to send 8 bits at a time using spiTransmit(). My first attempt was

    Code:
    spiTransmit( (char*)&my_int, 4);
    but this outputs 0x6729e3f0 (flipping the byte order in my_int).

    To get the correct byte order I could do a bunch of bitwise and/shift/ors on my_int, flipping the byte order before sending it through to spiTransmit, but that seems like an ugly way to solve my problem.

    Thanks for your time. Any suggestions?

    (ps. For the curious, the SPI is feeding configuration data into an Analog Devices ADF4001 PLL chip. The data involves various integers (14 bits, 13 bits) and control words, spliced together in an arbitrary pattern. I actually need to send 24, not 32, bits at a time - but I can handle that part.)

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    I think you'll have to flip them...check out: Endianness - Wikipedia, the free encyclopedia

    Here's an example of how to flip:

    Code:
    uint32_t bigEndian_uint32(uint32_t a)
    {
    	unsigned int tmp = 1;
    	if (*(unsigned char *)&tmp) // returns 1 if little-endian
    		return a << 24 & 0xff000000 | a << 8 & 0x00ff0000 | a >> 8 & 0x0000ff00 | a >> 24 & 0x000000ff;
    	else
    		return a;
    }

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    2
    Great, thanks very much - sort of what I was thinking of doing, but much nicer than I thought it could be. I also like the endian test.

    As a new user I'm really impressed!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's say n = 88, and ch = n.

    Although the size will differ, the hex value of n will be the same as ch. Char's are just small int's, with a very small range. Both n(88) and ch(X) equal 58 in hex. All the other bits in n are 0.

    Your transmit function may be swapping bits in preparation for transmitting, but that's just a guess.

    The sprint() function can be used to put integers into a string as char's, or as hex, etc., as you wish. Better than a series of bit swapping, and the slick way you were looking for, perhaps.

    Code:
    #include <stdio.h>
    
    int main() {
      int n = 88;
      char ch;
    
      //ch = n - '0'; <<== Not what you want
      ch = (char) n; //or just ch = n;
      printf("\nN = %d, Ch = %c, hexN = %x, hexCh = %x", n, ch, n, ch);
      printf("\nSizeof(N) = %d, Sizeof(ch) = %d", sizeof n, sizeof ch);
    
    
      printf("\n\n\t\t\t     press enter when ready");
      n = getchar();
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM