LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2007, 04:26 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
converting char to unsigned integer in c


Hi Folks,
I have the following code:
Code:
char binary[PAYLOADSIZE];
		
binary[0] = 0x2F;
binary[1] = 0xCD;
When I try to convert it to unsigned char like this:

Code:
unsigned short temp1,temp2;
temp1= (int)binary[0];
temp2=(int)binary[1];
binary[0] can be converted to integer correctly to 47, but binary[1] gets converted to 65485.

Any idea why it is like that?
 
Old 02-27-2007, 04:42 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,454

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

Remember signed is default - also for char.

Try to change it to
unsigned char binary[PAYLOADSIZE];

Last edited by Guttorm; 02-27-2007 at 08:31 AM.
 
Old 02-27-2007, 02:58 PM   #3
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Thanks a lot. I really appreciate that
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Length of an unsigned integer Deepak Inbasekaran Programming 8 06-07-2006 11:47 PM
C++ converting from char to integer MicahCarrick Programming 4 12-19-2005 02:16 PM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
unsigned char to wchar_t JurajPsycho Programming 6 03-19-2005 04:00 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration