int to char array?

This is a discussion on int to char array? within the C Programming forums, part of the General Programming Boards category; What is the best way to go about converting a 4-byte integer into a char array such that each element ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    int to char array?

    What is the best way to go about converting a 4-byte integer into a char array such that each element of the array contains a byte from the integer?

    For example: I have an integer, 56, which looks like this in memory: 0x00 0x00 0x00 0x38. I want to convert that integer to an array of chars, with array[0] = 0x00 ... array[3] = 0x38.

    I've tried memcpy() to copy the memory directly from the integer, and it works to an extent, but it doesn't seem to like int values over 128 (max size for a char), which makes me think that memcpy() is trying to copy the entire int into the first element of the array, which is obviously not what I want.

    I've also tried using a char pointer to access each byte of the integer individually, but I get the same problem.

    Any suggestions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe you could post what you have tried. You certainly CAN copy an integer to a char array. Also note that values over 128 will be negative in an array of signed char.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What happens with values over 128?
    Shouldn't you use an unsigned char* buffer instead?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Better try using unsigned char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    9
    The code I'm working with is this:

    Code:
    int number = 500;
    char charArray[4];
    bzero( charArray, 4 );
    memcpy( charArray, &number, 4 );
    
    int i;
    for( i = 0; i < 4; ++i ) {
    
        printf( "charArray[&#37;d]: %02X\n" );
    }
    I get this as output:

    00 00 01 FFFFFFF4

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    9
    I'm trying to find the emoticon that shows me slapping myself upside the head.

    Using an unsigned char buffer fixed the problem. Thanks everyone.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's not your actual code, right? Because that would print random rubbish, since there's no arguments for printf.

    Yes, that's because %x shows the number as an unsigned, and char values passed to printf are converted to int before they are passed along. You can, as suggested, use unsigned char, or you could do something like this:

    Code:
     printf( "charArray[%d]: %02X\n", i,  ((int)charArray[i]) & 0xff);
    And it's completely unnecessary to use bzero() immediately before memcpy(), since memcpy() will overwrite your zero's with the value from the integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    9
    Whoops, just forgot to add the args when typing it (it's not actual code, just the general idea of what I was doing).

    Good point about bzero(). Not sure what I was thinking there.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Note that you could achieve the same affect as the bzero call like this.
    Code:
    char charArray[4] = {0};
    Assuming you needed it, of course.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  3. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21