Converts a character or string into a binary string (a string of "0" or "1";s).
The use of std::string removes any need for dynamic allocation and pointer
maintenance.
From:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20525179.html
#include <string> using namespace std; string Byte2Binary(unsigned char c) { string S; unsigned char Msk = 0x80; for (int i = 0; i < 8; ++i) { if (c & Msk) S += '1'; else S += '0'; Msk >>= 1; } return S; } string String2Binary(const string &S) { const int StrLen = S.length(); string Result; for (int i = 0; i < StrLen; ++i) Result += Byte2Binary(unsigned char) S[i]); return Result; }
file: /Techref/language/ccpp/cppref/EXAMPLES/2binary_cc.htm, 1KB, , updated: 2006/3/31 16:37, local time: 2024/9/17 05:55,
3.147.69.93:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://piclist.com/techref/language/ccpp/cppref/EXAMPLES/2binary_cc.htm"> C++ Programming Example Convert to Binary</A> |
Did you find what you needed? |
PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.