long char?

This is a discussion on long char? within the C++ Programming forums, part of the General Programming Boards category; I want to convert an int to a char , and then put it into a file but it might ...

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

    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?

    thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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):
    Code:
    #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 03:55 PM.
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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, 04: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

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