unsigned long to char

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

unsigned long to char

Postby MaxD » Sat May 26, 2012 11:29 pm

how to change unsigned long to char?
CODE: SELECT_ALL_CODE
unsigned long currentData = 12345678;
char res[4];

res[0] = currentData & 0x000000FF;
res[1] = (currentData & 0x0000FF00) >>8;
res[2] = (currentData & 0x00FF0000) >>16;
res[3] = (currentData & 0xFF000000) >>24;

what I expect:
res[0] = 78
res[1] = 65
res[2] = 34
res[3] = 12

But I can't get the correct value. Why?
MaxD
Newbie
 
Posts: 11
Joined: Thu Apr 26, 2012 10:51 am

Re: unsigned long to char

Postby ABSF » Sun May 27, 2012 11:47 am

For those who are interested in knowing what the problems could be, read here...

http://forum.allaboutcircuits.com/showt ... hp?t=70479

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: unsigned long to char

Postby robosang » Mon May 28, 2012 9:00 am

Din went to the link Allen paste, but first guess would be the decimal value and hexadecimal value.

You assign the value with decimal value, but when you shift, the controller is shifting in binary bits :) So definitely you cannot get what you expect.

The formula works, just your assignment is in decimal, you should assign it in hexadecimal, it should works :)
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: unsigned long to char

Postby yonghui » Mon May 28, 2012 11:12 am

unsigned long currentData = 12345678;
char res[4];

res[0] = currentData & 0x000000FF;
res[1] = (currentData & 0x0000FF00) >>8;
res[2] = (currentData & 0x00FF0000) >>16;
res[3] = (currentData & 0xFF000000) >>24;


u do it in binary and u will know why.
convert decimal 12345678 to binary. then do it.


regards,
yh
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: unsigned long to char

Postby MaxD » Mon May 28, 2012 5:13 pm

OK, I know the problem. But what if, in my actual case, the long type value is read from the PORTS of the PIC. So I want to extract the value into the char array because I send/receive the data is in char array format (byte by byte).
MaxD
Newbie
 
Posts: 11
Joined: Thu Apr 26, 2012 10:51 am

Re: unsigned long to char

Postby takao21203 » Mon May 28, 2012 5:23 pm

there is more than one way.

All you need is practice with C.

You can for instance use typecast:

(char) (currentData >> 16) & 0xff

or you can use a pointer to the long data, and use this for char array as well.
This is however "dirty", if you port it, you get endian issues. For most programs it's not too much relavent = means don't care.

if you don't get correct value for the typecase you must use 0x000000ff, or 0x00ff0000.
User avatar
takao21203
Discoverer
 
Posts: 111
Joined: Mon Mar 19, 2012 7:52 pm
Location: Ireland, Europe

Re: unsigned long to char

Postby takao21203 » Mon May 28, 2012 5:25 pm

the original code can't work as pointed out, you should use 0x12345678 but that also is not decimal.
User avatar
takao21203
Discoverer
 
Posts: 111
Joined: Mon Mar 19, 2012 7:52 pm
Location: Ireland, Europe


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 30 guests