Wednesday, October 14, 2009

Convert an 32bit int to array of char in C






int
main()
{
char d[5];
unsigned int a=-123456789;
// int to array of char
char* p= (char *)(&a);
for(int i=0; i <4; i++)
d[i] = *(p+i); // store the int in 'd'
d[5] = '\0'; // add end character
// array of char to int
int out;
p= (char *)(&out); // create a char pointer to the first byte of the int
for(int i=0; i <4; i++)
*(p+i)=d[i];
printf("%d\n",out);
return 0;
}

Friday, September 11, 2009

vim: Automatic replace of tabs with spaces

Add the following lines to ~/.vimrc:

set tabstop=4
set expandtab

To convert the TAB-s to spaces in edited files use

:retab

Monday, June 15, 2009

Stream your soundcard using pulseaudio and Nokia N95

This guide will briefly explain how to create a network music stream server using the sound actually played on your sound card. I use to find this trick especially useful when I m sitting on my balcony with my laptop and in the same I can stream Pandora web radio via WiFi into my home music system (for the great happiness of my neighbours).

What you will need:

1. A computer with network connection.
2. A mobile phone able to connect to your PC via network and with the ability to play network mp3 streams. (I have an N95 8G phone with installed player CorePlayer. )


Setup:

  1. Install pavucontrol, vlc player and sox.
  2. Get the name of your sound card monitor device. Run in terminal:

    pactl list | grep -A2 'Source #' | grep 'Name: .*\.monitor$' | cut -d" " -f2

    The command should return something like this:

    alsa_output.pci_8086_293e_sound_card_0.monitor

  3. Run pavucontrol from your terminal and in 'Input devices' set the 'Show' option to 'All devices' . Find the monitor device and make sure that it is not muted.

  4. Configure your network for some IP. For example 192.168.2.2.


  5. Start up some music in your favourite music player.


  6. Now let's convert the output of the monitor device of your sound card to an mp3 stream. Run this in the terminal:

    parec --device=alsa_output.pci_8086_293e_sound_card_0_alsa_playback_0.monitor --format=s16le --rate=44100 --channels=2 | sox --type raw -s2L --rate 44100 --channels 2 - --type wav - | cvlc -v - --sout '#transcode{vcodec=mp4v,acodec=mp3,vb=800,ab=128}:standard{access=http,mux=raw,dst=192.168.2.2:8080}'



    where
    pci_8086_293e_sound_card_0_alsa_playback_0.monitor is the name of your monitor device and 192.168.2.2:8080 is your PC's IP address with the streaming port. Yes, it looks freaky.

  7. Connect your audio input of your mobile phone to your home audio system and play the 192.168.2.2:8080 stream in your mobile. With
    few seconds delay you should hear the output of your PC's sound monitor device.

Sunday, June 14, 2009

Good bye Ubuntu

After four years on Ubuntu (my first install was Hoary) I decided to install Fedora 11 and say goodbye to Ubuntu. The reason is simple, the latest release Jaunty is simply an alpha software. I think I have quite solid knowledge to hack and usually get working all the X, WLAN and audio problems but Jaunty is simply too much pain. Good bye Ubuntu.