PDA

View Full Version : long to string algor



LackeyLad
13th October 2004, 03:14 PM
anyone know an easy way to convert long int to string in c++ rather simply?

tashirosgt
13th October 2004, 04:22 PM
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_faq2.html#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.

LackeyLad
13th October 2004, 07:56 PM
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;
}

tashirosgt
13th October 2004, 08:29 PM
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.

crackers
14th October 2004, 04:01 AM
Granted, my C/C++ programming is rather rusty, but couldn't you just use sprintf?

LackeyLad
14th October 2004, 04:56 PM
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

tashirosgt
14th October 2004, 06:16 PM
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);
}

crackers
15th October 2004, 04:14 AM
Blech! I'm soooo glad I'm using Java these days... ;)

tashirosgt
15th October 2004, 02:44 PM
Ok, show us the Java way. But first you have to figure out what we're trying to do!

crackers
16th October 2004, 03:30 AM
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... :D

jcstille
16th October 2004, 04:35 AM
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?

jcstille
16th October 2004, 04:39 AM
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.

crackers
16th October 2004, 05:16 AM
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:


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? :D

Dog-One
16th October 2004, 05:38 AM
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......

Dog-One
16th October 2004, 05:44 AM
Oh heck with it, they're both the same. Take two sprintf's and call me in the morning. :rolleyes:

jcstille
16th October 2004, 08:50 AM
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:


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? :D


Yah know Crackers, the toString() method is a good way to do it. Why do I always forget about it. Thats why when programming in Java, the whole team thing works out. Because of this darn thing called "fundamental fixedness".
Just thought I would give you props for that.

tashirosgt
16th October 2004, 03:11 PM
jcstille,

What does the first java solution do? Does
"" + x
make a string whose characters are the bytes of x?

LackeyLad
16th October 2004, 04:31 PM
what i need is to send long2string a long number, lets say 3245. from the function i want to have a std::string of "3245".

im using this for table use, that is why i need the strings.

LackeyLad
16th October 2004, 04:33 PM
my origianal idea was to make a char array of 4, that is 4 bytes of memory(each char at 8 bits, long is 32). then, starting at the back of the long start masking off the by use of char[0] and adding them to the string. i have an algorithm written, but it sucks and there must be an easier way.

crackers
16th October 2004, 04:59 PM
jcstille,

What does the first java solution do? Does
"" + x
make a string whose characters are the bytes of x?
I'll jump in here - no, it doesn't make the String out of the bytes in x, it turns the value of x into a String: 12345L --> "12345"

When that snippit compiles to byte-code, since the first operand is a String, the compiler will automagically force any subsequent operands to be Strings as well by calling the toString() method on the Object. Since x is a primitive, it gets converted to it's "wrapper class" (Long), then the toString() method is invoked.

tashirosgt
16th October 2004, 05:11 PM
LackeyLad,
The bytes in a variable of type long are not the same as the characters you need to represent that value. For example the byte that represents the character '0' has bits that represent the number 48. Try an experiment like this:

#include <stdio.h>

int main()
{
int i;
char c;
for(i = 0; i < 10; i++)
{
c = (char)(48+ i);
printf(" character %c has value %u\n", c, (unsigned char)c );
}

}