Tuesday, April 26, 2016

C++: Convert a string to its hex form


#include <sstream>
#include <iomanip>

...

std::string toHex(const std::string &input)
{
    std::stringstream ss;
    ss.setf(ios::uppercase);
    ss << hex;

    for (size_t i = 0; i < input.length(); i++)
        ss << setw(2) << setfill('0') 
           << (unsigned short)(input[i] & 0xff);

    return ss.str();
}
 

Wednesday, March 30, 2016

C++ get array size with template


#include <iostream>

using namespace std;

template <typename T, unsigned int S>
unsigned int array_size(const T (&)[S])
{
    return S;
}

int main()
{
    char a[] = "Hello, world!";
    unsigned int s = array_size(a);
    cout << s << endl;
    return 0;
}


The output of the program is 14.

T(&)[S] is the type of the passed in parameter, i.e. T (&in)[S], where "in" is the reference of the array "a[14]" in our case. When doing the type deduction, the compiler will match T with char and S with 14. 

Wednesday, March 9, 2016

Android programming: UI Fragment hosted in Activity


AndroidManifest.xml:

<manifest package="com.solezero.android.learning"
           xmlns:android="http://schemas.android.com/apk/res/android">

    <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher" 
         android:label="@string/app_name" 
         android:supportsRtl="true" 
         android:theme="@style/AppTheme">
        <activity
             android:name=".MyActivity"
             android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>


MyActivity.java:

package com.solezero.android.learning;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

public class MyActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my);

        FragmentManager fragmentManager = getSupportFragmentManager();

        Fragment fragment = fragmentManager.findFragmentById(R.id.fragmentContainer);

        if (fragment == null) {
            fragment = createFragment();

           fragmentManager.beginTransaction().add(R.id.fragmentContainer, fragment).commit();        }
    }

    protected Fragment createFragment() {

        MyFragment myFragment = new MyFragment();

        return myFragment;
    }

}


layout/activity_my.xml:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/fragmentContainer"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />


MyFragment.java:

package com.solezero.android.learning;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {
    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my, container, false);

        return view;
    }
}

To access the Activity from the Fragment, use getActivity().


layout/fragment_my.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:orientation="vertical" 
               android:layout_width="match_parent"
               android:layout_height="match_parent">

</LinearLayout>


 
Get This <