Results 1 to 7 of 7

Thread: converting long to char

  1. #1
    gommo
    Guest

    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

  2. #2
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    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;

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I think you can do this way:
    PHP Code:
    long lNumber 255;
    char szArray[20];
    itoa((int)lNumberszArray,10); 
    Not sure if it's "itoa" or "atoi".
    Baaaaaaaaah

  4. #4
    Lively Member
    Join Date
    Jun 2002
    Posts
    81
    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

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    [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.

  6. #6
    Lively Member
    Join Date
    Jun 2002
    Posts
    81
    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 ;-)

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width