473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Printing unsigned long long int's

Hi,

I'm trying to convert a string to an unsigned long long int.

% /tmp/long
str 0x20000
base 16
decimal 131072
hex 20000
str 0x1abcd8765
base 10
decimal ffffffff
hex ffffffff

But when the value is > 2^32 "cout" does not cooperate.

% gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)
% cat long.cc
#include <iostream>
#include <string.h>
#include <stdlib.h>

void print_str(char *str)
{
int base;
long long int l;

base = (strncasecmp(str, "0x", 2) == 0) ? 16 : 10;

l = strtoul(str, NULL, base);

cout << "str " << str << endl;
cout << "base " << base << endl;
cout << "decimal " << l << endl;
cout << "hex " << hex << l << endl;
}

int main(void)
{
char str[80];

strcpy(str, "0x20000");
print_str(str);

strcpy(str, "0x1abcd8765");
print_str(str);

return 1;
}
Its NOT homework!

Thanks,
/Ed
Jul 19 '05 #1
1 19579
"Edward Arthur" <ea*****@attbi.com> wrote in message
news:56**************************@posting.google.c om...
I'm trying to convert a string to an unsigned long long int. .... But when the value is > 2^32 "cout" does not cooperate.
Are you sure it is "cout" that is not cooperating?
#include <iostream>
#include <string.h>
#include <stdlib.h>

void print_str(char *str)
{
int base;
long long int l;

base = (strncasecmp(str, "0x", 2) == 0) ? 16 : 10;

l = strtoul(str, NULL, base); Note: if you pass zero as a base, strtoul will
choose between decimal/octal/hex based on
the usual C++ prefixes.
But:
strtoul returns a 'long', not a 'long long' unsigned.
The C library has a strtoull (double-l) function since
the 1999 version of the standard, which you can use
instead if your platform supports it.
cout << "str " << str << endl;
cout << "base " << base << endl;
cout << "decimal " << l << endl;
cout << "hex " << hex << l << endl;

These should be ok if your compiler and library
support long long types. Note however that 'long long'
is not currently standard in C++ (it was introduced
in the 99 version of the C language).
If cout really was the part that doesn't work, maybe the
C++ library you use lacks the following operator overload:
std::ostream& operator << (std::ostream&, unsigned long long);
It would be easy enough to implement (a simplified version of) it,
using sprintf to first do the conversion to a string...
hth, Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: drj0nson | last post by:
Without getting too mathematical : how do you print/handle big numbers? for ( int i = 1 ; i < 10 ; i ++) cout << (123456789 << i) << endl; 246913578 493827156 987654312 1975308624...
7
by: crazy | last post by:
Hi It seems something really simple, but can't figure out. It's not my homework :), was just looking at some C problems on the web and came across this one. why doesn't the following code print...
6
by: Enrico `Trippo' Porreca | last post by:
Suppose I wanted to print an unsigned char (in hex). Should I say: unsigned char x = 0x12; printf("%X\n", x); or printf("%X\n", (unsigned) x);
10
by: Maxime Barbeau | last post by:
Hi. I have the following code: uint32_t quadlet = 0x12345678; printf("quadlet = %08X\n", quadlet); my compiler give me the following warning: unsigned int format, uint32_t arg (arg 2) Is...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
48
by: Yahooooooooo | last post by:
no gotos no loops how is it possbile..... Asked in interview ... Regards MA
20
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include...
5
by: A. Farber | last post by:
Hello, I'm trying to print out a const char* buffer by first printing 16 bytes as hex codes, then printing them again as characters or dots (if they aren't printable) and so on: 20 A5 96 74...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.