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 / C++ / MFC
  4. Char[1] to UINT ???

Char[1] to UINT ???

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
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.
  • W Offline
    W Offline
    wow9999
    wrote on last edited by
    #1

    Hi How to convert the Char[1] to UINT? Thanks

    T 1 Reply Last reply
    0
    • W wow9999

      Hi How to convert the Char[1] to UINT? Thanks

      T Offline
      T Offline
      Terry ONolley
      wrote on last edited by
      #2

      When you define a variable as char[1] rather than just char: char CharArray[1]; rather than char SingleChar; you have changed a lot! Even though the array you defined has only 1 element (making it the same as a single char memory usage-wise), it is referenced the same way as if you had defined it as char[1000] - ie as the memory address of its first (zero-eth) element. Since the value of CharArray is a memory address (in other words CharArray is a pointer), it is not compatible for direct assignment to an UINT. So, to convert a char[1] to an UINT you need to specify which element of the array you are talking about. In the case of an array of chars with 1 element, the only valid reference is the zeroeth element - CharArray[0]. Whenever you assign a variable that has a data type that is different from the one you are assigning it to, you need to cast it. "Cast" just means that you are basically telling the compiler "Thanks for the warnings, but I know what I'm doing. I know they were defined as different types, but I really want to do this, so butt out!". Casting will only work if the 2 data types are compatible. Since an UINT is large enough to hold the largest possible char and both data types are non-abstract, the types are compatible and your casting will succeed: char CharArray[1]; // has only 1 valid element - referenced as CharArray[0] char SingleChar; UINT x; // to assign a value to this array you must reference which element you are assigning CharArray[0] = 'A'; // since this is a char and not an array you can assign it directly SingleChar = 'A'; // Just cast the assignment by placing the data type you are casting (converting it to) in parentheses before the assignment. x = (UINT)SingleChar; x = (UINT)C[0];



      W 1 Reply Last reply
      0
      • T Terry ONolley

        When you define a variable as char[1] rather than just char: char CharArray[1]; rather than char SingleChar; you have changed a lot! Even though the array you defined has only 1 element (making it the same as a single char memory usage-wise), it is referenced the same way as if you had defined it as char[1000] - ie as the memory address of its first (zero-eth) element. Since the value of CharArray is a memory address (in other words CharArray is a pointer), it is not compatible for direct assignment to an UINT. So, to convert a char[1] to an UINT you need to specify which element of the array you are talking about. In the case of an array of chars with 1 element, the only valid reference is the zeroeth element - CharArray[0]. Whenever you assign a variable that has a data type that is different from the one you are assigning it to, you need to cast it. "Cast" just means that you are basically telling the compiler "Thanks for the warnings, but I know what I'm doing. I know they were defined as different types, but I really want to do this, so butt out!". Casting will only work if the 2 data types are compatible. Since an UINT is large enough to hold the largest possible char and both data types are non-abstract, the types are compatible and your casting will succeed: char CharArray[1]; // has only 1 valid element - referenced as CharArray[0] char SingleChar; UINT x; // to assign a value to this array you must reference which element you are assigning CharArray[0] = 'A'; // since this is a char and not an array you can assign it directly SingleChar = 'A'; // Just cast the assignment by placing the data type you are casting (converting it to) in parentheses before the assignment. x = (UINT)SingleChar; x = (UINT)C[0];



        W Offline
        W Offline
        wow9999
        wrote on last edited by
        #3

        Thanks for your reply. CString aStr = "中文";//a Chinese CString char cChinese[3]; cChinese[2] = '\0'; int i = 0; while (i= 0xA1 && (byte)aStr[i] <=0xFE) { cChinese[0] = aStr[i]; cChinese[1] = aStr[i+1]; UINT aWord = cChinese // is it possible to assign the arrary to UINT ++i; } ++i; }

        J T 2 Replies Last reply
        0
        • W wow9999

          Thanks for your reply. CString aStr = "中文";//a Chinese CString char cChinese[3]; cChinese[2] = '\0'; int i = 0; while (i= 0xA1 && (byte)aStr[i] <=0xFE) { cChinese[0] = aStr[i]; cChinese[1] = aStr[i+1]; UINT aWord = cChinese // is it possible to assign the arrary to UINT ++i; } ++i; }

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          wow9999 wrote: char cChinese[3]; UINT aWord = cChinese; Here, you are trying to put the address of your chinese character into the UINT variable. You could force the compiler to do that, but it probably makes no sense here. What are you trying to achive here? Assign one chinese letter to the UINT? Then you need to write something like UINT aWord = cChinese[i]; That means you need to say which letter you want to assign.


          Who is 'General Failure'? And why is he reading my harddisk?!?

          1 Reply Last reply
          0
          • W wow9999

            Thanks for your reply. CString aStr = "中文";//a Chinese CString char cChinese[3]; cChinese[2] = '\0'; int i = 0; while (i= 0xA1 && (byte)aStr[i] <=0xFE) { cChinese[0] = aStr[i]; cChinese[1] = aStr[i+1]; UINT aWord = cChinese // is it possible to assign the arrary to UINT ++i; } ++i; }

            T Offline
            T Offline
            Terry ONolley
            wrote on last edited by
            #5

            wow9999 wrote: UINT aWord = cChinese // is it possible to assign the arrary to UINT Yes (at least under VC++ 6.0). You would need to write it as so: UINT aWord = (UINT)cChinese; This is what I said in my previous post. You need to cast it. But the real question is why? When you assign the value of cChinese to an UINT you are not assigning the value of any char in cChinese - you are only copying the address of the zeroeth element. If you are trying to look at or manipulate the character codes for the Chinese character set, then you need to assign cChinese[0] to aWord and not cChinese.



            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