# Help with sending info to a computer.

I have a program that takes as input an SOH (ascii 1) and three digits as ascii ie. 0=30, 1=31.
another quirk is all numbers are three digits so it will send 1 as "1 30 30 31". Is there a way to do this with the arduino editor? ( in pbasic you would do this with a formater like "DEC3" )

I'm not sure what you are asking. Do you want the Arduino to send something over serial (USB) to your program?

30Hex = 48Dec = "0"

Serial.print( 48, HEX); // generates "30"

Serial.print( 30, DEC); // generates "30"

Do you want a print statement to generate "leading zeros" (in your case the string "30")?

Yes, this is going to a PC program (.exe). when the value in the MC is a 1 the program that works sends "1303031" when the Arduino sends the same I get "131" I need to have every number sent as three digits with leading zeros. I know I can do this brute force but was wondering if there was something I was overlooking that would be easier. I'm going to write the brute force program to make sure that works and that just may be the way it has to be.

Yes, this is going to a PC program (.exe). when the value in the MC is a 1 the program that works sends "1303031" when the Arduino sends the same I get "131" I need to have every number sent as three digits with leading zeros. I know I can do this brute force but was wondering if there was something I was overlooking that would be easier. I'm going to write the brute force program to make sure that works and that just may be the way it has to be.

I'm not sure what you mean by brute force in this context.

You can write a simple loop to put in the 0's based on the number of digits. And are you sending the string "1303031" as an ascii string of 7 bytes? In which case you have to write all the code to build that string anyway.

Where does the leading 1 come from?

You can write a simple loop to put in the 0's based on the number of digits. And are you sending the string "1303031" as an ascii string of 7 bytes?

No, it's 4 bytes, an ascii "SOH"(1) , ascii "0"(30) , ascii "0"(30) , ascii "1"(31).
By brute force I mean just what you say, a program segment that parses the number into three ascii characters and sends them to the com port.

You can write a simple loop to put in the 0's based on the number of digits. And are you sending the string "1303031" as an ascii string of 7 bytes?

No, it's 4 bytes, an ascii "SOH"(1) , ascii "0"(30) , ascii "0"(30) , ascii "1"(31).
By brute force I mean just what you say, a program segment that parses the number into three ascii characters and sends them to the com port.

If x is your byte, have you code do something like:

``````serial.print (1);
serial.print ((x/100)+30);
serial.print (((x/10)%10))+30);
serial.print ((x%100)+30);
``````

And this is not, in any way, what one would call brute force.

... and this is not, in any way, what one would call brute force.

I second that!

FWIW, there is a string formatting function available called sprintf, but using this links in almost 2k bytes of code and is not very intuitive to use. If your sketch needs lots of complex formatting it may be worth investigating but for most applications and certainly the one you have in mind, Oracle's solution is the beter approach:

``````#undef int  // needed  because of a macro added to version 0011
#include <stdio.h>

int val ;

// code to send a string starting with ascii 1, followed by the decimal value of val padded to three places with zeros.

char buffer[5];
sprintf(buffer,"%c%03d", 1, val);
Serial.print(buffer);
``````

one thing I just realized is my code should use +48 in each line, not +30. It's using decimal, not hex :).

Thanks.
Oracle, that's the direction I was going in last night but I totally forgot the +48 to get back to ascii so the numbers were all wrong. I call brute force having to write something that does the job but does not scale or port well. Bit banging serial or spi, things like that. I try to write them into functions I can reuse.
mem, sprintf I've used in c when writing for the pc but you are right, it's overkill for this.
Again, thanks to both of you. (back to programming)...

Thanks.
Oracle, that's the direction I was going in last night but I totally forgot the +48 to get back to ascii so the numbers were all wrong. I call brute force having to write something that does the job but does not scale or port well. Bit banging serial or spi, things like that. I try to write them into functions I can reuse.
mem, sprintf I've used in c when writing for the pc but you are right, it's overkill for this.
Again, thanks to both of you. (back to programming)...

I'm glad we were able to help. Good luck with your project. I just wish I had something to work on right now :).

From dictionary.com,

``````brute force programming
A primitive programming style in which the programmer relies on the computer's processing power instead of using his own intelligence to simplify the problem, often ignoring problems of scale and applying naive methods suited to small problems directly to large ones.
``````

To me, the key to the definition of brute force is using pure processing power in place of intelligence. And what it means by not scaling is that if the problem gets bigger you don't have enough processing power anymore.