Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error converting Int64 to Int32 and vice versa

Error converting Int64 to Int32 and vice versa

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    I'm using the following code to roundtrip a value between one Int64 var and two In32 vars.

    public static Int64 Int64FromPersistentIDs(int highID, int lowID)
    {
    return (((Int64)highID << 32) + (Int64)lowID);
    }

    public static void PersistentIDsFromInt64(Int64 longID, ref int highID, ref int lowID)
    {
    lowID = (int)(longID & 0xFFFFFFFF);
    highID = (int)((longID >> 32) & 0xFFFFFFFF);
    }

    This code roundtrips well when highID is positive, and when highID is negative and 9 digits, but it fails as soon as highID is negative and 10 digits - the highID comes back as off by one (one too large). What am I doing wrong here?

    H R 2 Replies Last reply
    0
    • J JoeRip

      I'm using the following code to roundtrip a value between one Int64 var and two In32 vars.

      public static Int64 Int64FromPersistentIDs(int highID, int lowID)
      {
      return (((Int64)highID << 32) + (Int64)lowID);
      }

      public static void PersistentIDsFromInt64(Int64 longID, ref int highID, ref int lowID)
      {
      lowID = (int)(longID & 0xFFFFFFFF);
      highID = (int)((longID >> 32) & 0xFFFFFFFF);
      }

      This code roundtrips well when highID is positive, and when highID is negative and 9 digits, but it fails as soon as highID is negative and 10 digits - the highID comes back as off by one (one too large). What am I doing wrong here?

      H Offline
      H Offline
      half life
      wrote on last edited by
      #2

      int64 is up to 19 digits and Max/Min value is +/-9,223,372,036,854,775,808 , where int32 is only 10 and Max/Min value is +/-2,147,483,647 what your'e doing is spilling bits off by (int)(longID & 0xFFFFFFFF); int is Int32 and not int64 :):)

      Have Fun Never forget it

      J 1 Reply Last reply
      0
      • H half life

        int64 is up to 19 digits and Max/Min value is +/-9,223,372,036,854,775,808 , where int32 is only 10 and Max/Min value is +/-2,147,483,647 what your'e doing is spilling bits off by (int)(longID & 0xFFFFFFFF); int is Int32 and not int64 :):)

        Have Fun Never forget it

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        Actually, that wasn't it. I think. I went back and changed all Int64 to UInt64, and it stopped failing in all cases.

        1 Reply Last reply
        0
        • J JoeRip

          I'm using the following code to roundtrip a value between one Int64 var and two In32 vars.

          public static Int64 Int64FromPersistentIDs(int highID, int lowID)
          {
          return (((Int64)highID << 32) + (Int64)lowID);
          }

          public static void PersistentIDsFromInt64(Int64 longID, ref int highID, ref int lowID)
          {
          lowID = (int)(longID & 0xFFFFFFFF);
          highID = (int)((longID >> 32) & 0xFFFFFFFF);
          }

          This code roundtrips well when highID is positive, and when highID is negative and 9 digits, but it fails as soon as highID is negative and 10 digits - the highID comes back as off by one (one too large). What am I doing wrong here?

          R Offline
          R Offline
          Robert C Cartaino
          wrote on last edited by
          #4

          Can you give an example that does not work? Using your methods, I plugged in some numbers and, round trip, it works just fine (even without changing the types to unsigned).

          static void Main(string\[\] args)
          {
              int highID = -1234567890;
              int lowID = 123456789;
          
              Console.WriteLine("Build 64-bit ID:");
              long longID = Int64FromPersistentIDs(highID, lowID);
              Console.WriteLine("highID: ({0,20})", highID);  // PrintBits(highID, 32);
              Console.WriteLine(" lowID: ({0,20})", lowID); // PrintBits(lowID, 32);
              Console.WriteLine("longID: ({0,20})", longID); // PrintBits(longID, 64);
          
              Console.WriteLine("\\nExtract-32 bit IDs:");
              PersistentIDsFromInt64(longID, ref highID, ref lowID);
              Console.WriteLine("longID: ({0,20})", longID); // PrintBits(longID, 64);
              Console.WriteLine("highID: ({0,20})", highID); // PrintBits(highID, 32);
              Console.WriteLine(" lowID: ({0,20})", lowID); // PrintBits(lowID, 32);
          }
          
          public static Int64 Int64FromPersistentIDs(int highID, int lowID)
          {
              return (((Int64)highID << 32) + (Int64)lowID);
          }
          
          public static void PersistentIDsFromInt64(Int64 longID, ref int highID, ref int lowID)
          {
              lowID = (int)(longID & 0xFFFFFFFF);
              highID = (int)((longID >> 32) & 0xFFFFFFFF);
          }
          

          Outputs:

          Build 64-bit ID:
          highID: ( -1234567890)
          lowID: ( 123456789)
          longID: (-5302428712118268651)

          Extract-32 bit IDs:
          longID: (-5302428712118268651)
          highID: ( -1234567890)
          lowID: ( 123456789)

          J 1 Reply Last reply
          0
          • R Robert C Cartaino

            Can you give an example that does not work? Using your methods, I plugged in some numbers and, round trip, it works just fine (even without changing the types to unsigned).

            static void Main(string\[\] args)
            {
                int highID = -1234567890;
                int lowID = 123456789;
            
                Console.WriteLine("Build 64-bit ID:");
                long longID = Int64FromPersistentIDs(highID, lowID);
                Console.WriteLine("highID: ({0,20})", highID);  // PrintBits(highID, 32);
                Console.WriteLine(" lowID: ({0,20})", lowID); // PrintBits(lowID, 32);
                Console.WriteLine("longID: ({0,20})", longID); // PrintBits(longID, 64);
            
                Console.WriteLine("\\nExtract-32 bit IDs:");
                PersistentIDsFromInt64(longID, ref highID, ref lowID);
                Console.WriteLine("longID: ({0,20})", longID); // PrintBits(longID, 64);
                Console.WriteLine("highID: ({0,20})", highID); // PrintBits(highID, 32);
                Console.WriteLine(" lowID: ({0,20})", lowID); // PrintBits(lowID, 32);
            }
            
            public static Int64 Int64FromPersistentIDs(int highID, int lowID)
            {
                return (((Int64)highID << 32) + (Int64)lowID);
            }
            
            public static void PersistentIDsFromInt64(Int64 longID, ref int highID, ref int lowID)
            {
                lowID = (int)(longID & 0xFFFFFFFF);
                highID = (int)((longID >> 32) & 0xFFFFFFFF);
            }
            

            Outputs:

            Build 64-bit ID:
            highID: ( -1234567890)
            lowID: ( 123456789)
            longID: (-5302428712118268651)

            Extract-32 bit IDs:
            longID: (-5302428712118268651)
            highID: ( -1234567890)
            lowID: ( 123456789)

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            Sure: The following High / Low combination: -1173074781 / -1181223073 was round-tripping back as -1173074782 / -1181223073 (note the High Order output is one more than the input.) I determined that that code which builds the 64bit INT was the culprit, here's why. These IDs are passed to me by an application that actually stores a 64 bit number in it's XML database. However, instead of passing me the 64bit number, it passes me two 32 bit numbers. I looked inside the database for the record I was querying, and the 64bit number stored there is: BA144CA3B997F75F But my code (return (((Int64)highID << 32) + (Int64)lowID)) was returning BA144CA2B997F75F (note the difference at the 8th digit - the 2 should be a three) So I was getting a bad 64 bit number from my code. I changed my units to UInt64, and my code started returning a 64 bit number which matched that number stored in the database. I don't know exactly why it was failing, but I could tell it was a "size" issue, because some inputs were returning the correct 64 bit numbers (as validated in the database), and some were not.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups