Sign in to follow this  
sarbruis

non-floating point 12-digit datatype?

Recommended Posts

I'm just looking for something that can store a 12-digit barcode. Long double stores enough digits, but when it's output it's in scientific notation. Do I need to make my own datatype? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
I believe long long will just barely give you enough room. It should be 64 bits under MinGW.
Actually, 64-bits is enough for 19 decimal digits.

Share this post


Link to post
Share on other sites
Okay, I did that and I'll type in a 12-digit number like 849569346323 and I initially read it in as a string, so when I use atoi() to change it back to a long long, I get some weird 10-digit number like 1465340171. Is this a problem with atoi()? Should I use a different function to convert from std::string to long long?

Share this post


Link to post
Share on other sites
Quote:
Original post by sarbruis
Okay, I did that and I'll type in a 12-digit number like 849569346323 and I initially read it in as a string, so when I use atoi() to change it back to a long long, I get some weird 10-digit number like 1465340171. Is this a problem with atoi()? Should I use a different function to convert from std::string to long long?
I think GCC implements an equivalent atoll() function for 64-bit integers.

Share this post


Link to post
Share on other sites
is there a reason that you can't just store them as strings? I don't know much about barcodes but I don't think that you are going to be using any of the standard mathematical operators with them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raghar
It would be waste of space, bit silly to store a number as string on computer, and slower to compare.

BTW one way how could you store it under C++ is: char barcode[12];

We've secretly replaced Raghar's original second paragraph with one that completely contradicts his first. Let's see if he notices!

CM

Share this post


Link to post
Share on other sites
It is worth noting that UPCs are inherantly a binary-coded decimal system. In other words, strings are a very natural way of representing them. Furthermore, individual decimal digits are needed at different stages depending on the specific item and use. So storing a UPC as a 64-bit binary number will yield numerous unneccessary binary to decimal conversions.

Just food for thought before dismissing strings as inefficient.

CM

Share this post


Link to post
Share on other sites
Something happened to the standard part of C++ library called string?
Actually I don't consider char array and a string as a synonym.

No definition of char array here either. ~_^

http://www.synonym.com/definition/string

Share this post


Link to post
Share on other sites
Another potential advantage of storing as a string/char array over long long is that maybe you don't want leading zeros on some codes and maybe you do on others - with a long long there'd be no easy way to differentiate them?

Share this post


Link to post
Share on other sites
Another consideration is that by using a long long, you won't be able to deal with alphanumeric barcodes if that ever becomes an issue. SKUs are usually just numeric, but then you might get the bright idea to track UPS shipping labels. Which just happened to me a while ago.

Share this post


Link to post
Share on other sites
Quote:
It would be waste of space, bit silly to store a number as string on computer, and slower to compare.


But a bar code isn't really a number, it is an array of single digits. Exactly the kind of thing to store as a string. I doubt it would be much slower. Plus storing it as a string will make your code easier to write and maintain.

Share this post


Link to post
Share on other sites
From my own experience working with databases, barcodes and retail/tracking, use a string. As mentioned, a barcode is a set of characters (that happen to be [0-9] 99% of the time). Storing it as a number (and you're not even getting a great spacing savings, 8 bytes vs 12 bytes) is a good way to make your code break in the future.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this