I have an int that I need to convert to an unsigned long. How would I do this?
int a = 7 ;
unsigned long b = ( unsigned long ) a ;
Maybe copy the int to a long variable
unsigned int anInt = 1234;
unsigned long aLong = 0;
void setup()
{
aLong = anInt;
}
or depending on the source of the int declare it as an unsigned long in the first place ?
So by doing (unsigned long)a on what was previously an int, c++ will automatically convert that for me?
Yes, although you have to think about what might happen if the int is a negative number. The language supposedly specifies what should happen, which might not be what you expect.
If the value is negative casting directly to a long type will sign-extend it even
if the result type is unsigned long. You might want to do:
unsigned long b = (unsigned long) (unsigned int) a ;
if that is an issue.
UKHeliBob:
Maybe copy the int to a long variableunsigned int anInt = 1234;
unsigned long aLong = 0;
void setup()
{
aLong = anInt;
}
or depending on the source of the int declare it as an unsigned long in the first place ?
The original int is used to determine the numeric value of an ACIII character for a char array coming from a Serial command. I translate the ASCII value to ints, minus the value of 0, which is 48. That will then get me ints with a value of 0 to 9. I check to ensure that all the ints are a valid number(so if its negative or positive then clearly the command entered wasn't a number).
I then need to transfer that to an unsigned long so I can multiple by 100,000, 10,000, etc. for the corresponding digits. This will then get me digit 1 being a value in the 6th as the command is a 6 digit command. So then when I add them together I transform something like:
char[6]="412509";
//Conversion code from char to int.
....
unsigned long = 412509;
==========================================
Am I correct in assuming that you can only get the Decimal value of an ASCII character when going to an int? Or can I go directly to an unsigned long?
MarkT:
If the value is negative casting directly to a long type will sign-extend it even
if the result type is unsigned long. You might want to do:unsigned long b = (unsigned long) (unsigned int) a ;
if that is an issue.
I take this number directly from a serial command and I exclude all values that are not ASCII = 48 to 57, so I shouldn't be able to have a nagative number.
You probably don't need to convert each digit to an unsigned long. You can probably just use unsigned long when you add them up.
char input[] = "123456" ; // dummy input
unsigned long result=0 ;
unsigned long mult=1UL ;
for ( int i=5 ; i>=0 ; i-- )
{
int j = input[i] - '0' ;
result+= mult*j ;
mult*=10 ;
}
something like this should work.
The only reason I do it on a per character basis is I check each character to ensure that each character is a valid number. For instance that code would return 10 if someone entered ":" as it's ASCII value is exactly 10 above 0.
Well you can check each character without making it an unsigned long each time.
for ( int i=5 ; i>=0 ; i-- )
{
uint8_t j = input[i] - '0' ;
if ( j >=0 && j <= 9 ) result+= mult*j ;
mult*=10 ;
}
This might, or might not, save a lot of pointless byte manipulation on an 8-bit processor, creating and shuffling around temporary 32 bit values.
Awesome! Thank you very much.