Hello All,

I have been trying to convert a long long to a char buff and having problems.

I have tried spintf, and some casting but still can not get the conversion right.

Any help would be great!

Steve

Recommended Answers

All 16 Replies

Post an attempt - and mention your OS/Compiler..

Well I am on Tandem Mainframe.

I have tried a few:

char buff[30];
long long lval;

memset(buf, 0, sizeof buf));
sprintf(buff, "%f", lval);
" %d "
" %u "

memcpy(buff, (char *)lval, sizeof(lval));

My std libs do not appear to have ltoa functions, juts atol, etc.

So your compiler supports long long, but no way to format it into a string? I find that hard to believe, did you check your compiler's documentation? At the very least there should be a non-standard function or extension that supports what you want.

I tried functions as ltoa, itoa, etc and I took a look at my std libs and their is no ltoa conversion listes just the opposite.

I am dealing with a 64 bit value goinf to a array of chars and I do not see anything special for that.

short is support to but thier is no special call functions for it.

What calls do you suggest and I can look in my libs to see if they are there. This compiler is pretty old I think as I have found it missing other calls.

I am on contract here and not an employee.

>I am dealing with a 64 bit value goinf to a array of
>chars and I do not see anything special for that.
Don't just look at the headers, check the documentation. I'm reasonably sure that if the compiler supports long long, it provides a format specifier for sprintf as well.

>I am dealing with a 64 bit value goinf to a array of
>chars and I do not see anything special for that.
Don't just look at the headers, check the documentation. I'm reasonably sure that if the compiler supports long long, it provides a format specifier for sprintf as well.

I am looking believe me! This contract sucks they do not use C at all but for this project was needed and they have a weak C environment. I am probably the only to have used the C compiler in ages!

I was just wondering if thier was a manual mathmatical way to do it?

Thanks Steve

>I was just wondering if thier was a manual mathmatical way to do it?
Well, long long is just another integer type, so converting it to a string is really no different from writing your own itoa function (or lltoa, as it were). If I were you[1], I would start looking for source code to itoa variations online (of which there are many). Chances are good you can change int to long long and be ready to go. You might have to look out for range assumptions, but aside from that the conversion should be trivial.

[1] If I were me, which I am, I would write my own from scratch because I've done it before and have sufficient experience implementing libraries to trust my own skills over some random snippet I found on the web. ;)

>I was just wondering if thier was a manual mathmatical way to do it?
Well, long long is just another integer type, so converting it to a string is really no different from writing your own itoa function (or lltoa, as it were). If I were you[1], I would start looking for source code to itoa variations online (of which there are many). Chances are good you can change int to long long and be ready to go. You might have to look out for range assumptions, but aside from that the conversion should be trivial.

[1] If I were me, which I am, I would write my own from scratch because I've done it before and have sufficient experience implementing libraries to trust my own skills over some random snippet I found on the web. ;)

Thanks I will look for that and I may have to do some bit shifting that I was trying to avoid! It just seems it is all catered to go from ascii to int, long, etc.

I've had this problem in the past. I used a custom function made by someone else, because I couldn't write my own...

Click

did you try this: sprintf(buff, "%ld", lval);

I've had this problem in the past. I used a custom function made by someone else, because I couldn't write my own...

Click

I am in that very same boat! I am not the greatest when it comes to conversion.

I am mostly a TCP/IP style programmer.

Thanks all of you for the help and I should also say Hi as I just joined after reading your
post for awhile!

I am Steve a independant consultant working in the Tampa FL area and have been in this biz for 20 years and it seems like old problems you have not seen in a while bight you the most.

Thanks,

Steve

Thanks,

Steve
Beep when the frontier is here. "Beep"

I've had this problem in the past. I used a custom function made by someone else, because I couldn't write my own...

Click

I thank you and have found the code snippet you routed to me and I am going to give it a whirl.

Thanks!

>did you try this: sprintf(buff, "%ld", lval);
How does that solve the problem? The OP is converting long long int, not long int. And I've already gone through this with him, but he wants to insist that sprintf doesn't have a conversion specifier for long long even though the compiler supports the type. :icon_rolleyes:

I FOUND IT!!!

I was messing with sprintf modifiers and came up with this

I have used %ld in the past for longs but for double longs a cap L is needed.

sprintf(buf, "%Ld", lval); and the number is the same so fnially got.

Thanks

All

> sprintf(buf, "%Ld", lval); and the number is the same so fnially got.
And that isn't documented in your compiler's documentation for it's standard library?

If it isn't, how do you know that your "trial and error" determination of "%Ld" is actually right, and not merely giving you the expected result for your test cases?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.