Thread: long char?

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007

    Thumbs up long char?

    I want to convert an int to a char, and then put it into a file but it might be over 255. I also want to retrieve the number later.

    I created a program (a really lengthy one) and it always crashes because I think it's because an unsigned char can only store up to 255. Similar to an int, is there something like a long for a char?


  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Why not keep it as an int? Are you using strings or something?

    You could use more than one char to store the int. Say, something like this (dealing with negative numbers is a pain, so I used unsigned ints):
    #include <climits>
    char buffer[SIZE];
    int x, number = whatever();
    for(x = 0; x < sizeof(unsigned); x ++) {
        buffer[x] = number & UCHAR_MAX;
        number >>= CHAR_BIT;
    To answer your question, there is no long char. Just shorts and ints etc.
    Last edited by dwks; 01-15-2008 at 04:55 PM.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Typically you just cast a pointer to the int into a pointer to char, and then write out sizeof(int) of them. Similiar for reading back in.
    Assuming you don't need to run on platforms of the other endianness of course.
    The common way nowdays is to just convert everything to text, or even write it out as xml.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM