[SOLVED] i have an error when i want to convert unsigned char to long int in codevision

Status
Not open for further replies.

mahm150

Full Member level 1
Joined
Dec 14, 2010
Messages
98
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Cairo, Egypt, Egypt
Activity points
2,001
i have an error when i want to convert unsigned char to long int or unsigned int in codevision altough i check in promote char to int .
unsigned char data;
unsigned int long_data;
unsigned int address;

address+=1;
while(x<9)
{
data=EEprom_READ(address);
long_data = (long_data << x)|data ;
x+=8;
address-=1;
}
 

its the move left is the issue

you need to use split word then add two words together


your code measures in levels of indifference
so pad 0 then...add with shift...
 

i make that (unsigned int)data=EEprom_READ(address);
and now i test until now it work good
about VSMVDD u mean that
data1 =EEprom_READ(address-1);
long_data =(unsigned int) data1 | data ;
or what do you mean about this sentence
your code measures in levels of indifference
so pad 0 then...add with shift...
 

long_data =(unsigned int) data1 | data ;
I think mikroC auto convert, unsigned char to unsigned long int.
Don’t want to explicitly convert like that.

unsigned char charValue;
unsigned long int output;
void main(){
charValue=50;
output=0;
output= EEPROM_Read(charValue)/charValue;

}
 
Last edited:

i not used mikroC. i use codevison
i think that my code now work good when i make this (unsigned int)data=EEprom_READ(address);
 

i not used mikroC. i use codevison
i think that my code now work good when i make this (unsigned int)data=EEprom_READ(address);
You can use
Code:
long_data = (long_data)|((unsigned int)data<<x) ;

shift the type-casted data and then OR with your variable,take care of the correct order of your bytes because this is the reverse of what you do.

I don't understand what does the (unsigned int)data=EEprom_READ(address); do , you are reading an 8bit variable into an 8bit variable, what is the point of typecasting at this point.

Alex
 

i make that because i read two bytes as u understand then make that
long_data = (long_data << x)|data ;//x=8
the long_data is (unsigned int).
 

Yes but when you use typecast in a part of your program (unsigned int)data=EEprom_READ(address); then it is valid only for that specific point,
this typecast is not preserved for the rest of the program so i don't understand why it makes a difference in the rest of the code.

Alex
 

i will try to comment in this casting then i wil tell u the result

---------- Post added at 19:38 ---------- Previous post was at 19:31 ----------

it work good ; that u mean i not need to make cast in long_data = (long_data << x)|data
although data unsigned char
 

Hello dear friend
Training to an article I introduce the programming you codevision ..!!

If you say you love?
 


No , i said that you need to use typecast in long_data = (long_data)|((unsigned int)data<<x) ; or anyway you do it
but not in (unsigned int)data=EEprom_READ(address);

It is not wrong but there is no point to use a typecast when you write an 8bit value in a 8bit variable, EEprom_READ should returns a byte.

Alex
 

Many things work without a typecast but maybe will stop working with another compiler,
to make sure that everything will be treated as you want to you have to force the compiler with typecasting.

Alex
 
i understand u now i make that
long_data = (long_data << x)|(unsigned int)data ;
but i not understand how it work good when i comment cast
 

Which cast do you remove?
You mean long_data = (long_data << x)|data ;

This can work because you are using shift left in a variable that is 16 bit long so the 8 bits lower bits can go 8 positions to the left without a problem, then you OR with a 8bit value, it is normal to OR the lower 8bits of the integer with a 8 bit variable.
If you use shift left 8 in a 8bit variable ir will become 0 because it will still be an 8 bit variable with 8 zeros shifted in, in that case you need to typecast first as a 16bit variable and then shift.

Alex
 

For example
0xFF<<8 becomes 0x00
(unsigned int)0xFF<<8 becomes 0xFF00 (16 bit result)
 

Examples and instructions Take a look this article

**broken link removed**

And password to download the form below: Be careful:
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…