Packed value in C++ or C - c++

struct FILE_UPC_RECORD
{
char UPC[FILE_UPC_KEY_SIZE];// packed up to 16 digits right justified and zero filled
// possibilities are:
// 1. 12-digit UPC w/2 leading 0's
// 2. 13-digit EAN w/1 leading 0
// 3. 14-digit EAN
char SKU[FILE_ITEM_KEY_SIZE]; // packed, 0000ssssssssssss
};
Where FILE_UPC_KEY_SIZE & FILE_ITEM_KEY_SIZE = 8. Is packed value equivalent to Hex value? How do i store '0123456789012' equivaent decimal calue in the UPC & SKU array? Thanks for all your help.

You asked "How do i ...", here's some example code with comments
#include <stdio.h>
#include <stdint.h>
int main(int argc, const char * argv[])
{
int i, newSize;
// Treat the result packed data as unsigned char (i.e., not a string so not
// NULL terminated)
uint8_t upc[8];
uint8_t *destPtr;
// Assume input is a char string (which will be NULL terminated)
char newUPC[] = "0123456789012";
char *srcPtr;
// -1 to remove the string null terminator
// /2 to pack 2 decimal values into each byte
newSize = (sizeof(newUPC) - 1) / 2;
// Work from the back to the front
// -1 because we count from 0
// -1 to skip the string null terminator from the input string
destPtr = upc + (sizeof(upc) - 1);
srcPtr = newUPC + (sizeof(newUPC) - 1 - 1);
// Now pack two decimal values into each byte.
// Pointers are decremented on individual lines for clarity but could
// be combined with the lines above.
for (i = 0; i < newSize; i++)
{
*destPtr = *srcPtr - '0';
srcPtr--;
*destPtr += (*srcPtr - '0') << 4;
srcPtr--;
destPtr--;
}
// If input was an odd lenght handle last value
if ( (newSize * 2) < (sizeof(newUPC) - 1) )
{
*destPtr = *srcPtr - '0';
destPtr--;
i++;
}
// Fill in leading zeros
while (i < sizeof(upc))
{
*destPtr = 0x00;
destPtr--;
i++;
}
// Print the hex output for validation.
for (i = 0; i < sizeof(upc); i++)
{
printf("0x%02x ", upc[i]);
}
printf("\n");
return 0;
}

Related

Printing Binary Number Backward

I need to print a binary number backward without explicitly converting to binary or using an array (i.e. if the binary number is 10, it should print as 01). Here is the code I've done for printing the number forward. I'm fairly certain that I just need to tell the code to run through the loop starting at the other end in order to have the number render backward. However, I have no idea how to go about doing that, or if that's even correct.
Bonus question -- can someone walk me through what this code is really doing? It's modified from one we were given in class, and I don't fully understand what it actually does.
NOTE: the test case I have been using is 50.
#include <stdio.h>
char str [sizeof(int)];
const int maxbit = 5;
char* IntToBinary (int n, char * BackwardBinaryString) {
int i;
for(i = 0; i <= maxbit; i++) {
if(n & 1 << i) {
BackwardBinaryString[maxbit - i] = '1';
}
else {
BackwardBinaryString[maxbit - i] = '0';
}
}
BackwardBinaryString[maxbit + 1] = '\0';
return BackwardBinaryString;
}
int main () {
int base10input;
scanf("%d", &base10input);
printf("The backwards binary representation is: %s\n", IntToBinary(base10input, str));
return 0;
}
To your disappointment, your code is wrong in these aspects.
sizeof(int) returns the bytes an int takes, but we need the bit it takes as we store each bit in a char, so we need to multiply it by 8.
Your char array str have a size of 4, which means only str[0] to str[3] are vaild. However, you modified str[4], str[5] and str[6] which are out of bounds and such undefined behavior will result in a disaster.
What you should do first is to create an array holds at least sizeof(int) * 8 + 1 chars. (sizeof(int) * 8 for the binary representation, one for the null-terminator) Then start your convention.
And I also suggest that the str should not be a global variable. It will be better to be a local variable of main function.
Your code should be modified like this. I've explained what it does in the comments.
#include <stdio.h>
#define INTBITS (sizeof(int) * 8) // bits an integer takes
char* IntToBinary(int n, char* backwardBinaryString) {
// convert in reverse order (str[INTBITS - 1] to str[0])
// remember that array subscript starts from 0
for (int i = 0; i < INTBITS; i++) {
// (n & (1 << i)) checks the i th bit of n is 0 or 1
// if it is 1, the value of this expression will be true
if (n & (1 << i)) {
backwardBinaryString[INTBITS - 1 - i] = '1';
}
else {
backwardBinaryString[INTBITS - 1 - i] = '0';
}
// here replacing the if-else with and conditional operator like this
// will make the code shorter and easier to read
// backwardBinaryString[INTBITS - 1 - i] = (n & (1 << i)) ? '1' : '0';
}
// add the null-terminator at the end of str (str[INTBITS + 1 - 1])
backwardBinaryString[INTBITS] = '\0';
return backwardBinaryString;
}
int main() {
char str[INTBITS + 1];
int base10input;
scanf("%d", &base10input);
printf("The backwards binary representation is: %s\n", IntToBinary(base10input, str));
return 0;
}
That code is far more elaborate than it needs to be. Since the requirement is to print the bits, there's no need to store them. Just print each one when it's generated. And that, in turn, means that you don't need to use i to keep track of which bit you're generating:
if (n == 0)
std::cout << '0';
else
while (n != 0) {
std::cout << (n & 1) ? '1' : '0';
n >>= 1;
}
std::cout << '\n';

how can I convert character to integer number

How could I change an array of character (string) to an integer number without using any ready function (ex atoi();) for example :-
char a[5]='4534';
I want the number 4534 , How can I get it ?
Without using any existing libraries, you have to:
Convert character to numeric digit.
Combine digits to form a number.
Converting character to digit:
digit = character - '0';
Forming a number:
number = 0;
Loop:
number = number * 10 + digit;
Your function will have to check for '+' and '-' and other non-digits characters.
First of all this statement
char a[5]='4534';
will not compile. I think you mean
char a[5]="4534";
^^ ^^
To convert this string to a number is enough simple.
For example
int number = 0;
for ( const char *p = a; *p >= '0' && *p <= '9'; ++p )
{
number = 10 * number + *p - '0';
}
Or you could skip leading white spaces.
For example
int number = 0;
const char *p = s;
while ( std::isspace( ( unsigned char )*p ) ) ++p;
for ( ; *p >= '0' && *p <= '9'; ++p )
{
number = 10 * number + *p - '0';
}
If the string may contain sign '+' or '-' then you can check at first whether the first character is a sign.
You can go like this
It's working
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char a[5] = "4534";
int converted = 0;
int arraysize = strlen(a);
int i = 0;
for (i = 0; i < arraysize ; i++)
{
converted = converted *10 + a[i] - '0';
}
printf("converted= %d", converted);
return 0;
}
I prefer to use the std::atoi() function to convert string into a number data type. In your example you have an non-zero terminated array "\0", so the function will not work with this code.
Consider to use zero terminated "strings", like:
char *a = "4534";
int mynumber = std::atoi( a ); // mynumber = 4534

Converting array of char into array of integers? [duplicate]

How could I change an array of character (string) to an integer number without using any ready function (ex atoi();) for example :-
char a[5]='4534';
I want the number 4534 , How can I get it ?
Without using any existing libraries, you have to:
Convert character to numeric digit.
Combine digits to form a number.
Converting character to digit:
digit = character - '0';
Forming a number:
number = 0;
Loop:
number = number * 10 + digit;
Your function will have to check for '+' and '-' and other non-digits characters.
First of all this statement
char a[5]='4534';
will not compile. I think you mean
char a[5]="4534";
^^ ^^
To convert this string to a number is enough simple.
For example
int number = 0;
for ( const char *p = a; *p >= '0' && *p <= '9'; ++p )
{
number = 10 * number + *p - '0';
}
Or you could skip leading white spaces.
For example
int number = 0;
const char *p = s;
while ( std::isspace( ( unsigned char )*p ) ) ++p;
for ( ; *p >= '0' && *p <= '9'; ++p )
{
number = 10 * number + *p - '0';
}
If the string may contain sign '+' or '-' then you can check at first whether the first character is a sign.
You can go like this
It's working
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char a[5] = "4534";
int converted = 0;
int arraysize = strlen(a);
int i = 0;
for (i = 0; i < arraysize ; i++)
{
converted = converted *10 + a[i] - '0';
}
printf("converted= %d", converted);
return 0;
}
I prefer to use the std::atoi() function to convert string into a number data type. In your example you have an non-zero terminated array "\0", so the function will not work with this code.
Consider to use zero terminated "strings", like:
char *a = "4534";
int mynumber = std::atoi( a ); // mynumber = 4534

Padding specified number of zeros to a char array

I want to pad a given char array to make it a 15 character array.
For eg. if the array contains two characters 1, 2 then 13 0 characters should be padded to make in 000000000000012 and if contains five characters then 10 0s should be padded. The resultant array should contain 15 characters always.
Found one solution here but that’s for stl string I need similar solution for char arrays. Please help.
What I have tried is below:
char moneyArray[256];
memset(moneyArray, 0, 256);
for(int i=0;i<(15-strlen(moneyArray))-1;i++)
sprintf(moneyArray,"0%s",moneyArray);
But I am looking for a standard solution if possible using a std function may be?
You can use the pad function below:
#include <iostream>
#include <cstring>
void pad(char *s, int n, int c) {
char *p = s + n - strlen(s);
strcpy(p, s);
p--;
while (p >= s) { p[0] = c; p--; }
}
int main () {
char b[16] = "123";
pad(b, 15, '0');
std::cout << b << std::endl;
return 0;
}
If you're fine with std::string (and I think you should be), you can make use of its fill constructor:
char s[] = "12";
std::string padded = std::string( (15 - strlen(s) ), '0').append(s);
Of course you might want to check whether strlen(s) > 15 first.
You have various options; one of them would be (again under the assumption we already know that moneyArray contains a string and is a 16-byte buffer at least):
size_t len = strlen(moneyArray);
memmove(moneyArray + 15 - len, moneyArray, len + 1);
memset(moneyArray, '0', 15 - len);
you could just write code to move the chars up
char str[10] = "123456";
padStart(str, 7, '0');
str would become "0123456". be sure the char array is large enough to fit the longer string
void padStart(char *str, int len, char padChar)
{
// find the null terminator
int strLen = 0;
while (str[strLen] != '\0')
{
strLen++;
};
// is there anything to actually do
if (strLen < len)
{
// move the string up to the given length
for (int i = 0; i <= strLen; i++) // notice the '<=' to include the \0 terminator
{
str[len - i] = str[strLen - i];
}
// add padChar to the start
for (int i = 0; i < len - strLen; i++)
{
str[i] = padChar;
}
}
}

Visual C++ : trying to convert binary to ascii value

I have problem with my binary to ASCII conversion what i have done is i have array of text boxes and i am trying to put binary values into that text boxes and passes those values to the function written as below :
System::String^ conversion(const char* input)
{
int length = strlen(input); //get length of string
int binary[8]; //array used to store 1 byte of binary number (1 character)
int asciiNum = 0; //the ascii number after conversion from binary
System::String^ ascii; //the ascii character itself
int z = 0; //counter used
for(int x = 0; x < length / 8; x++) //reading in bytes. total characters
{
for(int a = 0; a < 8; a++) //store info into binary[0] through binary[7]
{
binary[a] = (int) input[z] - 48; //z never resets
z++;
}
int power[8]; //will set powers of 2 in an array
int counter = 7; //power starts at 2^0, ends at 2^7
for(int x = 0; x < 8; x++)
{
power[x] = counter; //power[] = {7, 6, 5, ..... 1, 0}
counter--; //decrement counter each time
}
for(int y = 0; y < 8; y++) //will compute asciiNum
{
double a = binary[y]; //store the element from binary[] as "a"
double b = power[y]; //store the lement from power[] as "b"
asciiNum += a* pow(2, b);
}
ascii = System::Convert::ToString(asciiNum);
asciiNum = 0; //reset asciiNum for next loop
return ascii;
}
}
the problem is i only get the ASCII value and couldn't get the related character. I want that character any one help me. Thanx in advance.
char asciichar=(unsigned char) asciinum;
This will convert your number to ascii character and you can return the result as char instead of std::string
It is possible to print an integer as a character. Instead of using ToString, you could recast the integers to characters:
(char)asciiNum
Example:
#include <iostream.h>
using namespace std;
int main ()
{
int beta=100;
cout<<"Value of integer is: "<<beta<<endl;
cout <<"Value of char is: "<<(char) beta<<endl;
}
Results:
Value of integer is : 100
Value of char is : d