|
-
Jul 7th, 2002, 08:10 PM
#1
converting long to char
I have a long that is under 255 and I want to convert it to a char.
I'm loading into into an array of char's but only want this first number to go into the first element of the array.
When I use ltoa I end up with
array[0] = 2
array[1] = 5
array[2] = 5
I'd like
array[0] = 255
THanks
-
Jul 7th, 2002, 09:44 PM
#2
Hyperactive Member
is your question how to asign a char to a long(ex. 'a'=97) then
long lNumber=255;
char szArray[10];
szArray[0]=(char)lNumber;
-
Jul 8th, 2002, 12:13 AM
#3
PowerPoster
I think you can do this way:
PHP Code:
long lNumber = 255;
char szArray[20];
itoa((int)lNumber, szArray,10);
Not sure if it's "itoa" or "atoi".
-
Jul 8th, 2002, 04:36 PM
#4
Lively Member
long int num;
char ch[10];
ch[0]=(char)num;
if you need a fast way to split the number between the array's chars you can use:
ch[0]=(char)(num&0x000000ff); \\save the first byte
ch[1]=(char)(num&0x0000ff00); \\save the second byte
ch[2]=(char)(num&0x00ff0000); \\save the third byte
ch[3]=(char)(num&0xff000000); \\save the fourth byte
that way you'll be able to save more then one char on a long number.
to save the chars to it use:
num=char; //where char is the first number you want to save
num+=char<<8; //that'l save the first char and add another one
num+=char<<16; //adds the third one
num+=char<<24; //adds the forth one
-
Jul 9th, 2002, 01:55 AM
#5
transcendental analytic
[QUOTE]Originally posted by Mr.Fixit
[B]long int num;
char ch[10];
ch[0]=(char)num;
if you need a fast way to split the number between the array's chars you can use:
ch[0]=(char)(num&0x000000ff); \\save the first byte
ch[1]=(char)(num&0x0000ff00); \\save the second byte
ch[2]=(char)(num&0x00ff0000); \\save the third byte
ch[3]=(char)(num&0xff000000); \\save the fourth byte
ch[1] to ch[3] would be 0 since the chars would only concern the low byte.
*(long*)ch=num;
would do (and be a lot faster), the reverse process would be
num=*(long*)ch;
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 9th, 2002, 01:37 PM
#6
Lively Member
my mistake there:
needed to SHR after the masking...
(>>8 for the second byte >>16 for the third and so on)
but your way seem to do the job with greater clearity and ease ;-)
-
Jul 9th, 2002, 01:38 PM
#7
transcendental analytic
yeah but if you want them in reverse order then your way would be best
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|