Go Down

Topic: Why do people send and save numeric data as ASCII characters? (Read 1 time) previous topic - next topic

Mr_arduino

On thing that has always baffled me about lots of code that I see is the desire to store numeric data as plain text when it would be more efficient just to store the data as is without doing any conversions.

Here are disadvantages for converting your data to plain text:
  • Increase in storage requirements as it now takes more memory to store the data than it would by leaving it as it's binary representation
  • Potential loss in precision. This applies to data that is either float or fixed point data. For example your code outputs four decimals but you would have had more if you just saved the float/double.
  • It will be converted back anyway.
  • Increase in amount of data to transfer
  • Requires more CPU time to convert the data an ASCII representation which is bad for high speed data logging


So with all these disadvantages why would anyone in the right mind convert their data to an ASCII representation unless it needs to be displayed to a human?

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Nick_Pyner

This is utter nonsense and the sort of thing I used to hear from the knowledge-is-power white dustjacket brigade in the 1960s while they were eating their 01101100 01110101 01101110 01100011 01101000.

Now that the 20th century is drawing to a close, we are in a high-level language world, and most of us find that data is calculated or generated in plain english in the first place, so I submit is it better to ask why anybody in their right mind would convert it to binary without evidence of a need to do so, particularly when the end results is for high-level consumption. I suppose there is a place for stunts like that, but I don't see it in the Arduino world, so it sound like the wrong forum. A dog-chasing-car forum would be more appropriate.

nickgammon

Well, generally speaking data is stored in binary internally. For example if you use an int or long.

I just don't know about "lots of code" that doesn't do that.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Nick_Pyner

Well, generally speaking data is stored in binary internally.
Well, that's a relief. So, is there anything we need to worry about here?
Perhaps I can get on with fixing the 01100100 01101001 01101110 01101110 01100101 01110010.

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

TomGeorge

Hi,

Quote
Now that the 20th century is drawing to a close,
? ? ? ? ? ? ? ?

Tom....... :o
Everything runs on smoke, let the smoke out, it stops running....

Mr_arduino

I am not advocating for sending a bunch of ones and zeros as ASCII characters. Doing so is even less efficient.
Here is an example of what should be done in c-style pseudo code.
The getReading() is a made up functions that reads a value from a sensor.
Code: [Select]

int x=getReading();
saveInt(x);

Here is what I commonly see
Code: [Select]

char buf[12];
int x=getReading();
itoa(x,buf,10);
saveStr(x);

Maybe the confusion comes from my terminology. I was referring to (as  Nick Gammon mentioned in his second post) variables types such as char,short,int,long,long long (and of course the unsigned variants). What is the best general way to express these type of numbers versus ASCII string that just happens to contain a series of characters that are numbers. I came up with the term binary number due to the fact that these variable types create a larger number with n number of bits. Would binary-based number be better?

Regardless lets say you had two different bitmap file formats. One just stores the pixel data as a byte (for each pixel and channel of course) and another (for each pixel and channel) stores the pixels as a comma delineated series of numbers as a sequence of ASCII characters. The first bitmap format requires less storage space and less code to read and write said format. This increases loading and saving performance as well which will pay off especially well for larger images.

Nick_Pyner

Hi,
? ? ? ? ? ? ? ?

Tom....... :o
Yes, OK, but I thought that, with the way this appears to be going, it might be better dealt with without too much shock - i.e. one century at a time.

Nick_Pyner


keeper63

I am not advocating for sending a bunch of ones and zeros as ASCII characters. Doing so is even less efficient.
Here is an example of what should be done in c-style pseudo code.
The getReading() is a made up functions that reads a value from a sensor.
Do you have a real-world example (a link to it would be fine)?

Maybe what you are really seeing is poorly written code? Or maybe there's some kind of "method to the madness" (especially if you are looking at the lower level internals of the Arduino core library - maybe some of this has to do with the fact that the library exists to support many different microcontrollers, not just a single specific one - and thus, the code has to take this into account, and trade off efficiency and speed to gain that support)?

I am not saying that what you are getting at is wrong - ideally, especially for smaller microcontrollers - you want your code to be as compact as possible, to allow you to utilize it to its fullest extent. But this viewpoint is one of an experienced coder. For the people the Arduino is aimed at (mainly artists and non-coders) - doing these non-standard, non-efficient coding patterns may make more sense.

That said, your example - while illustrating the point - seems a bit contrived due to its simplistic nature; as I noted, I would like to see what you are getting at in a real-world scenario before I could make an honest judgement call.
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

testato

is there many example where it is usefull ITOA (or other conversion)
- you need to write on an LCD and his library accept only string to rpint
- you want search inside the number received from sensor, if you receive a number whit 10digit, but you need only the two in the middle, you can use ITOA and after search on it whit the String object method
- you have another microcontroller connnected via serial that await for a string, yes you can send the right number to the serial, but often is simple sent the right char because is smple to read the program

- [Guida] IDE - http://goo.gl/ln6glr
- [Lib] ST7032i LCD I2C - http://goo.gl/GNojT6
- [Lib] PCF8574+HD44780 LCD I2C - http://goo.gl/r7CstH

Go Up