-2

I'm writing little decoder. User enter string (hex) that program should decode.

My problem is that int value is not the same as inputed, therefore I don't know how should I advance after reading binary. Am I missing point on how to read binary ?

string input = "0802";

byte[] arr = Encoding.Default.GetBytes(input);

using (MemoryStream stream = new MemoryStream(arr))
{
    using (BinaryReader reader = new BinaryReader(stream))
    {
        int a = reader.ReadInt32();
        Console.WriteLine(a);
        //output: 842020912
    }
}
Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
SoulJam
  • 159
  • 3
  • 9
  • what value is not the same as input? – NicoRiff Jan 10 '17 at 17:58
  • 3
    You're encoding a `string`, but then try to read an `int`. They are not going to be the same. – juharr Jan 10 '17 at 17:59
  • This whole thing could just be one line of code: `int num = Convert.ToInt32("0802", 16);` – itsme86 Jan 10 '17 at 17:59
  • @NicoRiff Well I imagined that ```a``` would be same as ```input```. – SoulJam Jan 10 '17 at 18:00
  • @itsme86 I only showed one peace, there would be quite long string that should be decoded and it should be sorted. – SoulJam Jan 10 '17 at 18:02
  • @SoulJam Why? What you encode are the binary values that represent the characters of your string. That's completely different from reading an integer value from a binary stream. – juharr Jan 10 '17 at 18:02
  • Very confusing what you are trying to do. Possibly just [hex int](http://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-back-again) - but you've obviously already seen that post and likely want something different. – Alexei Levenkov Jan 10 '17 at 18:02
  • @SoulJam You need to do a better job of explaining what it is you're trying to do. For example, what constitutes a number? Is it every 2 hexdigits? From the question it looks like it should be the whole input string, but then you're talking about a longer string that contains multiple numbers. – itsme86 Jan 10 '17 at 18:05
  • @itsme86 Indeed I wasn't very clear, sorry. My main concern was to understand ```ReadInt32()``` better. Since I was confused if my outcome was correct. – SoulJam Jan 10 '17 at 19:24

2 Answers2

0

This is correct. You read 4 bytes from the string so they are interpreted as 4 bytes of your int.

If you check the hex value of your "incorrect" number 842020912 it will give you 0x32303830 and reading every byte as ASCII gives "2080".

The order is reversed as you are reading the value as little-endian.

Paweł Łukasik
  • 3,207
  • 1
  • 21
  • 31
  • I was confused what I was getting from function ```ReadInt32()```. Thanks for clarifying :) – SoulJam Jan 10 '17 at 19:21
  • Now just curious if there is some simple way of getting to that ASCII without much complicated conversion. – SoulJam Jan 10 '17 at 19:22
0

You are doing it the hard way. I using Bytes but can modify for 1in16, or int32 very easily. :

            string input = "0802";
            List<byte> bytes = new List<byte>();

            for (int i = 0; i < input.Length; i += 2)
            {
                bytes.Add(byte.Parse(input.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
            }
jdweng
  • 28,546
  • 2
  • 13
  • 18