tags:

views:

118

answers:

3

Hi I want to generate hexadecimal numbers in C starting with seed value(initial value) 0706050403020100.My next numbers should be 0f0e0d0c0b0a0908 and so on for next iteration.
In that way i want to generate numbers for say 1000 bytes.

1)how can i generate these hexadecimal numbers.

2)How to store these numbers if i want to compare the generated/produced hexadecimal numbers character by character with the data available in a buffer(dynamic) which has contents of a read file.

  Any suggestions/answers are most welcome as i am still learning C language.

EDIT:Here's the code i have tried.

#include<stdio.h>
#include<stdlib.h>

int main(){
  FILE *fp;
  char *buffer, c;
  size_t filesize, result;
  int i, expected_data[];

  fp = fopen("hex_data", "r");
  if (fp == NULL) {  
    fputs("Error\n", stderr);
    exit(1);
  }
  fseek(fp, 0L, SEEK_END);
  filesize = ftell(fp);
  printf("Size of hex_data file is:%u \n", filesize); 
  fseek(fp,0L,SEEK_SET); 
  buffer = (char*) malloc(sizeof(char)*filesize);

  if(buffer == NULL){
    fputs("\nMemory error ", stderr);
  }
  buffer_size = fread(buffer, sizeof(char), size, fp);
  for(i=0; i < result; i++) {
    printf("%c",*(buffer +i));
  }
  printf("No of elements read from file are:%u \n", buffer_size); 

  fseek(fp,0L,SEEK_SET);
  int current_pos = 0;
  while(current_pos < buffer_size) {
    if (buffer[current_pos] != expected_data) {
      fputs("Error\n",stderr);
    }
    else {
      current_pos++;
      expected_data = next_exp_data(data); //function is call hexadecimal numbers produced
    }
  }

Here i want to write a function to generate hex numbers for 1000 bytes starting from 0706050403020100.If this is the initial data everytime if i add 08 to each byte i should get the next number(til 1000 bytes).But i don't know how to do it.Can anyone help me out.

Any corrections in the code are most welcome.

A: 

Your numbers are large, so use a 64-bit unsigned integer data type, such as unsigned long long. You won't be able to deal with numbers greater than 2**64 without adopting a new scheme. Print hex values using printf("%x", value).

You can look at GNU GMP library for arbitrarily large integers.

Noah Watkins
+1  A: 

This will generate 1000 bytes of random hexadecimal numbers. (Or rather, the ASCII representation of 1000 hexadecimal digits.)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    int i;
    for (i=0; i<1000; i++) {
        printf("%x", rand()%16);
    }
    printf("\n");
    return EXIT_SUCCESS;
}

If you wanted to store them in a buffer to compare with something else later, you might want to look at sprintf instead of printf. For that, you really need to understand how strings are allocated and used in C first.

EDIT: This might be more what you're after. It reads hexadecimal digits from standard input (which can be redirected from a file if desired) and checks to see if they follow the pattern that you described.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    int expected_number = 7;
    unsigned int read_number;
    while (1 == scanf("%2x", &read_number)) {
        if (expected_number != read_number) {
            fprintf(stderr, "Expected %02x but got %02x.\n", expected_number, read_number);
        }
        expected_number--;
        if (expected_number == -1) expected_number = 15;
    }
    return EXIT_SUCCESS;
}
David
Thanks for your inputs David.But i want to produce sequential hexadecimal numbers so that it keep generating next eight numbers and so on.
Thej
Oh, ok, I get it. In that case, you probably don't need to **generate** them at all. If you're comparing them with existing data, just read in the existing data (using `scanf("%2x", ` or something similar) and each time you read a new pair of hex digits, compare them with an `int` counter that decreases from 15 until it gets down to 0, and then goes back to 15 again. See my edit above for some code that might help.
David
Thanks for your answers.But I want to write a application in C which reads contents of a file which has sequential data for 1000 bytes.I want to compare same data in my application and check if it is proper or not.my initial value(seed) is x0706050403020100(hex)(64 bit).I want to check for 1000 bytes.
Thej
@Thej: Unless I'm misunderstanding you, the code does just that. If you want more help then you need to explain what's wrong with the current solutions that are being offered.
David
@David:Thanks a lot.I didnt mean that way.I am not used to using commandline arguments in C.So i am having lil difficulty understanding what you are trying to do.Neverthless appreciate your code.
Thej
A: 

http://pastebin.com/hGBQzLej

Not random, but it's hexidecimal. :P

Alexander
What? This is UB.
strager
Okay; I made a little list. Don't pastebin SO answers. This is UB (as I stated) because the array `hex` is uninitialized. You meant to write `<` and not `<=` (more UB). Why are you printing the pointer? You probably meant to print the value. Why is there `cin.get()` there? Why are you specifying `long`? It's preferred that you don't `using namespace std;` in the global namespace. And worst of all: this question is about C, not C++.
strager