I using string to contain my 64 bits binary.
string aBinary;
aBinary = "100011111011101100000101101110000100111000011100100100110101100";
Initially i tried this..
stringstream ss;
ss << bitset<64>(aBinary).to_ulong();
buffer = ss.str();
cout << buffer << endl;
Its work for some binary, but this one it doesn't work. How can i convert the above 64 bits binary that is contain in a string container into a decimal which is of a string container too.
It's overflowing, because to_ulong() is 32-bits.
C++-11 introduces the function to_ullong(), which is what you want. If you don't have that, you can try splitting your string into two, get two 32-bit numbers, convert to 64-bit, do a shift and an add.
maybe you can done it with C:
char *Str = "0100011111011101100000101101110000100111000011100100100110101100";
char *ptr = NULL;
unsigned long long toValue;
toValue = 0;
ptr = Str + strlen(Str) - 1;
for(int i = 0 ; i < strlen(Str) , ptr != Str ; i++,ptr--)
{
if(*ptr != '0')
toValue += ((unsigned long long)1 << i);
}
printf("%d\n",toValue);
Related
I want to convert the hexadecimal string value 0x1B6 to unsigned char - where it will store the value in the format 0x1B, 0x60 We had achieved the scenarios in C++, but C doesn't support std::stringstream.
The following code is C++, how do I achieve similar behavior in C?
char byte[2];
std::string hexa;
std::string str = "0x1B6" // directly assigned the char* value in to string here
int index =0;
unsigned int i;
for(i = 2; i < str.length(); i++) {
hexa = "0x"
if(str[i + 1] !NULL) {
hexa = hexa + str[i] + str[i + 1];
short temp;
std::istringstream(hexa) >> std::hex >> temp;
byte[index] = static_cast<BYTE>(temp);
} else {
hexa = hexa+ str[i] + "0";
short temp;
std::istringstream(hexa) >> std::hex >> temp;
byte[index] = static_cast<BYTE>(temp);
}
}
output:
byte[0] --> 0x1B
byte[1]--> 0x60
I don't think your solution is very efficient. But disregarding that, with C you would use strtol. This is an example of how to achieve something similar:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void) {
const char *hex_string = "0x1B60";
long hex_as_long = strtol(hex_string, NULL, 16);
printf("%lx\n", hex_as_long);
// From right to left
for(int i = 0; i < strlen(&hex_string[2]); i += 2) {
printf("%x\n", (hex_as_long >> (i * 4)) & 0xff);
}
printf("---\n");
// From left to right
for(int i = strlen(&hex_string[2]) - 2; i >= 0; i -= 2) {
printf("%x\n", (hex_as_long >> (i * 4)) & 0xff);
}
}
So here we get the full value as a long inside hex_as_long. We then print the whole long with the first print and the individual bytes inside the second for loop. We are shifting multiples of 4 bits because one hex digit (0xf) covers exactly 4 bits of data.
To get the bytes or the long printed to a string rather than to stdout (if that is what you want to achieve), you can use strprintf or strnprintf in a similar way to how printf is used, but with a variable or array as destination.
This solution scans whole bytes (0xff) at a time. If you need to handle one hex digit (0xf) at a time you can divide all the operations by two and mask with 0xf instead of 0xff.
I have a string value:
string str = "2018";
Now I have to store in unsigned char array as hex representation but not really convert to hex:
unsigned char data [2]; //[0x20,0x18]
If I do it this way
data[0] = 0x20;
data[1] = 0x18;
It works, but my input is string, how I can resolve it?
Edit
If my input is unsigned char instead of string like
unsigned char y1 = 20;
unsigned char y2 = 18;
Is there any better way?.
A brief research made me find this function QString::toInt(bool&, int) which can be useful for your intent.
Basically you could:
if(str.size() % 2 == 1){
str = '0' + str;
}
for(int i = 0; i < str.size() / 2; i++){
data[i] = (str[2*i] + str[2*i+1]).toInt(res, 16);
}
I did not try this code, there surely a better way to extract the substring, and probably a more efficient way than to iterate over it.
Perhaps you could try something like this:
#include <iostream>
int main()
{
std::string s = "2018";
unsigned i;
std::sscanf(s.c_str(), "%04x", &i);
unsigned char data[2];
data[0] = i >> 8;
data[1] = i;
std::cout << std::hex << (int)data[0] << " " << (int)data[1] << std::endl;
return 0;
}
https://ideone.com/SyYKUl
Prints:
20 18
If you can assume the string to have 4 digits, you can convert it to BCD format simply and efficiently this way:
void convert_to_bcd4(unsigned char *data, const char *str) {
data[0] = (str[0] - '0') * 16 + (str[1] - '0');
data[1] = (str[2] - '0') * 16 + (str[3] - '0');
}
You can complete the conversion of "2018" to 0x20 0x18 using a hex string to binary converter. I think, for example, sscanf("%x",....) will do this. This typically gives an int. You can extract the byte values from the int in the normal way. (This method does not check for errors.)
I'm trying to initialize bitboards from an array of a chess board. Running a for loop through and checking if a piece matches, then appending the particular board with a 64 digit string converted to binary matching the pieces position.
for (int i=0;i<64;i++) {
Binary="0000000000000000000000000000000000000000000000000000000000000000";
Binary[i] = '1';
if(chessBoard[i/8][i%8] == "P"){
WP+=convertStringToBitboard(Binary);
}
For my convertStringToBitboard function i've tried:
uint64_t convertStringToBitboard(std::string Binary){
char * ptr;
long long temp = std::stoull(Binary, &ptr, 2);
std::cout << temp << std::endl;
return temp;
}
as well as
uint64_t convertStringToBitboard(std::string Binary){
std::bitset<64> x(std::string(Binary));
return x;
}
Any help would be more than appreciated!
first of all instead of using strings you can use a simple shift
long long unsigned bin = 1LL << i;
Other than that there is no standard functions to convert binaries. You can invent one by looping through the string elements and shifting. something like the following:
long long unsigned bin = 0;
for (int i = 0; i < 64; i++) {
long long unsigned bit = binaryString[i] - '0';
bin |= (bit << i);
}
Use strtoull, like this:
std::string s = "747";
int64_t n = std::strtoull(s.c_str(), NULL, 0);
I am trying to encode a string to base36.
static char *decode(unsigned long long value)
{
char base36[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[14];
unsigned int offset = sizeof(buffer);
buffer[--offset] = '\0';
do {
buffer[--offset] = base36[value % 36];
} while (value /= 36);
return _strdup(&buffer[offset]);
}
int main()
{
char original[8] = "0XDX3A1";
unsigned long long encoded = _strtoui64(original, NULL, 36);
char *decoded = decode(encoded);
cout << "Original: " << original << " Decoded: " << decoded << endl;
return 0;
}
The problem here is, while those functions work OK: if the string I am trying to encode has a leading 0 the decoded string is one character (or more) less than the original.
How to deal with this?
If you decode the string "01234" as a base-16 string (for example), you get the integer value 4660 (0x1234) -- exactly the same integer value you get by decoding the string "1234" or "00001234" as a base-16 string. By converting the string to an integer, you've thrown away any information about leading zeros. You've also discarded any information about uppercase vs. lowercase letters, assuming that A and a represent the same value.
Converting that integer value back to a string isn't going to restore that leading 0 unless you add it explicitly. And if you want to add that leading 0 (or multiple 0s) if and only if they were present in the original string, you're going to have to store that information somehow.
Introduce a new variable in your main, called zeroCount in main
Introduce a 2nd argument to function decode, called zeroCount
Count the amount of leading zeroes in the original to the zeroCount in main
Place zeroes to buffer[--offset] until you consume all the zeroCount before the return
Like this:
static char *decode( unsigned long long value, int zeroCount )
{ // introduced zeroCount argument there ^
char base36[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[14];
unsigned int offset = sizeof( buffer );
buffer[--offset] = '\0';
do {
buffer[--offset] = base36[value % 36];
} while ( value /= 36 );
while ( zeroCount-- ) buffer[--offset] = '0'; // <-- added this
return strdup( &buffer[offset] );
}
int main( )
{
char original[8] = "0XDX3A1";
unsigned long long encoded = _strtoui64( original, NULL, 36 );
int zeroCount = 0; // added
for ( int i = 0; i < sizeof original && original[i] == '0'; i++ ) // these
zeroCount++; // three
char *decoded = decode( encoded, zeroCount ); // <-- called along with zeroCount
cout << "Original: " << original << " Decoded: " << decoded << endl;
return 0;
}
Since there isn't any apparent rule for the 0 appending behaviour you desire, I had to assume that you'd like to have exact many leading zeroes that the original had.
You're calling a function tat takes a string containing a representation of a numeric value and converts it to an unsigned long long . The two string representations '00007' and '7' are both converted to numeric 7, and the leading zeroes are lost.
IF you want, eg, 00000036 to covert to 00000010 in base 36, you'll just have to count the zeroes you want and then decide how many of them to replace ( would it depend on the relative lengths of base 10 and base 36 strings? )
But it seems poor practice in the conversion functions. better, in my mind, to add leading zeroes when outputting the value. As many have commented, they have no significance and should not be part of the conversion logic.
I'd suggest you to create a wrapper around your method, and pass it a length parameter.
Eg.
char * wrap_base36enc(int out_len, unsigned long long value){
char pre_str[MAX_VAL]="", *ans = base36enc(value);
len -= strlen(ans);
while(len--){
strcat(pre_str,"0");
}
strcat(pre_str,ans);
return pre_str;
}
I want to convert the integer (whose maximum value can reach to 99999999) in to BCD and store in to array of 4 characters.
Like for example:
Input is : 12345 (Integer)
Output should be = "00012345" in BCD which is stored in to array of 4 characters.
Here 0x00 0x01 0x23 0x45 stored in BCD format.
I tried in the below manner but didnt work
int decNum = 12345;
long aux;
aux = (long)decNum;
cout<<" aux = "<<aux<<endl;
char* str = (char*)& aux;
char output[4];
int len = 0;
int i = 3;
while (len < 8)
{
cout <<"str: " << len << " " << (int)str[len] << endl;
unsigned char temp = str[len]%10;
len++;
cout <<"str: " << len << " " << (int)str[len] << endl;
output[i] = ((str[len]) << 4) | temp;
i--;
len++;
}
Any help will be appreciated
str points actually to a long (probably 4 bytes), but the iteration accesses 8 bytes.
The operation str[len]%10 looks as if you are expecting digits, but there is only binary data. In addition I suspect that i gets negative.
First, don't use C-style casts (like (long)a or (char*)). They are a bad smell. Instead, learn and use C++ style casts (like static_cast<long>(a)), because they point out where you are doing things that are dangeruos, instead of just silently working and causing undefined behavior.
char* str = (char*)& aux; gives you a pointer to the bytes of aux -- it is actually char* str = reinterpret_cast<char*>(&aux);. It does not give you a traditional string with digits in it. sizeof(char) is 1, sizeof(long) is almost certainly 4, so there are only 4 valid bytes in your aux variable. You proceed to try to read 8 of them.
I doubt this is doing what you want it to do. If you want to print out a number into a string, you will have to run actual code, not just reinterpret bits in memory.
std::string s; std::stringstream ss; ss << aux; ss >> s; will create a std::string with the base-10 digits of aux in it.
Then you can look at the characters in s to build your BCD.
This is far from the fastest method, but it at least is close to your original approach.
First of all sorry about the C code, I was deceived since this started as a C questions, porting to C++ should not really be such a big deal.
If you really want it to be in a char array I'll do something like following code, I find useful to still leave the result in a little endian format so I can just cast it to an int for printing out, however that is not strictly necessary:
#include <stdio.h>
typedef struct
{
char value[4];
} BCD_Number;
BCD_Number bin2bcd(int bin_number);
int main(int args, char **argv)
{
BCD_Number bcd_result;
bcd_result = bin2bcd(12345678);
/* Assuming an int is 4 bytes */
printf("result=0x%08x\n", *((int *)bcd_result.value));
}
BCD_Number bin2bcd(int bin_number)
{
BCD_Number bcd_number;
for(int i = 0; i < sizeof(bcd_number.value); i++)
{
bcd_number.value[i] = bin_number % 10;
bin_number /= 10;
bcd_number.value[i] |= bin_number % 10 << 4;
bin_number /= 10;
}
return bcd_number;
}