LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-11-2004, 01:28 AM   #1
demer1
LQ Newbie
 
Registered: Feb 2004
Posts: 2

Rep: Reputation: 0
small C hangup.. Sparc platform


Hey everyone,

As a small part of a program that I am currently writing, I am trying to read into a function a single int in machine language (1's and 0's) and shoot this number out as a char array in base 16. I am not aware of any library functions for doing this, and quite honestly, would prefer just coding it without functions. I am on a Sparc platform and coding in C.

Thanks.
 
Old 02-11-2004, 01:56 AM   #2
demer1
LQ Newbie
 
Registered: Feb 2004
Posts: 2

Original Poster
Rep: Reputation: 0
follow up question as well

Now that my curiousity has been piqued, is their a way (without using atoi) to get the binary value from something along the lines of :

char A[]="ADF123";

which would be something to the tune of 1010 1110 1111 0001 0010 0011

My cse courses never went over any of this in detail, so hopefully someone on here could provide some insight.

Thanks
 
Old 02-13-2004, 10:23 PM   #3
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Well, *all* data in C (and any other language for that matter) is already in binary; you just have to know how to print it out that way. If you want to read in a string of characters (1 and 0) representing a binary number, you'd probably have to write a function to do it, since I don't know of any standard C functions for it. A while loop could do the job too, but a function would be better so you could re-use it

Something along the lines of:

int result = 0;
while (more characters)
result *= 2;
if (1 was read), result += 1;
loop

As for printing in hexadecimal, check the manpage for printf()

Your second question could work along the same lines as the first:

int result = 0;
while (more characters)
result *= 16; // since it's base-16
result += current_digit_value; // hex digit converted to an int
loop

Converting the character from hexadecimal notation to an integer is the only slightly tricky part; something like this maybe:

if ( digit >= '0' && digit <= '9' ) // if digit is between 0 and 9
value = digit - '0'; // subtract ascii value of '0' from digit; '0' - '0' = 0
else if ( digit >= 'a' && digit <= 'f' )
value = digit - 'a' + 10; // since 'a' - 'a' = 0, add 10 to get value of 'a', etc.

Last edited by wapcaplet; 02-13-2004 at 10:37 PM.
 
  


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
Replaciing java platform by Sun's Java platform machan Linux - Newbie 1 11-11-2005 01:55 AM
Linux For Sun Sparc Platform bono02 Linux - Newbie 1 06-23-2005 06:57 AM
hangup problem ohlinux Red Hat 2 07-31-2004 08:36 AM
How can i send message from Linux platform to Windows platform linuxeagle Linux - Networking 1 02-17-2004 12:35 AM
modem hangup swekalle Slackware 4 12-26-2002 06:59 PM


All times are GMT -5. The time now is 05:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration