You are not logged in.

#1 2013-01-08 04:22:11

leetow2003
Member
Registered: 2012-04-08
Posts: 25

how to convert char from long

I read a book,it said:

long addr; 
char buf[4];
((long)&(buf[0]))=addr;

when I compile it,it doesn't pass,
how to correct it?

Offline

#2 2013-01-08 04:38:52

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,784

Re: how to convert char from long

leetow2003 wrote:

I read a book,it said:

long addr; 
char buf[4];
((long)&(buf[0]))=addr;

when I compile it,it doesn't pass,
how to correct it?

That is not going to work. 
char buf[4]; and long addr; both allocate space on the stack. 
you then try to change the address of buf[0], but do nothing with the space that had been allocated.

The following is not a good idea either, but it might work:

long addr;
char* buf;
buf = (char*)(& addr);

But, is this a big endian or little endian machine?  How many bytes are in a long?  Do you think you really want unsigned char?

edit:  BTW, what book?

Last edited by ewaller (2013-01-08 04:40:25)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2013-01-08 15:35:58

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: how to convert char from long

leetow2003 wrote:

I read a book,it said:

long addr; 
char buf[4];
((long)&(buf[0]))=addr;

when I compile it,it doesn't pass,
how to correct it?

What your book probably meant to do was

*( (long *) buf ) = addr;

(Note that buf in this case is equivalent to &buf[0], so I'm not sure why the book would use the more complex form.)

This should normally compile, but there are still many things wrong with the code. addr is never initialized, for instance, so you're off to a bad start. It assumes sizeof(long) == 4, which is not the case for Linux on x86_64. Unless you know the endianness of the platform you're on, you won't be sure which byte buf[0] corresponds to. Furthermore, long and char have different alignment requirements, so you could end up writing to (for instance) buf[-2..1] instead of buf[0..3]. Finally, on platforms where char is signed, you might find some unusual behavior when you try to use the contents of buf.

But the real problem with code like this is that it's rarely necessary or useful to know about the internal representation of numbers in C. If you find yourself writing code like this, take a step back and ask yourself whether it's really necessary to fiddle with bytes and representations when C provides high-level features that normally preclude that kind of unportable knowledge. In short, get a different book.

Offline

#4 2013-01-08 15:47:01

dammannj
Member
Registered: 2009-01-28
Posts: 44

Re: how to convert char from long

Trent, you beat me to it tongue

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

int main()
{
	long addr = 0x0102030405060708;
	char buf[sizeof(long)];
	int i;
	*((long *) buf) = addr;
	printf("sizeof(long) = %d\n",sizeof(long));
	printf("addr hex: %#lx\n", addr);
	printf("buf  hex: %#x%#x%#x%#x%#x%#x%#x%#x\n", buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
	return(0);
}

What I would like to add: it may not be useful to actually use that kind of code; but knowing about that kind of low level stuff and endianess especially is very important imho.

Offline

#5 2013-01-08 15:55:11

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: how to convert char from long

dammannj wrote:

What I would like to add: it may not be useful to actually use that kind of code; but knowing about that kind of low level stuff and endianess especially is very important imho.

It's occasionally useful, sure, but more in a "this is why we do it that way" conceptual sense than as actual practical knowledge. If alignment issues figure prominently in your code, you might be better served by assembly than C.

Offline

#6 2013-02-07 04:37:22

Paruga
Member
From: Germany
Registered: 2013-02-07
Posts: 8
Website

Re: how to convert char from long

union {
long addr;
char char_addr[sizeof(long)];
};

Last edited by Paruga (2013-02-07 04:37:48)


---sometimes the obvious isn't there!---

Offline

Board footer

Powered by FluxBB