LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-18-2005, 09:29 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
C++ converting from char to integer


Hey, easy question...

I am reading a pnm image file where each rgb value is 3 single bytes. I'm reading it into a char array. How can I then get the decimal (unsigned) value? For example, if the color is RGB(224,223,224), how can I get those into an unsigned variable as such? That is, I don't want RGB(-32,-33,-32).

I want to be able to do a comparison to see if each is within a range of values. So, for a given pixel, I'm checking if the red value is less than 225 and greater than 235 as an example. I want to be able to do

char r = 224; // stored in 8-bits = -32
if (r > 220 && r < 235)
{
// the above evauates to false because -32 is NOT > 220
}

I'm sure it's a simple newbie question.

Thanks,

Micah
 
Old 12-18-2005, 09:59 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
If you're programming in C or C++, it *IS* a simple question - you just cast to "unsigned char".

If, on the other hand, you're programming in Java ... the answer might be equally simple ... or there might not be any easy answer. For example:

http://forum.java.sun.com/thread.jsp...sageID=2956319
http://saloon.javaranch.com/cgi-bin/...&f=33&t=007156
 
Old 12-19-2005, 01:42 PM   #3
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
Oh yeah, sorry, it's c++. Problem is, when I cast to an unsigned int I get 4294967264 instead of 224
 
Old 12-19-2005, 01:47 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
int ir = (unsigned char)r;
int ig = (unsigned char)g;
int ib = (unsigned char)b;

The key is to make sure (if you happen to be starting out with a *signed* char - either explicitly or implicitly) to make sure your cast doesn't left-fill a bunch of unneeded/unwanted "1" bits.

The way to do this is to cast to "(unsigned char)" right at the outset.

'Hope that helps .. PSM
 
Old 12-19-2005, 02:16 PM   #5
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
Brilliant. That's it. Thank you so much!
 
  


Reply



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
Convert Integer to Char gjagadish Programming 5 10-14-2005 10:09 AM
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com Programming 5 10-04-2005 11:51 AM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM
Converting integer types... JStew Programming 3 12-03-2002 08:53 AM

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

All times are GMT -5. The time now is 01:17 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