Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why numbers converted from char to int are different than before conversion?

so, as the title says, I can’t convert numbers from char to integer. Am I doing something wrong?

Console.Clear();
int hourBin1 = 0, hourBin2 = 0, minuteBin1 = 0, minuteBin2 = 0, secondBin1 = 0, secondBin2 = 0;
int iteration=0;
void conversion(int variable){
    int i=0;
    int[] table = new int[6];
    while(variable>0){
        table[i]=variable%2;
        Console.WriteLine($"{i} {variable} {table[i]}");
        variable/=2;
        i++;
    }
    int result = 0;
    for(int j=i-1;j>=0;j--){
        result*=10;
        result+=table[j];
    }
    if(iteration==0){
        hourBin1=result;
        iteration++;
    }else if(iteration==1){
        hourBin2=result;
        iteration++;
    }else if(iteration==2){
        minuteBin1=result;
        iteration++;
    }else if(iteration==3){
        minuteBin2=result;
        iteration++;
    }else if (iteration==4){
        secondBin1=result;
        iteration++;
    }else if (iteration==5){
        secondBin2=result;
        iteration++;
    }
}
Console.Write("enter date in HH:MM:SS format to convert: ");
string numberString = Console.ReadLine()!.Trim();
char[] separation = numberString.ToCharArray();
foreach (var x in separation){
    Console.Write($"{x} ");
}
int hour1 = Convert.ToInt16(separation[0]);
int hour2 = Convert.ToInt16(separation[1]);
int minute1 = Convert.ToInt16(separation[3]);
int minute2 = Convert.ToInt16(separation[4]);
int second1 = Convert.ToInt16(separation[6]);
int second2 = Convert.ToInt16(separation[7]);
Console.WriteLine("converting to binary system");
conversion(hour1);
conversion(hour2);
conversion(minute1);
conversion(minute2);
conversion(second1);
conversion(second2);
Console.WriteLine($"\n{hour1} {hour2} {minute1} {minute2} {second1} {second2}");
Console.WriteLine($"hourBin1}\n{hourBin2}\n{minuteBin1}\n{minuteBin2}\n{secondBin1}\n{secondBin2}");

for example:

console outputs for "12:23:34" looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1 2 : 2 3 : 3 4 49 50 50 51 51 52

I want it to look like this :

1 2 : 2 3 : 3 4 1 2 2 3 3 4

and final output looks like this:

49 50 50 51 51 52 110001 110010 110010 110011 110011 110100

I want it to look like this :

1 2 2 3 3 4 0001 0010 0010 0011 0011 0100

PS for some reason, table cant take value smallet than 6, any ideas why?

>Solution :

There are basically the equivalent ASCII values, all you can do is simply subtract 48 from each value then you will get the exact value. ASCII value of char 1 is 49.

int hour1 = Convert.ToInt16(separation[0]) - 48;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading