LinuxQuestions.org
Help answer threads with 0 replies.
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-07-2004, 10:10 PM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
C questino breaking down long int


have a long int = 0X0c00a8c0

want to break it down to 4 integer
int1 = 0x0c
int2 = 0x00
int3 = 0xa8
int4 = 0xc0

how do I do it?
 
Old 09-07-2004, 10:44 PM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Here's one way (you're actually splitting it into bytes, not ints...):

long int a = 0x0c00a8c0;
int int1 = (int)(a >> 24);
int int2 = (int)((a >> 16) & 255) ;
int int3 = (int)((a >> 8) & 255 );
int int4 = (int)(a & 255);

I'm sure there's a more direct way to access the bytes through pointers, but it would be less portable (big-endian vs little-endian).
 
Old 09-09-2004, 12:08 AM   #3
kingtas
Member
 
Registered: Sep 2004
Distribution: RH9
Posts: 36

Rep: Reputation: 15
Convert int to string for parsing digits using the itoa command.

#include<stdio.h>
#include<stdlib.h>

void main()
{
int a=123,i;
char b[10];
itoa(a, b, 10);

for(i=0; i<3; i++)
printf("%c", b[i]);
}

//itoa = integer to alpha (defined in stdlib.h)
//first parameter is the number to be parsed
//second is the pointer variable which holds the parsed value
//third parameter is the base of the number which is parsed
//here 123 is considered to be in base 10
 
Old 09-09-2004, 04:58 AM   #4
phoenix7
Member
 
Registered: Aug 2004
Distribution: Mandrake 10.2(Mandriva), SuSE 9.3, Slackware 9.1, Xandros 3.1, Knoppix 3.9, FreeBSD 5.3, RedHat9
Posts: 122

Rep: Reputation: 15
Hi
You can this fragment of code.

Code:
#include <stdio.h>

int main (void)
{
    long int a = 0X0c00a8c0;
    unsigned char* c = (unsigned char*) (&a);
    int A [4];
    for (int i = 0; i <= 3; i ++)
    {
        A [i] = *c;
        printf ("0X%2X\n", *c);
	c ++;
    }
    return 0;
}
Cheers,
Mohammad
 
Old 09-09-2004, 05:13 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Mohammed's method shows exactly what I was talking about - if you run it on an Intel chip, the numbers come out backwards (0xC0 first, then 0xA8, etc), but if you compile and run the same code on (for example) a 68000 chip, it will be the other way around. So if you want to write portable code, don't do that
 
  


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
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
convert string to long/int alaios Programming 10 09-15-2005 09:01 AM
How to print unsigned long int alaios Programming 6 07-14-2005 10:27 AM
int value larger then unsigned long jpc82 Programming 3 03-12-2004 03:54 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM

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

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