c - too many values for a hex number - malloc

Could somebody please clarify what's happening here that's giving me a hex value of 0xffffffa5 versus 0xa5?
#define HEXVAL 0xA5
int main(int argc, char *argv[])
{
int numBytes = 3;
char *tmp;
char *pyld;
pyld = malloc(numBytes * sizeof(char));
memset(pyld, 0, sizeof(pyld));
tmp = pyld;
*(tmp) = HEXVAL;
printf("out = %x\n", pyld[0]); // -> ffffffa5
}
I was expecting "out = a5" instead of all "out = ffffffa5". I would like to add signed values to pyld so I'd rather not set pyld to unsigned char.
Thanks!

To begin, may I suggest using calloc? It formats the heap space for you.
To fix your problem, you have to make tmp unsigned. You can keep pyld signed. Just remember to deference tmp to print out the right value.
Here's my work:
#include <stdio.h>
#include <stdlib.h>
#define HEXVAL 0xA5
int main(int argc, char *argv[])
{
int numBytes = 3;
unsigned char *tmp;
char *pyld;
pyld = calloc(numBytes, sizeof(char));
tmp = pyld;
*tmp = HEXVAL;
printf("pyld = %X\n", *pyld);
printf("tmp = %X\n", *tmp);
return 0;
}
Here's the output
pyld = FFFFFFA5
tmp = A5
Hope that helps!

Related

problems using demo arguments in c code do deal with the strings in argv

I have a c code to show a lowercase of one argument:
This code runs ok when called from commandline with more than 6 argument
however,if called without argument, the demo part does not work ,and programme got stuck:
Below is the code file:
Can anyone help me, Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
int showLowerCase(char *argv[]){
char* aStr = argv[6];
//To lowercase: the demo got stuck probably here
for (int i = 0; aStr[i]; i++) {
aStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",aStr);
}
int main(int argc, char *argv[])
{
if(argc < 7){
// when no srguments given, try a demo:
// change argc to 7
argc = 7;
// make a example of argv2 with 7 strings
char *argv2[7];
argv2[0] = "killWindowsVersatile.exe";
argv2[1] = "key";
argv2[2] = "ci";
argv2[3] = "once";
argv2[4] = "2";
argv2[5] = "1000";
argv2[6] = "SuperCol"; //this argument will be shown as lowercase
showLowerCase(argv2);
return 0;
}
showLowerCase(argv);
return 0;
}
It seems that argv can be modified to lowercase in site, while the argv2 I constructed cannot be modified.
Solved by strcpy:
int showLowerCase(char *argv[]){
printf("aStr = argv[6]\n");
char* aStr = argv[6];
printf("aStr = argv[6]...done");
//make hard copy
char newStr[strlen(aStr)+1];
strcpy(newStr,aStr);
//To lowercase
for (int i = 0; newStr[i]; i++) {
printf("i=0\n");
printf("%c\n",newStr[i]);
newStr[i] = tolower(aStr[i]);
}
printf("a lower case for 6th input string: %s\n",newStr);
}

How to return a int converted to char array back to main for displaying it

My doubts are as follows :
1 : how to send 'str' from function 'fun' , So that i can display it in main function.
2 : And is the return type correct in the code ?
2 : the current code is displaying some different output.
char * fun(int *arr)
{
char *str[5];
int i;
for(i=0;i<5;i++)
{
char c[sizeof(int)] ;
sprintf(c,"%d",arr[i]);
str[i] = malloc(sizeof(c));
strcpy(str[i],c);
}
return str;
}
int main()
{
int arr[] = {2,1,3,4,5},i;
char *str = fun(arr);
for(i=0;i<5;i++)
{
printf("%c",str[i]);
}
return 0;
}
how to send 'str' from function 'fun' , So that i can display it in main function.
This is the way:
char* str = malloc( size );
if( str == NULL ) {
fprintf( stderr,"Failed to malloc\n");
}
/* Do stuff with str, use str[index],
* remember to free it in main*/
free(str);
And is the return type correct in the code ?
No, Probably char** is the one you need to return.
the current code is displaying some different output.
Consider explaining what/why do you want to do ? The way you have written, seems completely messed up way to me. You're passing array of integer but not its length. How is the fun() supposed to know length of array? Another problem is array of pointers in fun().
You can't write a int to a char (See the both size). So I used char array instead.
However, I'm not sure if this is what you want to do (might be a quick and dirty way of doing it):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char**
fun(int *arr, int size)
{
char **str = malloc( sizeof(char*)*size );
if( str == NULL ) {
fprintf( stderr, "Failed malloc\n");
}
int i;
for(i=0;i<5;i++) {
str[i] = malloc(sizeof(int));
if( str == NULL ) {
fprintf( stderr, "Failed malloc\n");
}
sprintf(str[i],"%d",arr[i]);
}
return str;
}
int
main()
{
int arr[] = {2,1,3,4,5},i;
char **str = fun(arr, 5);
for(i=0;i<5;i++) {
printf("%s\n",str[i]);
free(str[i]);
}
free(str);
return 0;
}
I made these changes to your code to get it working:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **fun(int *arr)
{
char **str = malloc(sizeof(char *) * 5);
int i;
for(i = 0; i < 5; i++) {
if ((arr[i] >= 0) && (arr[i] <= 9)) {
char c[2] ;
sprintf(c, "%d", arr[i]);
str[i] = (char *) malloc(strlen(c) + 1);
strcpy(str[i],c);
}
}
return str;
}
int main()
{
int arr[] = {2, 1, 3, 4, 5}, i;
char **str = fun(arr);
for(i = 0; i < 5; i++) {
printf("%s", str[i]);
free(str[i]);
}
printf("\n");
free(str);
return 0;
}
Output
21345
I added a check to make sure that arr[i] is a single digit number. Also, returning a pointer to a stack variable will result in undefined behavior, so I changed the code to allocate an array of strings. I don't check the return value of the malloc calls, which means this program could crash due to a NULL pointer reference.
This solution differs from the others in that it attempts to answer your question based on the intended use.
how to send 'str' from function 'fun' , So that i can display it in main function.
First, you need to define a function that returns a pointer to array.
char (*fun(int arr[]))[]
Allocating variable length strings doesn't buy you anything. The longest string you'll need for 64bit unsigned int is 20 digits. All you need is to allocate an array of 5 elements of 2 characters long each. You may adjust the length to suit your need. This sample assumes 1 digit and 1 null character. Note the allocation is done only once. You may choose to use the length of 21 (20 digits and 1 null).
For readability on which values here are related to the number of digits including the terminator, I'll define a macro that you can modify to suit your needs.
#define NUM_OF_DIGITS 3
You can then use this macro in the whole code.
char (*str)[NUM_OF_DIGITS] = malloc(5 * NUM_OF_DIGITS);
Finally the receiving variable in main() can be declared and assigned the returned array.
char (*str)[NUM_OF_DIGITS] = fun(arr);
Your complete code should look like this:
Code
char (*fun(int arr[]))[]
{
char (*str)[NUM_OF_DIGITS] = malloc(5 * NUM_OF_DIGITS);
int i;
for(i=0;i<5;i++)
{
snprintf(str[i],NUM_OF_DIGITS,"%d",arr[i]); //control and limit to single digit + null
}
return str;
}
int main()
{
int arr[] = {24,1,33,4,5},i;
char (*str)[NUM_OF_DIGITS] = fun(arr);
for(i=0;i<5;i++)
{
printf("%s",str[i]);
}
free(str);
return 0;
}
Output
2413345
With this method you only need to free the allocated memory once.

OpenMP and MPI hybrid dynamic scheduling

As the number of threads increase, the count which is "temp" decreases..
When I sent the number of threads as "1" it gives an correct answer but as the number of threads increases, running time shorter but gives wrong answer
#include <stdio.h>
#include <mpi.h>
#include <complex.h>
#include <time.h>
#include <omp.h>
#define MAXITERS 1000
// globals
int count = 0;
int nptsside;
float side2;
float side4;
int temp = 0;
int inset(double complex c) {
int iters;
float rl,im;
double complex z = c;
for (iters = 0; iters < MAXITERS; iters++) {
z = z*z + c;
rl = creal(z);
im = cimag(z);
if (rl*rl + im*im > 4) return 0;
}
return 1;
}
int main(int argc, char **argv)
{
nptsside = atoi(argv[1]);
side2 = nptsside / 2.0;
side4 = nptsside / 4.0;
//struct timespec bgn,nd;
//clock_gettime(CLOCK_REALTIME, &bgn);
int x,y; float xv,yv;
double complex z;
int i;
int mystart, myend;
int nrows;
int nprocs, mype;
int data;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &mype);
nrows = nptsside/nprocs;
printf("%d\n", nprocs);
mystart = mype*nrows;
myend = mystart + nrows - 1;
#pragma omp parallel shared(mystart, myend, temp)
{
int nth = omp_get_num_threads();
printf("%d\n", nth);
#ifdef STATIC
#pragma omp for reduction(+:temp) schedule(static)
#elif defined DYNAMIC
#pragma omp for reduction(+:temp) schedule(dynamic)
#elif defined GUIDED
#pragma omp for reduction(+:temp) schedule(guided)
#endif
for (x=mystart; x<=myend; x++) {
for ( y=0; y<nptsside; y++) {
xv = (x - side2) / side4;
yv = (y - side2) / side4;
z = xv + yv*I;
if (inset(z)) {
temp++;
}
}
}
}
if(mype==0) {
count += temp;
printf("%d\n", temp);
for (i = 1; i < nprocs; i++) {
MPI_Recv(&temp, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &status);
count += temp;
printf("%d\n", temp);
}
}
else{
MPI_Send(&temp, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
MPI_Finalize();
if(mype==0) {
printf("%d\n", count);
}
//clock_gettime(CLOCK_REALTIME, &nd);
//printf("%f\n",timediff(bgn,nd));
}
You are not defining any private variables for when you enter the OpenMP loop.
First off, you must always declare your loop counter for your OpenMP loop (as well as any loop counters for nested loops inside your OpenMP loop) private.
Secondly, you have three variables (xv, yv, and z) that each depend on your iterations in these loops. Thus, each thread needs to have its own private copy of these variables as well. Changing your parallel statement to
#pragma omp parallel shared(mystart, myend, temp) private(x, y, xv, yv, z)
should fix your OpenMP problems.
Seeing as you say that setting your number of threads to 1 yields the correct answer, I have not looked at your MPI code.
EDIT: OK I lied, I briefly looked into your MPI code now. Instead of all of your sends and receives, you should be writing a single reduce. This collective will be much faster than the blocking communication you set up currently.
MPI_Reduce(&temp, &count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

Building 32bit Float from 4 Bytes [Binary File I/O] in C++

I'm sure this must be a common problem but can't seem to find an equivalent question* or example.
I have a binary file that is a series of 4 byte floats. I am reading into a vector that is sized by the length of the file (divided by the size of my float). I have used the bytesToFloat method from another post. When printing out the data my code returns the same value for all data points. What's wrong?
*Sorry to the admins if I have missed it.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
typedef unsigned char uchar;
float bytesToFloat(uchar b0, uchar b1, uchar b2, uchar b3);
int main()
{
int i,j;
char u[4];
// Open file
ifstream file;
file.open("file.dat");
// Find file size in bytes
file.seekg(0,ios::end);
double size = 0;
size = file.tellg();
file.seekg(0,ios::beg);
vector<float> data;
data.resize(size/4);
i=0;
while(i<size/4)
{
j=0;
while(j<4)
{
file.read(&u[j],1);
j++;
}
data[i] = bytesToFloat(u[0],u[1],u[2],u[3]);
cout << data[i]<< endl;
i++;
}
// End program
file.close();
return 0;
}
float bytesToFloat(uchar b0, uchar b1, uchar b2, uchar b3)
{
float output;
*((uchar*)(&output) + 3) = b0;
*((uchar*)(&output) + 2) = b1;
*((uchar*)(&output) + 1) = b2;
*((uchar*)(&output) + 0) = b3;
return output;
}
So with a bit of effort and Igor's comment I was able to solve the problem. The following function reads everything into a buffer vector.
vector<char> buffer;
void fill() {
string filename = "";
cout << "Please enter a filename:\n>";
getline(cin, filename);
ifstream file(filename.c_str());
if (file) {
file.seekg(0,std::ios::end);
streampos length = file.tellg();
cout<< length << endl;
file.seekg(0,std::ios::beg);
file.seekg(540,'\0');
length-=540;
buffer.resize(length);
file.read(&buffer[0],length);
}
}
Then later on I call bytesToFloat in a loop. The endian-ness of bytesToFloat was incorrect so has now been reversed and it outputs the same values as my original file (I made my random file generator output a plain text version for comparison).

How to use a set of numbers as the Key for RCA Encryption

i would like to know how can i use a set of numbers as a KEY for the rc4 encryption.
According to the internet and wiki the KEY is actually a string of letters but the bytes are used . But in my program i need to use a 6 digit number as a KEY. Should i covert it to a string or how.
Key Sheudling Algorithm is indicated below.
void ksa(u_char *State, u_char *key) {
int byte, i, keylen, j=0;
keylen = (int) strlen((char *) key);
for(i=0; i<256; i++) {
j = (j + State[i] + key[i%keylen]) % 256;
swap(&State[i], &State[j]);
}
How can i modify the code or should i just convert the numbers to string.
Strings and numbers are both bytes. Here is a working RC4 code that accepts a key of unsigned chars:
#include<stdio.h>
#include<string.h>
#define SIZE 256
unsigned char SBox[SIZE];
int i;
int j;
void initRC4(unsigned char Key[]);
unsigned char getByte(void);
void initRC4(unsigned char Key[])
{
unsigned char tmp;
unsigned char KBox[SIZE];
for(i=0;i<SIZE;i++)
SBox[i]=i;
for(i=0;i<SIZE;i++)
KBox[i]=Key[i % strnlen(Key,SIZE)];
for(j=0,i=0;i<SIZE;i++)
{
j=(j+SBox[i]+KBox[i]) % SIZE;
tmp=SBox[i];
SBox[i]=SBox[j];
SBox[j]=tmp;
}
}
unsigned char getByte(void)
{
unsigned char tmp;
i=(i+1)%SIZE;
j=(j+SBox[i])%SIZE;
tmp=SBox[i];
SBox[i]=SBox[j];
SBox[j]=tmp;
return SBox[(SBox[i]+SBox[j])%SIZE];
}
First, you initialize the RC4 stream:
initRC4(key);
Then you do:
getByte()
...which always returns 1 byte from the RC4 stream you've set up.
One thing to remember though - a letter in string is not always equal to 1 byte. Same goes for the integers and number symbols in strings. Really, you must read an introduction to computer programming before you mess with ciphers.
Here is a demonstration of how bytes differ in strings in integers:
#include <string>
int main(int argc, char **argv) {
const int n=67898;
const std::string str = "67898";
const int arrayLength = sizeof(int);
const int stringArrayLength = str.size();
unsigned char *bytePtr=(unsigned char*)&n;
printf("Bytes for integer: ");
for(int i=0;i<arrayLength;i++)
{
printf("%X ", bytePtr[i]);
}
printf("\n");
printf("Bytes for string: ");
for(int i=0;i<stringArrayLength;i++)
{
printf("%X ", str.at(i));
}
printf("\n");
return 0;
}
Output:
Bytes for integer: 3A 9 1 0
Bytes for string: 36 37 38 39 38
There will usually be a terminating byte at the end of a string, so you could add +1 byte to string size.

Resources