473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

String Array to Binary Number Conversion

Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?

for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);

where the "CStringToBinary" function is what I am looking to find.
the int representation should be 1 or in hex 0x01
Any help is greatly appreciated.
Delali
Jul 19 '05 #1
4 11553
Delali Dzirasa wrote:
Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?


Here we discuss standard C++. We don't know what 'CString' is.

http://www.slack.net/~shiva/welcome.txt
http://www.parashift.com/c++-faq-lite/
http://www.slack.net/~shiva/offtopic.txt

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2

Delali Dzirasa <De************@jhuapl.edu> wrote in message
news:bj**********@houston.jhuapl.edu...
Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?
There's no 'CString' type in standard C++. I can show you
how to convert a 'std::string' containing text representing
a binary value to an integer type.

You'll need to figure out how to convert from 'CString'
to 'std::string'. This is indeed possible, look up
the 'CString' and 'std::string' member functions.
Also, since there's no way to determine the which binary
representation an implementation uses, it doesn't really make sense
to try to represent the binary pattern of a signed type
like that. So my example below uses an unsigned int instead
of signed.


for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);

where the "CStringToBinary" function is what I am looking to find.
the int representation should be 1 or in hex 0x01
Any help is greatly appreciated.


#include <bitset>
#include <climits>
#include <iostream>
#include <stdexcept>
#include <string>

unsigned int binstr2uint(const std::string& s)
{
const std::string::size_type
max_bits(sizeof(unsigned int) * CHAR_BIT);

if(s.size() > max_bits)
throw "[binstr2uint]: Too many digits in input\n";

return (std::bitset<max_bits>(s).to_ulong());
}

int main()
{
std::string s("101010101");

try
{
unsigned int i(binstr2uint(s));

std::cout << "s == " << s << '\n'
<< "i == " << i << '\n';

}
catch(const char *e)
{
std::cout << e << '\n';
}
catch(std::invalid_argument&)
{
std::cout << "[binstr2uint]: Invalid digit in input\n";
}

return 0;
}
Output:

s == 101010101
i == 341
-Mike

Jul 19 '05 #3
"Delali Dzirasa" <De************@jhuapl.edu> wrote in message news:<bj**********@houston.jhuapl.edu>...
Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?

for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);
where the "CStringToBinary" function is what I am looking to find.

the int representation should be 1 or in hex 0x01
Any help is greatly appreciated.

Delali


int myIntVal = 0;
const char* ptr = myVal;
while (*ptr)
myIntVal += myIntVal + (*ptr++ - '0');

Of course that only works for binary numbers :-)
Jul 19 '05 #4
this worked great!

Thanks!
Delali

"Carl Muller" <ca********@hotmail.com> wrote in message
news:2d*************************@posting.google.co m...
"Delali Dzirasa" <De************@jhuapl.edu> wrote in message

news:<bj**********@houston.jhuapl.edu>...
Does anyone know of the fastest way to convert a CString of numbers ie
"00000001" to the binary representation in an int?

for example

CString myVal = "00000001";

int myIntVal = 0;

myIntVal = CStringToBinary(myVal);
where the "CStringToBinary" function is what I am looking to find.

the int representation should be 1 or in hex 0x01
Any help is greatly appreciated.

Delali


int myIntVal = 0;
const char* ptr = myVal;
while (*ptr)
myIntVal += myIntVal + (*ptr++ - '0');

Of course that only works for binary numbers :-)

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim H | last post by:
I am getting binary data form a database binary(8). I am copying it to a byte array byte. I need to covert this to a number, ulong, so I can write it to a string in hex format. Any idea on how...
2
by: Diablo | last post by:
Hi, I am converting a byte array to string using System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the resulting text is converted back to binary, using the reverse function:...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
27
by: fuch6921 | last post by:
I want to read in an Octal number argument and have it stored as an octal number. For instance the user will type: ./a.out 777 and it will store the octal number 777. But it atoi does this as an...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
4
by: Alan Ning | last post by:
Hi all, Say I have the following, string str("Hello World\n") I would like to extract 5 bits at a time from the string. I am doing this to convert ascii string to another string type. Is...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
4
by: vcnewbie | last post by:
Hi I'm maintaining a VisualC++ project to increase its security regarding stored passwords. I thought about using SHA256Managed to create a hash for the password when creating a user and when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.