Endian problem

This is a discussion on Endian problem within the C++ Programming forums, part of the General Programming Boards category; how would i convert a Hex Endian to an int example: 0100 = 256 0001 = 1 etc... please help!!...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    Angry Endian problem

    how would i convert a Hex Endian to an int example:

    0100 = 256
    0001 = 1

    etc...

    please help!!

  2. #2
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Sounds like a school project... you need to know the value of every character from 0..9 and A..F, you could use an array for that. Also, you need to know that each value to left of a another value is worth 16x its value (just like the 100's place in decimal is worth 10x the 10's place, since it is one digit to the left). This should be enough knowledge to get you going in the right direction.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    confused!

    i don't understand 00 = to the left right which = 0 and 01 = to the right so 00 < 01
    so how can you do that ? confusing ! shouldnt there be a function for it to make life easyer?

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    You have me confused now... are you speaking about converting a hex string into an integer, or coverting two adjacent bytes in memory into an integer?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    hex

    converting a Hex string to integer, sorry

  6. #6
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Hex strings are just like decimal strings - the numbers to the left (that appear first in the string) are the more significant numbers. I would try and make a program that converts a decimal string into an integer first, and then doing it with hex digits becomes elementary.

    The first problem is that you don't know how large the string is (and you don't need to call a function to get the strings length). Each additional digit means that the digits you already look at are 1 digit to left than you originally thought - and what happens when you move all digits 1 to the left?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    printf("%d",0x0100"); works but how would i make it use a string like printf("%d%s","0x","0100); ?

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    ah sorry about that last post

    printf("%d",0x0001) - works so how would i do

    char *HexStr="0001";
    printf("%d%s","0x",HexStr)?

  9. #9
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Are you asking how to print an integer in hexadecimal notation? If so, then you wish to convert an integer into a hex string - not convert a hex string into an integer. Please state what you are trying to do exactly to resolve the confusion.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    sorry about the confusin and wasteing your time, i've given up need sleep

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    GŲteborg, Sweden
    Posts
    2,072
    Here's one way to do it:
    Code:
    int hexStringToInt(const std::string& str)
    {
      int num = 0;
      for (int p=0;p<str.length();++p)
      {
        num *= 16;
        if (num >= '0' && num <= '9')
          num += str[p] - '0';
        else // a...f
          num += std::tolower(str[p]) - 'a' + 10;
      }
      return num;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Away.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    From the source example
    Note: Yes, I used GOTOs in this program.
    Don't give me grief over it. I needed to concentrate on other homework, and take a "B" in this assignment.
    It also uses void main. I'd say they get a C-, not a B in that assignment
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    hmm can some one tell me if these functions look ok please?
    thanks

    Code:
    int BigEndian8(char *data,int start)
    {
    	unsigned short endian;
    		endian = data[start];
    	return endian;
    }
    
    
    int BigEndian16(char *data,int start)
    {
    	unsigned char Big;
    	unsigned char Little;
    	unsigned short endian;
    		Big = data[start+1];
    		Little = data[start];
    		endian = (Big << 8) + Little;
    	return endian;
    }
    
    int BigEndian32(char *data,int start)
    {
    	unsigned char Big;
    	unsigned char Little;
    	unsigned short endian;
    		Big = data[start+1];
    		Little = data[start];
    		endian = (Big << 8) + Little;
    
    		Big = data[start+3];
    		Little = data[start+2];
    		endian = endian + (Big << 24) + Little;
    	return endian;
    }

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    hmm another Question: how do i get the length of a packet with null's in example of a packet:

    <"name"><null><endian><data><null>

    if i do strlen(packet) it returns 4 when it's really > 4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. Odd Pointer Problem
    By loopshot in forum C Programming
    Replies: 4
    Last Post: 10-26-2006, 09:49 PM

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