Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Saturday, December 7, 2013

LSB in steganography

Steganography is very interesting. A simple method is LSB. We hide our information bit by bit in every pixel, and the number of bits and position of hidden bits are determined by our program. I've finished LSB for a long time but don't spread my implementation considering my poor programming ability. But impressed by my "elegant" code, I decided to share it in this blog. (In fact, I was inspired by someone else's code but couldn't think of where and who, please contact me if you have any opinions)

MATLAB makes people process image quite easily, so I used MATLAB to finish it. Here is my code :

% secret : matrix made of hidden infomation
% carrier: matrix made of pixel
% extract: matrix to store hidden information
% N: position, usually equaling 1
carrier = bitor( bitand(carrier, bitcmp(2^N-1, 8)), secret );
extract = uint8( bitand( 255, bitshift(carrier, 8-N)));

P.S. To implement a function, you might need dec2bin() mat2str() bin2dec() for support.

Monday, November 11, 2013

Alternately Store Elements of Two Char Arrays

Recently I was asked a question, i.e. how to output two char arrays alternately. It is easy to work out, so the main point here may be to practice my writing skills and to increase the number of my blogs. By the way, record my solution. I chose to store them first and then you could do anything as you like. Maybe output it. Maybe pass it. Whatever.

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

int main()
{
    char a[512]={0}, b[512]={0}, buf[1024]={0};
    int i=0, length_a=0, length_b=0, minlength=0;
 
    scanf("%s", a);
    scanf("%s", b);

    length_a = strlen(a);
    length_b = strlen(b);
    minlength = length_a > length_b ? length_b : length_a;
    for(i=0; i<minlength; ++i)
    {
        buf[i*2] = a[i];
        buf[i*2+1] = b[i];
    }

    while (i < length_a)
    {
        buf[i+length_b] = a[i++];
    }

    while (i < length_b)
    {
        buf[i+length_a] = b[i++];
    }

    printf("%s", buf);

    return 0;
}





Thursday, October 31, 2013

Sieve of Eratosthenes C++

I think Wikipedia has introduced it very clearly, and I implemented it on my machine with Code::Block. It works quite well.

#include <stdio.h>
#include <math.h>

#define MAX 1000001

int main()
{
    int i=0, j=0, m=0;
    bool prime[MAX]={0};

    for(i=2; i<MAX; ++i) prime[i] = (i & 1);

    m = sqrt(MAX);
    for(i=3; i<=m; i+=2)
    {
        if (prime[i])
        {
            for(j=i*i; j<MAX; j+=i) prime[j] = false;
        }
    }

    scanf("%d", &m);
    for(i=2; i<m; ++i)
    {
        if (prime[i]) printf("%d ",i);
    }

    return 0;
}