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. Why do I need typecast hex?

Why do I need typecast hex?

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    This is silly , but most I/O devices spec sheets data are written as "hex". If I want to pass this char *TXBuffer = (char*) 0x04; to a function it has to be type casted. Like so

        char \*TXBuffer = (char\*) 0x04;
    int  \*TEST = (int\*)0x04;
    

    If not I'll get "invalid conversion". Cheers Vaclav

    L 1 Reply Last reply
    0
    • V Vaclav_

      This is silly , but most I/O devices spec sheets data are written as "hex". If I want to pass this char *TXBuffer = (char*) 0x04; to a function it has to be type casted. Like so

          char \*TXBuffer = (char\*) 0x04;
      int  \*TEST = (int\*)0x04;
      

      If not I'll get "invalid conversion". Cheers Vaclav

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Vaclav_ wrote:

      char *TXBuffer = (char*) 0x04;

      No you don't need to do that, and you shouldn't because it is wrong. You are trying to pass the value 0x04 as the buffer address, which will cause an access violation. If you need a buffer containing the hex value, and you want to send that buffer's address to a function then you need to do one or other of the following:

      unsigned char TXBuffer = 0x04; // a single character
      function(&TXBuffer); // use the addressOf operator to pass the address of the character to the function

      // or

      unsigned char TXBuffer[] = { 0x04, 0 }; // multiple characters
      function(TXBuffer); // TXBuffer is an array, so its name will be translated to its address by the compiler.

      // the receiving function should be coded as
      xxx function(unsigned char* buffer)
      {
      // xxx is the return type
      // the function must know how many characters will be sent in the buffer
      // either a number that is always the same, or the length passed as a second parameter.
      // to access items from the buffer you use:
      unsigned char value = *buffer; // get the first value
      buffer++; // increment the pointer to the next element (if more than 1)
      value = *buffer; // get the next value
      }

      V 1 Reply Last reply
      0
      • L Lost User

        Vaclav_ wrote:

        char *TXBuffer = (char*) 0x04;

        No you don't need to do that, and you shouldn't because it is wrong. You are trying to pass the value 0x04 as the buffer address, which will cause an access violation. If you need a buffer containing the hex value, and you want to send that buffer's address to a function then you need to do one or other of the following:

        unsigned char TXBuffer = 0x04; // a single character
        function(&TXBuffer); // use the addressOf operator to pass the address of the character to the function

        // or

        unsigned char TXBuffer[] = { 0x04, 0 }; // multiple characters
        function(TXBuffer); // TXBuffer is an array, so its name will be translated to its address by the compiler.

        // the receiving function should be coded as
        xxx function(unsigned char* buffer)
        {
        // xxx is the return type
        // the function must know how many characters will be sent in the buffer
        // either a number that is always the same, or the length passed as a second parameter.
        // to access items from the buffer you use:
        unsigned char value = *buffer; // get the first value
        buffer++; // increment the pointer to the next element (if more than 1)
        value = *buffer; // get the next value
        }

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        Thanks Richard, you are a pal. Appreciate your comments.

        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