LinuxQuestions.org
Visit Jeremy's Blog.
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 09-18-2009, 03:31 PM   #1
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Rep: Reputation: 29
Reading one byte at a time.


I'm writing a program on Linux to read from the serial port one byte at a time. I had it working before. For some reason now though, I try to read a byte in as a character and the result is way longer than one byte.

Code:
char tmp;
read(fd,tmp,1);
printf("0x%X",&tmp);
And my output is:
0xB7B7A3B7

What's the deal? I thought chars only contained one byte.
 
Old 09-18-2009, 03:41 PM   #2
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by prushik View Post
I'm writing a program on Linux to read from the serial port one byte at a time. I had it working before. For some reason now though, I try to read a byte in as a character and the result is way longer than one byte.

Code:
char tmp;
read(fd,tmp,1);
printf("0x%X",&tmp);
And my output is:
0xB7B7A3B7

What's the deal? I thought chars only contained one byte.
Sorry, Sorry. My fault.
I have been printing the memory address of the char instead of the value.
Sucks, I spent a whole day on a stupid mistake.

Sincere Apologies.
 
Old 09-19-2009, 12:17 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
nope wrong again
Code:
chat tmp;
read(fd,&tmp,1);
printf("0x%X", tmp);
is correct
 
Old 09-19-2009, 02:45 PM   #4
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by smeezekitty View Post
nope wrong again
Code:
chat tmp;
read(fd,&tmp,1);
printf("0x%X", tmp);
is correct
Right, my fault. Thats what I have in my program (except I used a char not a chat ), just a typo when I posted.
 
Old 09-19-2009, 07:16 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
no the chat was my fault
of cource it means char LOL
 
Old 09-20-2009, 01:35 AM   #6
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by smeezekitty View Post
no the chat was my fault
of cource it means char LOL
I figured. Typos are acceptable.
 
  


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
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
byte to byte remote comparison louisJ Linux - Newbie 3 09-21-2007 05:28 PM
writing and reading at same time dideas Programming 16 11-13-2006 06:49 PM
reading a line at a time in a file topcat Programming 2 12-14-2004 05:49 AM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM

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

All times are GMT -5. The time now is 02:59 PM.

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