How to generate 8 byte hex value? [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I want to generate this sequential data in C:
data_packet[1] = 0706050403020100 (seed_value)
next
data_packet[2] = 0f0e0d0c0b0a0908
Next will be the next 8 hexadecimal characters and so on for say 100 bytes. How can I do it? Can we do it using character array?

You don't want to use a char array since char may or may not be signed (implementation defined). If you are playing with hexadecimal numbers from 0x00 to 0xFF, I strongly recommend using an unsigned char array.
Looks like the values in the array are sequential, from 0 to N. This indicates using a for loop.
The array is a fixed size of 8 bytes. Hmmm, another good candidate for a for loop.
The hard part of your task is the direction of the bytes. For example are filling in the array starting at position 7 or at position 0?
So here is some psuedo code to help you along:
For each value from 0 to N do:
begin
for array position from 0 to 7 do: // or from 7 to 0
put 'value' into the array at 'position';
call function to process the array.
end
For more fun, change the functionality of "put 'value'" to "put random value".
If you want us to actually write your program, let us know. I could use the extra money. :-)

Related

How to generate random letters (capital letters) and save into a text file in C? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How i can generate for example 100 random letters (capital letters) and save into a text file in C?
i read rand() and srand() function for generate random numbers. but for letters, i don't know what can i do:(
If r is a random number in the range 0..25, 'A'+r is a random capital letter.
Hint:
fopen(file, "w")
(rand() % 26) 100 times and fputc('A' + your_random, file)
fclose(file)
In this case you get a pseudo-random integral number in the range between 0 and 25.
Read the tutorials on generating random numbers and file I/O in C.
There are several methods you could use to generate random numbers the simplest one being rand().
http://www.cplusplus.com/reference/cstdlib/rand/
And for file I/O
http://www.cprogramming.com/tutorial/cfileio.html
Read up on these and try what you asked. That should get you your solution.

Dynamically declare variables/structure in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For example, I have a file, which says
char,5
int,6
Reading the above file, is it possible to declare 2 variable array in the code? So in future
if i add a new line it will automatically declare?
No, not in C.
You will need to write a script which reads this file and writes the c program.
In short, what you need is a C Source Code Generator.
Sure, just code exactly what you want. You can start with a structure that can hold either a character or an integer (with some boolean or integer to indicate which). Then you can allocate an array of them of any size.
When you read the first line, create an array of 5 such structures. Set their type variable to "char".
When you read the second lien, increase the size by 6. Set those six new ones to be integers.
And so on.
You can use an enum to track the type of each entry in the array. You can use a struct to hold the integer value, character value, (or just re-use the integer value) and type. You can make helper functions like isInteger, setIntegerValue, getCharacterValue, and so on.

How to assign a hexadecimal value to an unsigned char variable in a C program [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The hexadecimal value that I want to store in a variable is of length 32.
"DC4938C31B9E8B30F32FC0F5EC894E16".
I also want to then print this value but I don't know the format specifier in printf for unsigned char.
That's a string of 32 characters, being hexadecimal digits. That makes 16 bytes, or 128 bits.
m0skit0 told you how to store it in a string. If you actually want to store that in an integer, you'd need something like unsigned long long intvar = strtoull( stringvar, NULL, 16 ) - provided "long long" on your machine can stomach 128 bits.
But what you want is something completely different, which became clear only after you linked to that other question. (It's really bad to take something out of context like that, especially if you are confused about what you're actually doing.)
If you take a look at the API documentation, you will see that the parameter you are looking at is a pointer to DES_cblock. That's not a string, and not an integer. Have a look at seed and ivsetup in that other question, how they are initialized, and think for a minute.
That's not a char. It's a string:
unsigned char* value = "DC4938C31B9E8B30F32FC0F5EC894E16";
printf("%s\n", value);
Also, if this is a number I strongly do not suggest you transforming it into a string or char, since strings are slower and prone to more coding errors than numbers.
Numbers represented as hexadecimal (and not hexadecimal numbers, all numbers are hexadecimal, it's just a representation of the value) do not have any characters. Again, it's a number. You can of course convert it to a string (as with any other number) but to do so you should have a strong reason (e.g. the algorithm you want to use is faster with strings than numbers).
I suggest you reading about representation of numbers. A lot of new programmers have problems with this topic.

Large number store in float type in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
The problem I got is if I assign a large number to a float type, for example, float f = 1.0e20 then I print it to the screen with printf("f = %f\n",f), then on the screen it will be f = 100000002004087730000.000000. Could anyone tell me why the number display is not 100000000000000000000.000000. Thanks in advance.
The number you are using has no exact representation in the type you are storing it. As a result, the answers will never be exactly right.
By analogy, consider a computer that used 6 decimal digits. The best you can do for 1/3 is .333333 But then 3 * (1/3) != 1. Oh well.
And what about 2/3? If you use .666667 then 2/3 != 2 * (1/3). If you use .666666 then 1/3 + 2/3 != 1. Oh well.
That's just the way it is with floating point numbers.
Summarizing, floating point variables are represented as a aproximation of a fixed number which is later scaled with an exponent. In your case, the number is too big to being stored in a accurate way. Try using a double instead and printing it out with %lf. It'll be more accurate, although don't expect a real equality.

Reverse the place of the string in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want reverse the string in particular format.
For example, "My name is Nishant" should be converted to "Nishant is name My".
If you have your words in a char[] words array then it is a simple loop:
for (i = 0; i < mid; i++)
exchange(words[i], words[number_of_words - i]);
for sane definitions of mid, number_of_words and exchange.
If all you have is a big char containing the entire statement, doing a strtok first is helpful. Then, use the above loop.
Give our regards to your instructor. If this is a homework assignment, you should write the code yourself.
Here is a little hint, though: use a char pointer to iterate through each character in the array until you hit the NUL terminator at the end. Now iterate in reverse until you hit a space. Save your place in another pointer, move forward by one then copy each of the characters up to but not including the NUL into your output buffer.
Now retrieve the position of that last space in that other pointer where you saved your place, and back up again. When you move forward, you actually need to stop when you encounter either a space of a NULL - ASCII '\0' or a zero byte - and not just a NUL.
It would be a little faster if you save the positions of each of the spaces in some kind of list as you iterate forward at the beginning. That way you don't then need to iterate backward over the entire string, with short iterations over each word. The code would be a little more complicated.
The increased efficiency would be insignificant for short strings like individual English sentences, but would be quite a lot of you were reversing a large file that you just read into memory.

Resources