0

Let me describe the scenario first.

What I'm trying to do is convert a string to hex.

For example let's say a binary string comprising of 1s & 0s viz. 110011, pairing them as a group of 4 digits(appending two 0s for 8 digits here), converting each pair into hex separately and then joining the hex string result to obtain the output.

For octal, same as was for binary but here input octal string is split into groups of 3 digits

For ASCII, each digit's byte equivalent is converted to hex and stored.

Now the problem is what should I do for decimal string input?

-Should I consider using the same method as that for ASCII? -Or is there another way?

EDIT :-

I'm not just converting numbers, but converting an array of numbers.

Binary string - groups of 4 digits & then convert them to hex
Octal string - groups of 3 digits & then convert them to hex
ASCII string - byte equivalent for each character & then convert that to hex

So length isn't the issue. The issue is how to convert the decimal string(what sort of pairing/grouping should I use)

NOTE : I already know about converting octal, binary & decimal numbers to hex. This part is more about how to "divide the decimal string into groups" so as to convert each decimal group into hex separately and then joining the resultant hex.

  • 1
    You mean like this for instance? [Converting long string of binary to hex c#](http://stackoverflow.com/q/5612306/3744182). – dbc Dec 20 '16 at 05:35
  • Actually that's for binary string. As I mentioned above, the problem is to convert the decimal string to hex – GameMaster Greatee Dec 20 '16 at 05:37
  • So then like this? [How to convert numbers between hexadecimal and decimal in C#?](http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c?rq=1) – Abion47 Dec 20 '16 at 05:38
  • OK, then see [BigInteger to Hex/Decimal/Octal/Binary strings?](http://stackoverflow.com/q/14048476/3744182). You can parse to a `BigInteger` then output as Hex. – dbc Dec 20 '16 at 05:39
  • @dbc I'd stick with `int` or `long` unless OP for some reason needs numbers bigger than +/- 9,223,372,036,854,775,807. – Abion47 Dec 20 '16 at 05:40
  • Sorry mates but I'm not converting numbers. So length isn't the issue. I'm converting "strings" of binary, octal, ascii & decimal into hex "string". The issue is how to convert the decimal string(what sort of pairing should I use). – GameMaster Greatee Dec 20 '16 at 05:43
  • Well then if only there were a way to parse a string into an int or long. – Abion47 Dec 20 '16 at 05:46
  • @GameMasterGreatee - right, that's why I suggested using [`BigInteger`](https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx). It represents *an arbitrarily large signed integer* and so can be used for arithmetic (including base conversions) on numeric strings of any length. – dbc Dec 20 '16 at 05:47
  • @dbc Actually the program I'm developing has two modes - NumberMode & StringMode. What you're suggesting has already been implemented in the NumberMode. Problem is what to do for the StringMode. :-) – GameMaster Greatee Dec 20 '16 at 05:54
  • You parse the string into a number and go from there. This doesn't need to be a big ordeal. – Abion47 Dec 20 '16 at 05:59
  • @Abion47 I already said that what you r saying is already done in the NumberMode. Any ideas how to treat the decimal string for the StringMode other than that already done in the NumberMode. I'm out of ideas that's why I'm asking. I did some edits in the question. Please see if it helps you understand what I'm trying to accomplish. – GameMaster Greatee Dec 20 '16 at 06:13
  • If what you are doing is different, then give us a sample of your input data followed by what you would like the output data to be. Otherwise, what it sounds like is this is precisely the same as working with "NumberMode" with merely the added step of parsing the string into a numerical type. – Abion47 Dec 20 '16 at 06:15
  • Hehe. The problem is I have the input but I want the output to be different to that of the output obtained in NumberMode. Example for Binary - NumberMode: Convert the entire binary string into hex, StringMode: Split the binary into groups of 4 digits, convert each into hex and String.Join all the hex outputs – GameMaster Greatee Dec 20 '16 at 06:22
  • @GameMasterGreatee - I'm pretty sure you'd get the exact same result doing that. `0b11001001` -> `C9` or `0b11001001` -> `1100,1001` -> `C,9` -> `C9`. Same result. – Bobson Dec 20 '16 at 06:36
  • @Bobson yeah right. Problem was what to do with the decimal string – GameMaster Greatee Dec 20 '16 at 06:42
  • This thread is strange by Your wish. There is nothing like "hex string", "octal string". These are forms of NUMBER, and number has his value idependent of representation (number 117 has the same value expressed hex, octal, etc). Clear conception is base of correct solution – Jacek Cz Dec 20 '16 at 06:55
  • @dbc thanks for BigInteger link – GameMaster Greatee Dec 20 '16 at 06:57
  • @JacekCz I just needed an idea so as to how to perform the conversion for the case of decimal strings. Anything new could've worked but not the traditionally used ones as I already mentioned in the above comments. – GameMaster Greatee Dec 20 '16 at 07:00
  • Hard to imagine correct algorithm without being number in the middle. – Jacek Cz Dec 20 '16 at 07:03
  • 1
    Alright, so your question, as it turns out, is NOT about converting a string from one number system to another, but by splitting a string into segments. In the future, you will get better answers by being clearer with your intentions up front. To split a binary/octal/decimal/hex string still involves the initial conversion like before. You can then separate the string into segments of a fixed length, like in [this answer for example](http://stackoverflow.com/questions/1450774/splitting-a-string-into-chunks-of-a-certain-size). – Abion47 Dec 20 '16 at 07:54

2 Answers2

6

There's no need to reinvent the wheel here.

string input = "123456";
string outputHex = int.Parse(input).ToString("X");

// output = "1E240"

Or even better:

string outputHex = Convert.ToString(int.Parse(input), 16);

This method lets you do other number systems as well:

string outputOct = Convert.ToString(int.Parse(input), 8);
string outputBin = Convert.ToString(int.Parse(input), 2);
Abion47
  • 22,211
  • 4
  • 65
  • 88
0

Easiest way to achieve it would be to regard the digits as ASCII characters(0-9 have ASCII values 48-57) and convert it to Hex like you are already doing.

If you want to do it in another way, you would need to introduce new logic to your program. It depends upon your personal preference.

Bishal
  • 807
  • 1
  • 5
  • 20
  • Seems like a possible solution but in that case the conversion for ascii & decimal strings would be same. – GameMaster Greatee Dec 20 '16 at 06:15
  • If you want to be able to differentiate between ints and string of int, you need to have a way to tell the difference. Probably surround a string of ints by doublequotes( – Bishal Dec 20 '16 at 06:21
  • You are designing your own encoding system. Sky is your limit. – Bishal Dec 20 '16 at 06:27
  • Actually, I ain't designing an encoding system but it's rather just a feature in my new app. The app will be hosted in github accout https://github.com/gmastergreatee – GameMaster Greatee Dec 20 '16 at 06:35