Follow us on Twitter!
It is never to LATE to become what you never WERE.
Thursday, April 17, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 22
Members Online: 1

Registered Members: 82822
Newest Member: TheBunter
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C - Shifting Strings

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 12-10-10 22:44
I was finishing my Huffman Code, and then i realized i was in real trouble. I'm trying to print characters that have not been compressed as simple char type, but when i need to print compressed characters, i need to print them bitwise. The fact is, if i throw everything into a string, i'm not able to shift the elements to the left or right, because of memory corruption errors.

The question is: "Is there any way to shift strings (preferrably dinamically allocated ones), to the left or right, without memory corruption errors?"

I've been thinking of using unions, but i have never used unions before. Also, I don't have a specific size of file, unless i use a very huge constant like:

Code
union my_union{
    char my_big_array[ 9999999 ] ;
};


Author

RE: C - Shifting Strings


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 13-10-10 09:47
Code

#include <stdio.h>
#include <string.h>

int
main( void )
{
    char string[13] = "Hello Kitty!";
    int   i, j,
          len       = strlen( string );

    for( i = 0 ; i < what_you_want ; ++i )
    {
        for( j = len-1 ; j > 0 ; --j )
            string[j] = string[j-1];
        *string = ' ';
    }

    printf( "%s", string );

    return( 0 );
}





Edited by on 13-10-10 09:48
Author

RE: C - Shifting Strings

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 14-10-10 02:07
I was talking about bitwise string shift, so that piece of code does not help me out.

I have solved my problem with the use of a really complex algorithm, which i believe, i'm the only person that understands it.
Author

RE: C - Shifting Strings


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 14-10-10 05:41
chess_rock wrote:
I have solved my problem with the use of a really complex algorithm, which i believe, i'm the only person that understands it.

Wow, aren't we humble? By all means, do post your code anyway, might be interesting to some how you decided to solve your problem. Especially since you substituted a double linked list for a tree.


Author

RE: C - Shifting Strings

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 14-10-10 05:47
COM wrote:
chess_rock wrote:
I have solved my problem with the use of a really complex algorithm, which i believe, i'm the only person that understands it.

Wow, aren't we humble? By all means, do post your code anyway, might be interesting to some how you decided to solve your problem. Especially since you substituted a double linked list for a tree.


Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: C - Shifting Strings

yours31f
Member



Posts: 1678
Location: Dallas Texas
Joined: 27.04.07
Rank:
Elite
Posted on 14-10-10 16:49
Complex does not always mean good.


Debugging is what programmers do to beta software to make it take up more room on your hard drive if it is running too efficiently.


img259.imageshack.us/img259/3713/sigr.png

yours31f@live.com yours31f@yahoo.com rpwd.info
Author

RE: C - Shifting Strings


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 14-10-10 19:42
Ok, here's another one:

Code

#include <stdio.h>
#include <string.h>

void
shift( char *str, int act, int carry, int times )
{
    static int size;
    int        bit;
    char       tmp;

    if( times <= 0 )
        return;

    if( act == 0 )
        size = strlen( str );

    bit      = str[act] & 1;                           /* the most right bit  */

    tmp      =   (str[act] >> 1)                       /* shift right and add */
               | (carry << (8 * sizeof( int ) - 1) );  /* the most left bit   */

    str[act] = tmp;

    if( ++act < size )
        shift( str, act, bit, times );             /* continue with the carry */

    if( --times > 0 )
        shift( str, 0,   0,   times );            /* start from the beginning */

    return;
}

int
main( void )
{
    char
        str[] = "Hello Kitty!";

    shift( str, 0, 0, 1);

    printf( "%s\n", str );

    return( 0 );
}



Author

RE: C - Shifting Strings

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 15-10-10 01:12
stealth- wrote:
Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.


COM, stealth- is right. It is not by any means genius, but, confusing poor coding. I will still fix the code and make it look readable, for i haven't used many comments and some functions are ridiculously huge. Below you can see my poor coded functions, yet complex, to solve my shifting problem:

Code
void FLE_store_all_compressed( char * comp_file ,
                   List * most ,
                   char caption ,
                   int * counter_bit ,
                   int * counter_byte )
{
  unsigned char temp ;
   
  int flag ;
  int i ;
  int how_many_bits ;
   
  List * runner ;
 
  flag = 0 ;
   
  for( runner = most ; runner!= NULL ; runner = runner->before )
  {
    if( runner->letter == caption )
    {
      flag = 1 ;
      break ;
    }
  }
  if( flag == 0 )
  {
    for( i = 0 ; i < 8 ; i++ )
    {
      temp = caption ;
      comp_file[ *counter_byte ] |= ( ( ( temp >> ( 7 - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;
 
      *counter_bit += 1 ;
      if( *counter_bit > 7 )
      {
    *counter_bit = 0 ;
    *counter_byte += 1 ;
      }
    }
  }
  else
  {
    temp = runner->amount ;
    for( i = 0 ; i < 8 ; i++ )
    {
      if( ( runner->amount >> i ) == 0 )
    break ;
    }
    how_many_bits = i - 1 ;
   
    temp = runner->amount ;   
   
    for( i = 0 ; i <= how_many_bits ; i++ )
    {
      comp_file[ *counter_byte ] |= ( ( ( temp >> ( how_many_bits - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;
     
      *counter_bit += 1 ;
      if( *counter_bit > 7 )
      {
    *counter_bit = 0 ;
    *counter_byte += 1 ;
      }
    }
  }
}




If you pay close attention, you'll see the meaning of complex, yet not well done:

Code
comp_file[ *counter_byte ] |= ( ( ( temp >> ( how_many_bits - i ) ) & 0x01 ) << ( 7 - *counter_bit ) ) ;



Author

RE: C - Shifting Strings


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 15-10-10 12:07
chess_rock wrote:
stealth- wrote:
Lol, I think he was more referring to have used crazy, hard to follow programming practices than his ultimate genius above us all.


COM, stealth- is right.

Then sorry, my mistake.

I checked your code out and took the liberty of changing some things up. This ought to work as you intended. Try it, look through it or ignore it, whatever you prefer.

Code
void FLE_store_all_compressed( char * comp_file , List * most , unsigned char caption , int * counter_bit , int * counter_byte )
{
    bool flag;
    int i;

    List * runner ;
    (*counter_bit) %= 8;

    for( runner = most ; runner!= NULL ; runner = runner->before )
    {
        if( runner->letter == caption )
        {
            flag = true ;
            break ;
        }
    }
    if( !flag )
    {
        comp_file[*counter_byte] |= caption >> *counter_bit;
        comp_file[(*counter_byte)+1] |= caption << (8 - *counter_bit);
        (*counter_byte)++;
        return;
    }
    for( i = 0 ; i < 8 ; i++ )
    {
        if( ( runner->amount >> i ) == 0 )
            break ;
    }

    comp_file[*counter_byte] |= (runner->amount << (8 - i)) >> *counter_bit;
    if( i + (*counter_bit) > 7 )
    {
        (*counter_byte)++;
        comp_file[*counter_byte] |= runner->amount << i + (*counter_bit) - 8;
        (*counter_bit) -= 8;
    }
}