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. Hex addition and Hex-to-ASCII string conversion with VC++

Hex addition and Hex-to-ASCII string conversion with VC++

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpc++tutorial
4 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.
  • P Offline
    P Offline
    pkyiu
    wrote on last edited by
    #1

    Hello everybody, I am a newbie to C++ programming and I need to do some hex addition with VC++ and then output the result as ASCII. Here is my problem: hex1(5A 30 35 30 30) which is equal to decimal(387355848752), I want to add hex(100) to hex1, result is hex2(5A 30 35 31 30) equal to decimal(387355849008). This is exactly equal to adding 256 to the first decimal. I know unsigned int data type can only support 4294967295 which is still not enough for my case, and I would like to know how to convert these hex to ASCII with VC++? (Help received! Thx) Besides, I need to convert the output to ASCII string format, which is "Z0500" and "Z0510" respectively, how can I do that? Your help is very much appreciated, thank you!:) Here is the code with help from forum member, but my second question is not yet solve: #include #include #include #include #include #include int main() { long long h; long long h1 = 0x5A30353030LL; long long h2 = 0x5A303F3F30LL; long long h3 = 0x100LL; CString Str; unsigned char Write_Buff[1]; for (h=h1; h<=h2; h+=h3) { _tprintf_s(_T("%I64X\n"), h); Write_Buff[0] = h; Str.Format("0x0%x",Write_Buff[0]); Sleep(10); } _getch(); }

    G 1 Reply Last reply
    0
    • P pkyiu

      Hello everybody, I am a newbie to C++ programming and I need to do some hex addition with VC++ and then output the result as ASCII. Here is my problem: hex1(5A 30 35 30 30) which is equal to decimal(387355848752), I want to add hex(100) to hex1, result is hex2(5A 30 35 31 30) equal to decimal(387355849008). This is exactly equal to adding 256 to the first decimal. I know unsigned int data type can only support 4294967295 which is still not enough for my case, and I would like to know how to convert these hex to ASCII with VC++? (Help received! Thx) Besides, I need to convert the output to ASCII string format, which is "Z0500" and "Z0510" respectively, how can I do that? Your help is very much appreciated, thank you!:) Here is the code with help from forum member, but my second question is not yet solve: #include #include #include #include #include #include int main() { long long h; long long h1 = 0x5A30353030LL; long long h2 = 0x5A303F3F30LL; long long h3 = 0x100LL; CString Str; unsigned char Write_Buff[1]; for (h=h1; h<=h2; h+=h3) { _tprintf_s(_T("%I64X\n"), h); Write_Buff[0] = h; Str.Format("0x0%x",Write_Buff[0]); Sleep(10); } _getch(); }

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      What does "ASCII String Format" mean? I have found ASCII String Format and ASCIIZ searching on the web, and both refer to a simple C String. Here is an example that does that: // Configuration Property: Character Set should not be set to // Unicode, where _TCHAR = char // Or, you can undefine _UNICODE and UNICODE #include #include #include "stdafx.h" #include int _tmain(int argc, _TCHAR* argv[]) { long long h1 = 0x5A30353030LL; long long h2 = 0x100LL; long long h3 = h1 + h2; std::vector<_TCHAR> buffer(256); int result = _stprintf_s(&buffer[0], buffer.size(), _T("%I64X\n"), h3); if (result == - 1) _tprintf_s(_T("_stprintf_s faiiled.\n")); else _tprintf_s(_T("%s"), &buffer[0]); return 0; } Also, is "Z0500" and "Z0510" some kind of command based on the above hex values?

      P 1 Reply Last reply
      0
      • G George L Jackson

        What does "ASCII String Format" mean? I have found ASCII String Format and ASCIIZ searching on the web, and both refer to a simple C String. Here is an example that does that: // Configuration Property: Character Set should not be set to // Unicode, where _TCHAR = char // Or, you can undefine _UNICODE and UNICODE #include #include #include "stdafx.h" #include int _tmain(int argc, _TCHAR* argv[]) { long long h1 = 0x5A30353030LL; long long h2 = 0x100LL; long long h3 = h1 + h2; std::vector<_TCHAR> buffer(256); int result = _stprintf_s(&buffer[0], buffer.size(), _T("%I64X\n"), h3); if (result == - 1) _tprintf_s(_T("_stprintf_s faiiled.\n")); else _tprintf_s(_T("%s"), &buffer[0]); return 0; } Also, is "Z0500" and "Z0510" some kind of command based on the above hex values?

        P Offline
        P Offline
        pkyiu
        wrote on last edited by
        #3

        Hi, Thanks for the reply, here is what I finally wrote since 5A and 30 are kept constant. unsigned long int z; char buf[6]; z = 0x303530; // 5A-303530-30 do { buf[0] = 0x5A; buf[1] = (z & 0xff0000) >> 16; buf[2] = (z & 0xff00) >> 8; buf[3] = (z & 0xff); buf[4] = 0x30; buf[5] = 0x00; printf("%s\n", buf); serial.Write(buf); z = z + 1; if ((z & 0xff) > 0x3f) { z = (z + 0x100) & 0xffff00; z = z + 0x30; if ((z & 0xff00) > 0x3f00) { z = (z + 0x10000) & 0xff00ff; z = z + 0x3000; if ((z & 0xff0000) > 0x3f0000) { z = (z + 0x1000000) & 0x00ffff; z = z + 0x300000; } } } Sleep(150); } while (z <= 0x353130); // 5A-353130-30 It's HEX addaition of 1 and output to RS232 by cserial, thanks anyway.;)

        G 1 Reply Last reply
        0
        • P pkyiu

          Hi, Thanks for the reply, here is what I finally wrote since 5A and 30 are kept constant. unsigned long int z; char buf[6]; z = 0x303530; // 5A-303530-30 do { buf[0] = 0x5A; buf[1] = (z & 0xff0000) >> 16; buf[2] = (z & 0xff00) >> 8; buf[3] = (z & 0xff); buf[4] = 0x30; buf[5] = 0x00; printf("%s\n", buf); serial.Write(buf); z = z + 1; if ((z & 0xff) > 0x3f) { z = (z + 0x100) & 0xffff00; z = z + 0x30; if ((z & 0xff00) > 0x3f00) { z = (z + 0x10000) & 0xff00ff; z = z + 0x3000; if ((z & 0xff0000) > 0x3f0000) { z = (z + 0x1000000) & 0x00ffff; z = z + 0x300000; } } } Sleep(150); } while (z <= 0x353130); // 5A-353130-30 It's HEX addaition of 1 and output to RS232 by cserial, thanks anyway.;)

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          Thanks for the feedback!

          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