?
Solved

cast long to char array?

Posted on 2003-02-25
18
Medium Priority
?
2,464 Views
Last Modified: 2007-12-19
I have a long int and want to put it into a char array. e.g.

long numb1 = 0x F0A3 32E2 B0FF FFFF;
unsigned char numb2[8];

//magic conversion code!

then numb2[0] = F0, numb2[1] = A3, etc.

This question isn't particularly concise but I'm sure you can work it out!
0
Comment
Question by:as2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8019570
So, what you really want is the hex values for each byte.

Now, do you want it as the number is expressed on your machine (endian issues) or just as the actual hex number would be interpreted?

To get the hex, just sprintf it...

sprintf(numb2, "%x", numb1);  but I don't think that is what you are getting at, is it?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8019590
A few other questions -

what platform are you on - it looks like a 64-bit one?
0
 
LVL 3

Expert Comment

by:marcjb
ID: 8019615
First, numb1 should be:

long long numb1 = 0xF0A332E2B0FFFFFF;

You need 8 bytes (64 bits) to store this number.  Then, define numb2 to be 9 bytes in length, and you can use 'sprintf' to write to the buffer (sprintf will append a NULL).

Hope this helps,

Marc
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8019698
marcjb -

what is:

long long numb1 = ....; - sure ain't any C I know of...

Anyhow, that's why I asked if he was on a 64-bit machine, in which case he is OK (and not overflowing his values)...

And, as I said, sprintf will give him his number in hex (the code I showed will do that), but it won't guarantee the machine representation, depending upon the endian of his machine...

Lastly, he better define numb2 to be 17 in length, not 9, or he will overflow it during sprintf.  It also should not be unsigned - didn't catch that in my first post...

e.g.

char numb2[17];
0
 
LVL 6

Expert Comment

by:gj62
ID: 8019763
Oh, one other note.

If you use

sprintf(numb2, "%x", numb1);  

you'll get

f0a332e2b0ffffff

if you use

sprintf(numb2, "%X", numb1);  

you'll get

F0A332E2B0FFFFFF
0
 
LVL 3

Expert Comment

by:marcjb
ID: 8019816
C99, the current C Standard, uses 'long long' for 8 byte integers.  'long' only refers to 4 bytes (32 bits).  Many of the current compilers, including gcc, already support (and have for some time) the use of 'long long'.

As for numb2, yes, it should be 17 bytes.  16 for the data, and 1 for the NULL.

0
 
LVL 6

Expert Comment

by:gj62
ID: 8019846
I stand corrected on that (I work on 32-bit platforms mostly).  Sorry 'bout that...  I heard that and forgot all about it...
0
 
LVL 1

Accepted Solution

by:
catbutt earned 84 total points
ID: 8019851
I'm guessing you don't want hex values, you want "short short ints" (unsigned chars which are really values between 0 and 0xff).

Are you sure you need 8 characters?  On most platforms you will need just 4 (32 bits)

If you are on a 32 bit platform, you can try just doing a memcpy.  Depending on byte ordering that may work for you:

long numb1 = 0xB0FFFFFF;
unsigned char numb2[4];

memcpy (numb2, numb1, sizeof(numb1));

If you need to change the byte order, look at htonl(), which will put it in "network order" for you:

long tmp = htonl (numb1);
memcpy (numb2, tmp, sizeof(numb1));

0
 
LVL 3

Expert Comment

by:marcjb
ID: 8019858
As a side note, I would suggest using a flag and field width specifier with sprintf in case you numbers with leading 0's.

0
 
LVL 1

Expert Comment

by:catbutt
ID: 8019860
sorry you should put a & before the second parameter to memcpy:

memcpy (numb2, &numb1, sizeof(numb1));
and
memcpy (numb2, &tmp, sizeof(numb1));
0
 
LVL 1

Expert Comment

by:catbutt
ID: 8019869
sorry you should put a & before the second parameter to memcpy:

memcpy (numb2, &numb1, sizeof(numb1));
and
memcpy (numb2, &tmp, sizeof(numb1));
0
 
LVL 3

Expert Comment

by:marcjb
ID: 8019884
No problem, gj62.  Actually, I think catbutt is correct and as2003 really just wants the values, not a printable representation.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8019949
Yep, marcjb, you are right.

If he  needed to reference it that way often if the value changes, he could just union it to a char[8] as well - though that would give him host order, not network order, right?
0
 
LVL 1

Expert Comment

by:catbutt
ID: 8019960
If you are actually working on a 64 but machine, to reverse byte order you can do something like this:

long numb1 = 0xF0A332E2B0FFFFFF;
unsigned char numb2[8];

memcpy (numb2, numb1, sizeof(numb1));

char tmp;

tmp = numb2[0];
numb2[0] = numb2[7];
numb[7] = tmp;

tmp = numb2[1];
numb2[1] = numb2[6];
numb[6] = tmp;

tmp = numb2[2];
numb2[2] = numb2[5];
numb[5] = tmp;

tmp = numb2[3];
numb2[3] = numb2[4];
numb[4] = tmp;



Of course, you may not have to do this, depending on the byte ordering of your machine.  (if its "little endian", you can use it as is without reversing it)
0
 
LVL 1

Expert Comment

by:catbutt
ID: 8019977
(man I need more coffee today :) ....again, add the & where needed, and change "numb" to "numb2" where I didn't)
0
 
LVL 6

Expert Comment

by:gj62
ID: 8020103
Couldn't you also do:

union uLongChar
{
   unsigned long long l;
   unsigned char hexChars[8];
};

union uLongChar test;
test.l = 0xF0A332E2B0FFFFFF;

printf("%2x",test.hexChars[0]); // etc to print out the hex?

I understand this would give host order...
0
 
LVL 4

Author Comment

by:as2003
ID: 8021051
thanks catbutt, i'm sure there should be some easy cast but memcpy will do perfectly!
0
 
LVL 1

Expert Comment

by:catbutt
ID: 8021212
you can do this if you prefer it to memcpy:

*((long *)numb2) = numb1;

although its probably just a matter of preference/readability, most compilers will create the same code with memcpy as with this.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question