Exemplo n.º 1
0
char* header(int numero){										//Recibe numero de bytes, y lo devuelve en 4 bytes (Ej. recibe "2" y devuelve "0002")
	char* asd=string_new();
	char* num_char = string_itoa(numero);
	num_char = string_reverse(num_char);
	string_append(&asd,num_char);
	free(num_char);
	string_append(&asd,"0000");
	char* longitud=string_substring(asd,0,4);
	free(asd);
	char* longitudPosta=string_reverse(longitud);
	free(longitud);
	return longitudPosta;
}
Exemplo n.º 2
0
// convert 64 bit unique encoded hex to 10 hex digit 40 bit UID
// this is the same as em4x02 but with final output bits in a different order
BOOL unique_hex_to_hex(unsigned char *hex, unsigned char *unique)
{
    BYTE tmp[64];

    if(em4x02_hex_to_bin(tmp, unique))
    {
        string_reverse(tmp, 40);
        binarraytohex(hex, tmp, 40);
        string_byteswap(hex,10);
        string_reverse(hex, 10);
        return TRUE;
    }
    return FALSE;
}
Exemplo n.º 3
0
/**
  * Converts a given integer into a base 10 ASCII equivalent.
  *
  * @param n The number to convert.
  * @param s Pointer to a buffer in which to store the resulting string.
  * @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
  */
int itoa(int n, char *s)
{
    int i = 0;
    int positive = (n >= 0);

    if (s == NULL)
        return MICROBIT_INVALID_PARAMETER;

    // Record the sign of the number,
    // Ensure our working value is positive.
    if (positive)
        n = -n;          

    // Calculate each character, starting with the LSB.    
    do {       
         s[i++] = abs(n % 10) + '0';
    } while (abs(n /= 10) > 0);
    
    // Add a negative sign as needed
    if (!positive)
        s[i++] = '-';
    
    // Terminate the string.
    s[i] = '\0';
    
    // Flip the order.
    string_reverse(s);

    return MICROBIT_OK;
}
Exemplo n.º 4
0
void main() {
	char fwd[] = "This is the forward string";
	char* reversed;
	printf("string: %s \n", fwd);
	reversed = string_reverse(fwd);
	printf("string: %s \n", reversed);
	free(reversed);
}
Exemplo n.º 5
0
/* Description : Calculate the sum of two integers which given as two strings
 * Input       : a & b -- input integer as string
 * Output      : ppsum -- the result of a plus b
 * Note        : since ppsum is allocated inside, so you need to free it
 */
int big_number_sum(char* a, char* b, char** ppsum)
{
    if (NULL == a || NULL == b)
    {
        printf("Invalid param: a=%p, b=%p.\n", a, b);
        return -1;
    }

    int a_len = strlen(a);
    int b_len = strlen(b);
    // alloc 2 more bytes for carry and '\0'
    int sum_len = (a_len > b_len) ? a_len : b_len + 2;
    char* sum   = (char*)malloc(sum_len);
    if (NULL == sum)
    {
        printf("alloc memory failed.\n");
        return -1;
    }
    memset(sum, 0, sum_len);

    int carry = 0;
    for (int i = 0; i < sum_len-1; i++)
    {
        int left  = (i < a_len) ? a[a_len-1-i] - '0' : 0;
        int right = (i < b_len) ? b[b_len-1-i] - '0' : 0;

        sum[i] = left + right + carry;
        carry  = sum[i] / 10;
        sum[i] = sum[i] % 10 + '0';
    }

    if ('0' == sum[sum_len-2])
    {
        // if carry is '0', replace it '0' with '\0'
        sum[sum_len-2] = '\0';
        string_reverse(sum, sum_len-3);
    }
    else
    {
        string_reverse(sum, sum_len-2);
    }

    *ppsum = sum;
    return 0;
}
Exemplo n.º 6
0
// convert human readable UID to 128 bit fdx-b binary array
BOOL uid_to_hdx_bin(BYTE *bin, BYTE *uid)
{
    BYTE tmp1[64], crc[8], i;
    unsigned int country, crctag;
    unsigned long long id;

    memset(tmp1, 0x00, 64);

    // set animal flag
    if(uid[0] == 'A')
        tmp1[0]= 0x01;
    else
        if(uid[0] != '0')
            return FALSE;

    // set data flag
    if(uid[1] == 'D')
        tmp1[15]= 0x01;
    else
        if(uid[1] != '0')
            return FALSE;

    // set country code - 4 hex digits -> 10 bits
    country= bcdtouint(uid + 2, 4);
    inttobinarray(tmp1 + 16, country, 10);

    // set national ID - 12 hex digits -> 38 bits
    id= bcdtoulonglong(uid + 6, 12);
    ulonglongtobinarray(tmp1 + 26, id, 38);

    // reverse binary
    string_reverse(tmp1, 64);

    // add header for over-the-air: 10 x 0x00 + 0x01
    memset(bin, 0x00, 10);

    // every 9th bit is 0x01, but we can just fill the rest with 0x01 and overwrite
    memset(bin + 10, 0x01, 118);

    //data is 8 blocks of 8 bits, plus obfuscation bit
    for(i= 0 ; i < 8 ; ++i)
        memcpy(bin + 11 + i * 9, tmp1 + i * 8, 8);

    // calculate & append crc for 64 bits of data
    for(i= 0 ; i < 8 ; ++i)
        crc[i]= (BYTE) binarraytoint(tmp1 + i * 8, 8);
    crctag= crc_ccitt(crc, 8);
    inttobinarray(bin + 83, crctag >> 8, 8);
    inttobinarray(bin + 92, crctag, 8);

    // add trailer
    for(i= 0 ; i < 3 ; ++i)
        memset(bin + 101 + i * 9, 0x00, 8);

    return TRUE;
}
Exemplo n.º 7
0
// convert null-terminated hex UID (10 digits) to unique encoded 64 bit binary array
// which is the same as EM4X02 but with source hex arranged in a different order
BOOL hex_to_unique_bin(unsigned char *unique, unsigned char *hex)
{
    unsigned char tmp1[10], tmp2[40];

    if(strlen(hex) > 10)
        return FALSE;

    // pad to 10 hex digits
    memset(tmp1, '0', 10);
    memcpy(tmp1 + 10 - strlen(hex), hex, strlen(hex));
    // juggle into correct nibble order
    string_reverse(tmp1, 10);
    string_byteswap(tmp1, 10);
    // convert to binary
    hextobinarray(tmp2, tmp1);
    string_reverse(tmp2, 40);
    // encode for em4x02
    bin_to_em4x02_bin(unique, tmp2);
    return TRUE;
}
Exemplo n.º 8
0
void itoa(int n, char s[])
{
    int i = 0, sign;

    if ((sign = n) < 0)
        n = -n;

    do {
        s[i++] = n % 10 + '0';
    } while ((n/=10) > 0);

    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';

    string_reverse(s);
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
	char *output;

	if (argc != 2) {
		printf("Enter a string to copy\n");
		return -1;
	}

	printf("Input: %s\n", argv[1]);
	string_reverse_in(argv[1]);
	printf("Reverse:  %s\n", argv[1]);
	output = string_reverse(argv[1]);
	printf("Another reverse:  %s\n", output);

	string_reverse_done(output);

	return 0;
}
Exemplo n.º 10
0
// copy raw UID to data blocks for re-transmission by target tag
void tag_raw_uid_to_data(BYTE *data, BYTE *uid, BYTE host_tagtype)
{
    BYTE            tmp[MAXUID + 1];
    unsigned int    i;
    
    switch(host_tagtype)
    {
        // EM4X05 sends everything LSB, so reverse data
        case TAG_TYPE_EM4X05:
            hextobinarray(tmp, uid);
            for(i= 0 ; i < HEXTOBITS(strlen(uid)) ; i += EM4X05_BLOCKSIZE)
                string_reverse(tmp + i, EM4X05_BLOCKSIZE);
            binarraytohex(data, tmp, HEXTOBITS(strlen(uid)));
            break;
            
        // for most tags a straight copy will do
        default:
            strcpy(data, uid);
            break;
    }
}
Exemplo n.º 11
0
unsigned int bin_to_decimal (const QString &s)
{
  unsigned int table[31];
  unsigned int c = 1;
  unsigned int result = 0;
  QString sn = string_reverse (s);

  table[0] = 1;

  for (int i = 1; i < 32; i++)
      {
       c *= 2;
       table[i] = c;
      }

  for (int i = 0; i < sn.size(); i++)
      if (sn[i] == '1')
         result += table[i];

  return result;
}
Exemplo n.º 12
0
int main()
{
    char from[] = "hello";
    char to[20];
    char a[10] = {'1','2','3','4'};
    char s[20] = "hai";
    char t[6] = "world";


    string_copy(to, from);
    printf("to: %s - from: %s \n", to, from);

    printf("length = to: %d - from: %d \n",
           string_length(to), string_length(from));

    printf("a[] = %s -- atoi = %d \n", a, atoi(a));

    printf("lower: %c \n", lower('C'));
    printf("upper: %c \n", upper('x'));

    char name[20] = "sangeeth";
    squeez(name, 'e');
    printf("squeez(sangeeth) : %s \n", name);

    printf("strcat(%s, %s): ", s, t);
    string_concat(s, t);
    printf("%s \n", s);

    printf("strrev(%s): ", from);
    string_reverse(from);
    printf("%s \n", from);

    int num = -1234;
    char numc[10];
    itoa(num, numc);
    printf("itoa(%d): %s \n", num, numc);

    return 0;
}
Exemplo n.º 13
0
int sentence_reverse(char * sentence, int sentenceLength) {
    int headIndex = 0;
    int tailIndex = 0;

    while (headIndex < sentenceLength && tailIndex < sentenceLength) {
        if (sentence[tailIndex] == ' ') {
            tailIndex++;
        } else {
            // we found the start of a word, find the end
            while (headIndex + 1 < (sentenceLength) && sentence[headIndex + 1] != ' ' &&
                    sentence[headIndex + 1] != '\0') {
                headIndex++;
            } // we found the end of the word

            // call string_rev on it
            string_reverse(sentence + tailIndex, headIndex - tailIndex + 1);

            tailIndex = headIndex + 1;
            headIndex++;
        }
    }
    return 0;
}
Exemplo n.º 14
0
// convert fdxb 128 bit binary array to human readable UID
// format is ADCCCCIIIIIIIIIIII where A is 'A' or '0' for animal / non-animal,
// D is 'D' or '0' for Data block available / No data available, 
// CCCC is ISO-3166 country code or ICAR.ORG manufacturer code
// IIIIIIIIIIII is national ID
BOOL hdx_hex_to_uid(BYTE *uid, BYTE *hex)
{
    BYTE tmp[128];
    unsigned int country;
    unsigned long long id;


    // strip headers etc.
    if(!fdxb_hex_to_bin(tmp, hex))
        return FALSE;

    // reverse binary
    string_reverse(tmp, 64);

    // output animal flag
    if(tmp[0])
        uid[0]= 'A';
    else
        uid[0]= '0';

    // output data flag
    if(tmp[15])
        uid[1]= 'D';
    else
        uid[1]= '0';

    // output country/icar code
    country= binarraytoint(&tmp[16], 10);
    sprintf(&uid[2], "%04u", country);

    // output national ID
    id= binarraytoulonglong(&tmp[26], 38);
    sprintf(&uid[6], "%012llu", id);

    return TRUE;
}
Exemplo n.º 15
0
//[[Rcpp::export]]
std::string string_reverse_single(std::string str){
  return string_reverse(str);
}
Exemplo n.º 16
0
//[[Rcpp::export]]
std::vector < std::string > string_reverse_vector(std::vector < std::string > strs){
  return string_reverse(strs);
}
Exemplo n.º 17
0
int main()
{
    char s1[MAX], s2[MAX], choice;
    int temp;
    do
    {
        switch(display_menu())
        {
        case 1: //Compare 2 strings
            read_string(s1);
            read_string(s2);
            temp = string_compare(s1,s2);
            if(temp) //Strings are not equal
            {
                if(temp == 1)
                    printf("String 1: %s\nis greater than\nString 2: %s!\n",s1,s2);
                else //temp == -1
                    printf("String 2: %s\nis greater than\nString 1: %s!\n",s2,s1);
            }
            else //temp == 0
            {
                printf("Both the strings are equal!\n");
            }
            break;

        case 2: //Copy a string
            read_string(s1);
            string_copy(s2,s1);
            printf("Original String: %s\nCopied String: %s\n",s1,s2);
            break;

        case 3: //Reverse string
            read_string(s1);
            string_reverse(s2,s1);
            printf("Original String: %s\nReversed String: %s\n",s1,s2);
            break;

        case 4: //Check whether string is a palindrome
            read_string(s1);
            temp = string_palindrome(s1);
            if(temp) //temp == 1, if palindrome
                printf("The string \" %s \" is a palindrome.\n",s1);
            else
                printf("The string \" %s \" is not a palindrome.\n",s1);
            break;

        case 5: //Check substring
            read_string(s1);
            printf("Substring:\t");
            read_string(s2);
            temp = substring(s1,s2);
            if(temp) //temp == 1 means substring present
                printf("The substring \" %s \" is a part of \" %s \" .\nIt occurs %d times.\n",s2,s1,temp);
            else //Not a substring, hence temp == 0
                printf("The substring \" %s \" is NOT a part of \" %s \" .\n",s2,s1);
            break;
        }
        printf("Do you want to run the program again? Press 'Y' or 'y' for yes. ");
        scanf("%c",&choice);
    }
    while((choice == 'Y') || (choice == 'y'));
    return 0;
}
Exemplo n.º 18
0
String StringUtil::Reverse(CStrRef input) {
  if (input.empty()) return input;
  int len = input.size();
  return String(string_reverse(input.data(), len), len, AttachString);
}
Exemplo n.º 19
0
int main(void)
{
    char *text = NULL;
    size_t len = 0;
    int count = 0;
    char result[100];
    int index = 0;
    int check = 0;

    char* wordsArr[cMaxNumberWords];
    char* palindromes[cMaxNumberWords];


    printf("text --> ");
    getline(&text, &len, stdin);

    wordsArr[count] = strtok(text, ", .?!");
    while ((NULL != wordsArr[count]) && (count < cMaxNumberWords))
    {
        count++;
        wordsArr[count] = strtok(NULL, ", .?!");
    }

    int i;
    for (i = 0; i < count; i++)
    {
        int size = strlen(wordsArr[i]);
        if ('\n' == wordsArr[i][size - 1])
        {
            wordsArr[i][size - 1] = '\0';
            size--;
        }

        string_reverse(wordsArr[i], result, size);

        if (0 == strcmp(wordsArr[i], result))
        {
            int j;
            for (j = 0; j < index; j++)
            {
                if (0 == strcmp(wordsArr[i], palindromes[j]))
                {
                    check = 1;
                }
            }

            if (0 == check)
            {
                palindromes[index] = malloc(cMaxWordSize);
                if(palindromes[index] == NULL)
                {
                    exit -1;
                }

                strcpy(palindromes[index],wordsArr[i]);

                index++;
            }
        }
    }

    sort(index, palindromes);
    print(index, palindromes);

    free(text);

    return 0;
}