long integers and byte order

This is a discussion on long integers and byte order within the C Programming forums, part of the General Programming Boards category; Thanks grumpy and sorry about the conversion error for 6844. My long int is always under 65x10^6 (e.g. less than ...

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Thanks grumpy and sorry about the conversion error for 6844.

    My long int is always under 65x10^6 (e.g. less than 80000000) so I assume I don't need to worry about signs.

    I settled on a method that does not invoke dividing to shift out the bytes and it seems work. This is because although the long int bytes are stored in reversed order, the nibbles are in proper order.
    The routine which I have been using and seems to work is shown below.
    I think it is a bit verbose-I thought that perhaps C and its functions provided a "nice" way to simply convert long to hex in a buffer.

    Thanks again-
    Fritz

    Code:
    void cvt_long_to_hex(long int lngint, char hexout[4])
    {
       long int *liptr;
       char *cptr;
    	liptr=&lngint;
    	cptr=(char*)liptr;
       hexout[0]=cptr[3];
       hexout[1]=cptr[2];
       hexout[2]=cptr[1];
       hexout[3]=cptr[0];
    } //end cvt_long_to_hex

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sonnichs View Post
    Thanks grumpy and sorry about the conversion error for 6844.

    My long int is always under 65x10^6 (e.g. less than 80000000) so I assume I don't need to worry about signs.

    I settled on a method that does not invoke dividing to shift out the bytes and it seems work. This is because although the long int bytes are stored in reversed order, the nibbles are in proper order.
    The routine which I have been using and seems to work is shown below.
    I think it is a bit verbose-I thought that perhaps C and its functions provided a "nice" way to simply convert long to hex in a buffer.

    Thanks again-
    Fritz

    Code:
    void cvt_long_to_hex(long int lngint, char hexout[4])
    {
       long int *liptr;
       char *cptr;
    	liptr=&lngint;
    	cptr=(char*)liptr;
       hexout[0]=cptr[3];
       hexout[1]=cptr[2];
       hexout[2]=cptr[1];
       hexout[3]=cptr[0];
    } //end cvt_long_to_hex
    Your hexout array needs to be "unsigned char" ... although you never hit the sign bit in the integer value you almost certainly will hit it in one or more of the extracted bytes.

  3. #18
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Hmmmm-the reason I did not want to use unsigned was because the serial port routines on this processor complain about it. However, when I set my long int to X'11FFFFFFFF', the result in the buffer is the same.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sonnichs View Post
    Hmmmm-the reason I did not want to use unsigned was because the serial port routines on this processor complain about it. However, when I set my long int to X'11FFFFFFFF', the result in the buffer is the same.
    If you are pulling bytes directly out of the buffer and making no other use of them signed vs unsigned may not matter... however if you were looking for the specific value of one byte it could magically be transformed to a negative number...

  5. #20
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    "magically be transformed to a negative number"
    Yuk-now I remember while I used to have the patience to suffer with assembler!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conver long to 8 byte array and back to long
    By plopes in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 01:39 AM
  2. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 06:40 AM
  3. changing byte order of an unsigned long?
    By stustu92 in forum C Programming
    Replies: 6
    Last Post: 01-27-2006, 06:21 AM
  4. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 01:50 PM
  5. byte to long
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-22-2001, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21