need little help form you guys
i just wanna to cheak if the byte is readable or not, i have search for sulution but not find
hope you will help me
i have this code i need if tag that cheak if byte is readable
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
void main()
{
float ramsize;
char *ch;
unsigned int j=128,readbyte;
long i;
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
ramsize = statex.ullTotalPhys;
for(i=0;i<ramsize;i = i+1)
{
ch = (char*) i;
readbyte = *ch;
// if readbyte is readable
printf("you have readable byte in address: %x , that contain in Binary:",&readbyte);
for(i=0;i<8;i++)
{
if(readbyte&j)
printf("1");
else
printf("0");
j=j>>1;
}
putchar('\n');
// if readbyte is not readable
printf("Sorry: you cant read this byte: %x",&readbyte);
}
}
If a byte is not readable the OS will send a signal to your process. You need to catch that signal or your program will terminate.
Read up on signals in your course textbook.
Related
I am trying to create a sequence of integers which is the same everytime as the generator is seeded however I am struggling to get it working. Right now the sequence is never the same.
#include "sodium.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
if (sodium_init() == -1)
{
return 1;
}
unsigned int myInts[128];
char seed[randombytes_SEEDBYTES] = "a seeeeeed";
printf("%s", seed);
randombytes_buf_deterministic(myInts, 128, seed);
for (int i = 0; i < 128; i++)
{
printf("\n(%u)", myInts[i]);
}
}
The problem is, that here
randombytes_buf_deterministic(myInts, 128, seed);
you generate 128 bytes of pseudorandom-data, but your buffer
unsigned int myInts[128];
is sizeof(int)*128 bytes big. So you have to generate enough data with
randombytes_buf_deterministic(myInts, sizeof(myInts), seed);
to fill the whole buffer with deterministic values. Then it should give the output you expect.
Could someone instruct me on how to print a single letter, for example "b" in C while using only the write function (not printf).
I'm pretty sure it uses
#include <unistd.h>
Could you also tell me how the write properties work? I don't really understand
int write( int handle, void *buffer, int nbyte );
Could some of you guys toss in a few C beginner tips as well?
I am using UNIX.
You have found your function, all you need now is to pass it proper parameters:
int handle = open("myfile.bin", O_WRONLY);
//... You need to check the result here
int count = write(handle, "b", 1); // Pass a single character
if (count == 1) {
printf("Success!");
}
I did indeed want to use stdout. How do I write a version to display the whole alphabet?
You could use a pre-defined constant for stdout. It is called STDOUT_FILENO.
If you would like to write out the whole alphabet, you could do it like this:
for (char c = 'A' ; c <= 'Z' ; c++) {
write(STDOUT_FILENO, &c, 1);
}
Demo.
Let's see the man page of write(), which says,
ssize_t write(int fd, const void *buf, size_t count);
Description
write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
As per your requirement, you need to pass an address of a buffer containing b to print to standard output.
Let's see some code along with, shall we?
#include <stdio.h>
#include <unistd.h>
int main(void) {
char b = 'b';
write(STDOUT_FILENO, &b, 1);
return 0;
}
Let me explain. Here, the STDOUT_FILENO is the file descriptior for standard output as defined in unistd.h, &b is the address of the buffer containing 'b' and the number of bytes is 1.
Also valid is:
#include <stdio.h>
#include <unistd.h>
int main(void) {
char b[1] = {'b'};
write(STDOUT_FILENO, b, 1);
return 0;
}
Or:
#include <stdio.h>
#include <unistd.h>
int main(void) {
char b[1] = "b";
write(STDOUT_FILENO, b, 1);
return 0;
}
I've been trying to use the SHA_256 functions in sha256.h on a FreeBSD 9.1 system but it seems to be corrupting memory space of my program causing all kinds of manic behavior. I wrote up a quick program to just to play with those functions and still am having problems.
In the example below, int i is changed when I call SHA256_Init(), as shown by the output from the printf() statements surrounding it.
This is what I get running the code.
$ ./miner "hello world"
i = 0
i = 32
0000000032 9010a9cf81ce2c28a642fd03ddf6da5790c65c30cd4a148c4257d3fe488bacc7
Why is this value changing to 32? Am I missing something? The code is below...
#include <sha256.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#define DIFFICULTY 0
int main(int argc, const char **argv) {
uint nonce, i, j;
SHA256_CTX ctx;
size_t arglen;
unsigned char digest[32];
char * data;
if(argc < 1) exit(1);
arglen = strlen(argv[1]);
data = malloc(arglen + 1);
char digestStr[65];
i = 0;
do {
nonce = i;
strncpy(data, argv[1], arglen + 1);
printf("i = %i\n", i);
SHA256_Init(&ctx);
printf("i = %i\n", i);
SHA256_Update(&ctx, data, arglen);
SHA256_Update(&ctx, (unsigned char *) &nonce, sizeof(nonce));
SHA256_Final(digest, &ctx);
SHA256_End(&ctx, digestStr);
printf("%010i\t%s\n", i, digestStr);
j = 0;
while(j < 32 && digest[j] == '\0') {
j++;
}
i++;
} while(j < DIFFICULTY);
free(data);
return 0;
}
I just had this exact same problem and solved it.
The issue is that your are including a different header in your code than the SHA2 library you linked into your application is using.
In my case the SHA256_CTX struct was a different size in the openSSL library. The openSSL library's struct was 8 bytes bigger than the struct length in the file.
The function SHA256_Init(&ctx) does a memset on the SHA256_CTX struct which then corrupts 8 extra random bytes after the struct. I say random because it will do different things in a release vs debug build because the optimizing compiler will move your variables around.
#include <stdio.h>
#include <ctype.h>
#define int long
int main ()
{
char c;
int i=0;
char str[]="Example sentence to test isspace\n";
while (str[i])
{
c=str[i];
if (isspace(c)) c='\n';
putchar (c);
i++;
}
return 0;
}
It gives error on linux environment, because:
isspace will be expanded as
if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISspace)) c='\n';
As I changed int to long through macro, it will become
if (((*__ctype_b_loc ())[(long) ((c))] & (unsigned short long) _ISspace)) c='\n';
Hence it throws error, Please provide me with the answer.
Simple answer: Don't change int to long with a macro.
If you really must you can can do something like this:
#include <stdio.h>
#include <ctype.h>
#define int long
int main ()
{
char c;
int i=0;
char str[]="Example sentence to test isspace\n";
while (str[i])
{
c=str[i];
#define int int
if (isspace(c)) c='\n';
#define int long
putchar (c);
i++;
}
return 0;
}
#define int long
This is a very bad idea. Don't do this.
It gives error on linux environment
No it doesn't. Compiles and executes just fine with gcc.
As I changed int to long through macro
This is a very bad idea. Don't do this.
The answer to your question is: if you try to re-invent the C language with macros, bad things will happen and you are left on your own to solve them.
While reading a book called "Let us C" I read that a function showbit() exists which can show you the bits of the number. There wasn't any special header file mentioned for it. Searched for it on the internet and didn't found anything useful. Is there such a function? I want this to print the binary of decimal numbers. Else please give me a replacement function. Thanks
All integers are actually in binary in your computer. Its just that it is turned into a string that is the decimal representation of that value when you try to print it using printf and "%d". If you want to know what it looks like in some other base (e.g. base 2 or binary), you either have to provide the proper printf format string if it exists (e.g. "%x" for hex) or just build that string yourself and print it out.
Here is some code that can build the string representation of an integer in any base in [2,36].
#include <stdio.h>
#include <string.h>
char digits[]="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void reverse(char* start, char* end)
{
for(end--;start<end;start++,end--)
{
char t=*start;
*start=*end;
*end=t;
}
}
int base_change(int n, int base,char* buffer)
{
int pos=0;
if (base>strlen(digits))
return -1;
while(n)
{
buffer[pos]=digits[n%base];
n/=base;
pos++;
}
buffer[pos]='\0';
reverse(buffer,buffer+pos);
return 0;
}
int main()
{
char buffer[32];
int conv=base_change(1024,2,buffer);
if (conv==0) printf("%s\n",buffer);
return 0;
}
You can also try this snippet which uses bit-shifting:
EDIT: (I've made it more portable)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BITS_IN_BYTE 8
#define INTEGRAL_TYPE unsigned int
void showBits(INTEGRAL_TYPE x) {
int i;
static int intSizeInBits = sizeof(INTEGRAL_TYPE) * BITS_IN_BYTE;
static char symbol[2] = {'0','1'};
char * binary = (char*) malloc(intSizeInBits + 1);
memset(binary, 0, intSizeInBits + 1);
for (i=0; i< intSizeInBits; i++) {
binary[intSizeInBits-i-1] = symbol[(x>>i) & 0x01];
}
printf("%s\n", binary);
free(binary);
}
int main() {
showBits(8698513);
return 0;
}
HTH!
This is a very simple solution for printing the bits of an integer
int value = 14;
int n;
for (n=8*sizeof(int)-1;n>=0;n--) {
printf("%d",(value >>n)&1);
}
The book "Let Us C" doesn't define it as a built-in function. The showbits() function is defined in later part of the book as a complete user defined function as I personally went through it. This answer is to anyone who haven't reached that later part of the book and are stuck at the first appearance of the function in the book.
you need to look here
#downvoter It works fine in c also. You just need to reformat your code in c-style.
#include <stdlib.h>
#include <stdio.h>
int main()
{
char buffer[20];
int i = 3445;
_itoa( i, buffer, 2 );
printf("String of integer %d (radix 2): %s",i,buffer);
return 0;
}
*you need to save your file as .c in MSVC++ for _itoa() to work.*
this is the header file for showbits
void showbits(unsigned int x)
{
int i;
for(i=(sizeof(int)*5)-1; i>=0; i--)
(x&(1u<<i))?putchar('1'):putchar('0');
printf("\n");
}
No there is no pre built function and you do not need to include any specific header file.
You will have to provide implementation for function showbits(int).
#include <stdio.h>
void showbits(int);
int main()
{
int j,k;
for(j=0;j<=11;j++)
{
printf("\nBinary value of decimal %d is :",j);
showbits(j);
}
return 0;
}
showbits(int n){
int i,k,andmask;
for(i = 15;i>=0;i--){
andmask = 1<<i;
k = n & andmask;
k==0 ? printf("0"):printf("1");
}
}
If you want to print out the bits of a float, for example you could do something like:
float myFloat = 45.2;
for (int n=0;n<8*sizeof(float);n++) {
printf("%d",(myFloat>>n)&1);
}