long to string algor
FedoraForum.org - Fedora Support Forums and Community
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 21
  1. #1
    LackeyLad Guest

    long to string algor

    anyone know an easy way to convert long int to string in c++ rather simply?

  2. #2
    Join Date
    Aug 2004
    Posts
    4,082
    If you are talking about a genuine string (from the C++ <string> class), the man himself tells us in the page:
    http://www.research.att.com/~bs/bs_f...#int-to-string

    It worries me that his function seems to create a new sstream class on every call to the conversion function, but thats C++ for you.

    If you meant an array of characters, you can think in C and use
    sprintf(&myarray[0],"%d", mylong);

    sprintf is one of the most dangerous c functions to use because you can get strange errors if your array is not long enough to hold all the digits of the integer.
    Last edited by tashirosgt; 13th October 2004 at 04:29 PM.
    "Never let the task you are trying to accomplish distract you from the study of computers."

  3. #3
    LackeyLad Guest
    i need to make the following func work properly

    static string long2string(long longNum)
    {

    string result;
    char temp;

    temp = longNum & 0xF;

    result += temp;

    return result;
    }

  4. #4
    Join Date
    Aug 2004
    Posts
    4,082
    I am unable to tell from this code, what it would mean for the function to work properly. Do you have some simple examples of the desired output from given inputs?

    Edit: One thing I notice is that
    temp = longNum & 0xF;
    will make temp a character between 0 and 15 and these characters are not printable.
    Last edited by tashirosgt; 13th October 2004 at 09:09 PM.
    "Never let the task you are trying to accomplish distract you from the study of computers."

  5. #5
    Join Date
    Feb 2004
    Location
    Seattle, WA, USA
    Age
    62
    Posts
    3,423
    Granted, my C/C++ programming is rather rusty, but couldn't you just use sprintf?
    Linux User #28251 (April '93)
    Professional Java Geek :cool:

  6. #6
    LackeyLad Guest
    on our machine a long is 32 bits, and a char is 8, so therefore i need to make it so that this function shifts and maks the bits 4x and adds the char value to the string, one might think

  7. #7
    Join Date
    Aug 2004
    Posts
    4,082
    Is this what you're trying to do?


    #include <string.h>
    #include <string>
    #include <netinet/in.h>

    using namespace std;

    static string long2string(long longNum)
    {

    // longNum = htonl(longNum); // if you want big endian byte order

    char* array = new char[sizeof(longNum)];
    memcpy(array,(char*)&longNum, sizeof(longNum) );

    string s(array);

    delete [] array;

    return(s);
    }
    "Never let the task you are trying to accomplish distract you from the study of computers."

  8. #8
    Join Date
    Feb 2004
    Location
    Seattle, WA, USA
    Age
    62
    Posts
    3,423
    Blech! I'm soooo glad I'm using Java these days...
    Linux User #28251 (April '93)
    Professional Java Geek :cool:

  9. #9
    Join Date
    Aug 2004
    Posts
    4,082
    Ok, show us the Java way. But first you have to figure out what we're trying to do!
    "Never let the task you are trying to accomplish distract you from the study of computers."

  10. #10
    Join Date
    Feb 2004
    Location
    Seattle, WA, USA
    Age
    62
    Posts
    3,423
    Okay, I did say I was real rusty, but what it looks like is you're trying to stuff each byte of a long into a string (aka char array). There's not quite such an interesting way of doing that particular operation in Java - you have to mask each byte and put that into a byte array, then turn it into a String (which is not an array of char). Takes more operations, but then this kind of pointer trickery was deliberately not included in Java - mostly because of all the bad things you can do with pointers. I did a few of those, myself...
    Linux User #28251 (April '93)
    Professional Java Geek :cool:

  11. #11
    Join Date
    May 2004
    Location
    Raleigh, NC
    Posts
    741
    Quote Originally Posted by tashirosgt
    Ok, show us the Java way. But first you have to figure out what we're trying to do!

    In Java all you have to do is

    int x = aNumber;
    String xString = "" + x;


    if you want to grab each char you could always do
    char c = '';
    xString.chatAt(pos);

    Or is this not what you are trying to accomplish?
    E-mail: jcstille@gmail.com
    FedoraForum.org Administrator

    Please read the Guidelines
    So these are the people FedoraForum.org Here and their Blogs


    My Blog <-Where I let you know whats going on

  12. #12
    Join Date
    May 2004
    Location
    Raleigh, NC
    Posts
    741
    Is this another method of doing it?
    http://www.cplusplus.com/ref/cstdlib/ltoa.html

    This might be what you are looking for, but it was after a little googling.
    E-mail: jcstille@gmail.com
    FedoraForum.org Administrator

    Please read the Guidelines
    So these are the people FedoraForum.org Here and their Blogs


    My Blog <-Where I let you know whats going on

  13. #13
    Join Date
    Feb 2004
    Location
    Seattle, WA, USA
    Age
    62
    Posts
    3,423
    Damn, I am rusty! In Java, jcstille's way is used a lot in output, but when you're going to be doing other things with the String besides output, this is a lot clearer:
    Code:
    long x = 122345L;
    String s = Long.toString(x);
    One of the things you learn along with Java, especially on a large development team, is that obfuscation and cleverness are all well and good, but how would you like to maintain code written like that?
    Linux User #28251 (April '93)
    Professional Java Geek :cool:

  14. #14
    Join Date
    Sep 2004
    Location
    NORTHCOM
    Posts
    813

    Arrow

    Try this: man strtol and see if that makes your day.

    And if you thing that's funny because it's backwards and I'm too sleepy to get it right the first time, try this: man atol.

    Homer sleep now. Zzzzzz.z......
    Last edited by Dog-One; 16th October 2004 at 05:41 AM.
    Please give credit where credit is due--say thanks in the active thread.
    Refresh yourself with the Posting Guidelines........Frequently Asked Questions........Registered Linux User #369513

  15. #15
    Join Date
    Sep 2004
    Location
    NORTHCOM
    Posts
    813
    Oh heck with it, they're both the same. Take two sprintf's and call me in the morning.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. server glx vendor string: SGI server glx version string: 1.2
    By akp in forum Installation, Upgrades and Live Media
    Replies: 4
    Last Post: 25th November 2007, 08:04 PM
  2. Replies: 1
    Last Post: 2nd September 2007, 02:56 AM
  3. Replies: 10
    Last Post: 29th June 2007, 04:07 PM
  4. String changes in FC5
    By meslick in forum Programming & Packaging
    Replies: 2
    Last Post: 23rd January 2006, 12:19 PM
  5. string manipulation
    By mac23 in forum Programming & Packaging
    Replies: 0
    Last Post: 28th December 2005, 03:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •