Hex addition and Hex-to-ASCII string conversion with VC++
-
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(); }
-
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(); }
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?
-
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?
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.;)
-
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.;)
Thanks for the feedback!