Leading zero in serial print

Question.

I receive alot of digits from my pc meaning to proces them but my ints lose leading zeros.

Is there a way i should store them different in the int or is it the Serial print or lcd print?

I lcd print them via a large string whit more ints and letters in
Example of a string 069 001 abc dfg
But i receive 69 1 abc dfg

I tried serial print same problem as lcd write and print

Create a function that checks the range your number falls into.

if < 10 you then print a two 0s

else if < 100 print one 0

finally print the number

Therefore 9 will be printed as 009

Therefore 99 will be printed as 099

 Void leadzero (int miss) {
  String spit; 
  If (miss < 10) { spit = "00" + miss;} 
  else if (miss < 100) { spit = "0" + miss; } 
  else { spit = miss;} 
  Return(spit) ;
[code] 

Would this work like u said?

What happened when you tried it ?

‘String’ is not recommended as it may cause memory problems; use a ‘char’ string instead.

larryd:
What happened when you tried it ?

‘String’ is not recommended as it may cause memory problems; use a ‘char’ string instead.

I have extreme nightmares whit char sofar.
Probaly due too my poor code writing. Hence back to strings sofar. I havent tried yet as the kids have my pc under hostage just was wondering if that would be along the way u was recommanding.

Void should be void (lower case v, not capitalized).
How can leadzero(...) return a String if it is declared to return void ?

Perhaps Void should be spelled S t r i n g .

... but of course I agree that an array of char should be used instead of String.

michielsweb:
I have extreme nightmares whit char sofar.

I've had extreme nightmares with Arduino strings...

Quick note ibwil try thebfix soon.

About char wel sofar i had troubles whit it i switched to string and it seem working for me.

I rather havey prototype working before i figure oit char cause i have no idea sofar what i did wrong when i was using char

ok so quick recap.

  1. i dont understand how the char works like i tried char name = "input" but i seem to not be able to make it like glue info together. i end up whit weird characters on my lcd.

  2. i used code before.
    but i either get 00 or 0 but the int doesnt glue.

String leadzero(int miss) {
  String spit;
  if(miss < 10 ) { spit = "00"; spit = spit + miss; }
   else if  (miss < 100) { spit = "0"; spit = spit= + miss; }
  else { spit = miss; }
  return(spit);
}

i use it like this i create 2 strings for my lcd.

      string1 = leadzero(token[0]); string1 = string1 + " " + leadzero(token[1]); string1 = string1 + " "; string1 = string1 + vertical[token[7]]; string1 = string1 + "  " +  horizontal[token[8]];
      string2 = autopilot[token[7]] + " "; string2 = string2 + token[3]; string2 = string2 + " "; string2 = string2 +  token[4]; string2 =  string2 + " " + token[5];

current output on the leadzeros is: no digit, 0, or 00

i compare the strings to the old storage string. if i update screen or not.
example if(string1 != string1old or string2 != string2old) { //update screen
}

i am open to change to char. but if i turn the initial String to char it simply wont work as created here.

[/code]

char name = "input"

A char is literally a single character(byte) but the string "input" consist of six bytes; six into one doesn't go.

char* name = "input";
or
char name [] = "input";

Read this for a high level intro to cStrings (null terminated char arrays)

Description
Text strings can be represented in two ways. you can use the String data type, which is part of the core as of version 0019, or you can make a string out of an array of type char and null-terminate it. This page described the latter method. For more details on the String object, which gives you more functionality at the cost of more memory, see the String object page.
...

problem seem fixed.
i got a other problem :slight_smile:
maybe 1 can solve this.

i get 4 digits send to my arduino.
i split the digits :slight_smile:

i change the digits.

now i want to glue them together again.

what would be the best way?

so lets say i start whit 1234

i split the digits . each int contains digits
is there a way i can glue them together again?

michielsweb:
problem seem fixed.
i got a other problem :slight_smile:
maybe 1 can solve this.

i get 4 digits send to my arduino.
i split the digits :slight_smile:

i change the digits.

now i want to glue them together again.

what would be the best way?

so lets say i start whit 1234

i split the digits . each int contains digits
is there a way i can glue them together again?

just to be clear:
currently i do this:

int income = "1234";

int digit1 = income / 1000 % 10;
int digit2 = income / 100 % 10;
int digit3 = income / 10 % 10;
int digit4 = income % 10;

// skipping processing codes to save length


// digit1,2,3,4  are changed..

now what is best way to glue digit 1 til 4 together again.?

now what is best way to glue digit 1 til 4 together again.?

int result = (1000 * digit1) + (100 * digit2) + (10 * digit3) + digit4;

Brackets used for clarity but not actually needed

int income = "1234";

Why are you storing a 5 byte c string (4 characters plus the terminating null) in a 2 byte int?

unsigned long income = '1234'; :smiley:

unsigned long income = '1234';

Sorry but this doesn't work correctly either. An unsigned long is only 4 bytes and you need to account for the terminating null, and the double quotes have become single quotes.

If the single quotes were intentional, they should not be used around multiple characters.

Sorry but this doesn't work correctly either

Missed smiley error I think !

vaj4088:

unsigned long income = '1234';

Sorry but this doesn't work correctly either. An unsigned long is only 4 bytes and you need to account for the terminating null, and the double quotes have become single quotes.

If the single quotes were intentional, they should not be used around multiple characters.

Try it.
Be surprised.

Define "correctly"