- Generate String From Regex
- C Generate Unique Key From String Lights
- C Generate Unique Key From String To Word
Generate String From Regex
Hi Barun Parichha,
This is not my field of expertise but I may be able to help?
I assume that you are looking for a 16-digit (not 16 byte) hex number in 4x2 byte decimated hex notation to denote a unique number? I also assume that this unique number MUST be unique from all other numbers generated? Something like a serial number generator, but in 4x2 hex decimated notation.
Your idea of using a random number generator, seeded by current time, although innovative, I don't believe will guarantee you a unique number, with respect to numbers previously generated.
Your second suggestion would be the method that I would consider, especially if you have the flexibility of 8 bytes (16 hex digits) to play with. Providing the unique number is only ever generated on a single system where you can guarantee that the clock will always be accurate in terms of ‘real world’ time, timezone and daylight savings etc, this should guarantee you a unique number. Before I go further, 2 explanations are required:
1. Single system: If you have multiple systems generating a number based purely on time, there is always the chance that both systems may generate the same number. If you are using multiple systems, you will need to consider this and perhaps incorporate a unique feature of each system into the number generation equation, such as MAC address etc.
2. Real world time: Should the system generating this unique number, based on time, not be accurate with its time keeping, there is the risk of producing duplicate numbers. Also be aware of daylight savings and its time transitions!
Assuming a single system with good clock keeping skills, no daylight saving, single timezone and delays of at least 10 seconds, to get a simple 4 bytes of 'unique' hex output, my approach might be:
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
void stoupper (char *src) {
// Convert chars in string to upper case
while (*src != '0')
*(src++) = toupper (*src);
}
int main (int argc, char *argv[]) {
struct timeval tv;
struct timezone tz;
char seq[6];
union {
unsigned int t;
unsigned char ct[4];
} u;
gettimeofday (&tv,&tz);
u.t=(unsigned int)tv.tv_sec;
// Number of seconds since the Epoch – 1 Jan 1970 00:00:00
// Write time as 2x4 byte hex values
// Include byte-reversal for Intel architecture
sprintf (seq,'%02x%02x.%02x%02x',(unsigned char)u.ct[3],(unsigned char)u.ct[2],(unsigned char)u.ct[1],(unsigned char)u.ct[0]);
// Convert the hex values to upper case
stoupper (seq);
printf ('8bit hex time = (%s)n',seq);
return (0);
}
You still have 4 bytes to use if you wish to incorporate multiple systems, time issues etc.
I hope that helps,
BitBuster.
This is not my field of expertise but I may be able to help?
I assume that you are looking for a 16-digit (not 16 byte) hex number in 4x2 byte decimated hex notation to denote a unique number? I also assume that this unique number MUST be unique from all other numbers generated? Something like a serial number generator, but in 4x2 hex decimated notation.
Your idea of using a random number generator, seeded by current time, although innovative, I don't believe will guarantee you a unique number, with respect to numbers previously generated.
Your second suggestion would be the method that I would consider, especially if you have the flexibility of 8 bytes (16 hex digits) to play with. Providing the unique number is only ever generated on a single system where you can guarantee that the clock will always be accurate in terms of ‘real world’ time, timezone and daylight savings etc, this should guarantee you a unique number. Before I go further, 2 explanations are required:
1. Single system: If you have multiple systems generating a number based purely on time, there is always the chance that both systems may generate the same number. If you are using multiple systems, you will need to consider this and perhaps incorporate a unique feature of each system into the number generation equation, such as MAC address etc.
2. Real world time: Should the system generating this unique number, based on time, not be accurate with its time keeping, there is the risk of producing duplicate numbers. Also be aware of daylight savings and its time transitions!
Assuming a single system with good clock keeping skills, no daylight saving, single timezone and delays of at least 10 seconds, to get a simple 4 bytes of 'unique' hex output, my approach might be:
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
void stoupper (char *src) {
// Convert chars in string to upper case
while (*src != '0')
*(src++) = toupper (*src);
}
int main (int argc, char *argv[]) {
struct timeval tv;
struct timezone tz;
char seq[6];
union {
unsigned int t;
unsigned char ct[4];
} u;
gettimeofday (&tv,&tz);
u.t=(unsigned int)tv.tv_sec;
// Number of seconds since the Epoch – 1 Jan 1970 00:00:00
// Write time as 2x4 byte hex values
// Include byte-reversal for Intel architecture
sprintf (seq,'%02x%02x.%02x%02x',(unsigned char)u.ct[3],(unsigned char)u.ct[2],(unsigned char)u.ct[1],(unsigned char)u.ct[0]);
// Convert the hex values to upper case
stoupper (seq);
printf ('8bit hex time = (%s)n',seq);
return (0);
}
You still have 4 bytes to use if you wish to incorporate multiple systems, time issues etc.
I hope that helps,
BitBuster.
C Generate Unique Key From String Lights
C Generate Unique Key From String To Word
Unique key having length of 8. I have to generate the unique key having a length of 8, which should be comprised of some set of characters. Store the last. Sep 14, 2010 I need to generate a unique 8-digit number for a database field to be used as an employee ID. This field is used in course registrations. Will serve as a temporary number for incoming employees, a. Generate a unique 8-digit number for a Primary Key field. To generate the IDNumber you could use an autonumber, but this might not necessarily. May 27, 2014 Do you want to generate a unique id in C#?. You can use Guid to generate unique data or ID. Below is a sample code snippet that demonstrates how to generate unique ID in C#. How to Generate Unique ID in C#? Using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using. Unique Sequence Number Generator in C. And after every 10 sec I have to generate some unique new key. The key must be 16byte hex code (e.g FFFF.FF01.FF35.1234.