ASCII to Decimal

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3870 Views - Last Post: 06 April 2011 - 08:14 AM Rate Topic: -----

#1 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

ASCII to Decimal

Posted 05 April 2011 - 06:06 PM

Hey guys,

I got a weird error.

I have a server that is sending me IP address in ascii form. I can translate the address by hand using an ascii to decimal convert (google).

My code which is simply casting the letters to ip segments doesn't convert properly. For example:

//This produces: 352
int i = (int)'Š';



but in reality when I use a by hand converter it should be 138! Why is this happen? How can I convert properly? (It's kind of hard to use 352 in an ip address... :lol: )

Thanks
Metric

Is This A Good Question/Topic? 0
  • +

Replies To: ASCII to Decimal

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 06:14 PM

What are you exactly trying to do ?
Converting "123.234.012.210" into 4 bytes containing 123, 234, 012, 210 ?
Was This Post Helpful? 1
  • +
  • -

#3 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: ASCII to Decimal

Posted 05 April 2011 - 06:18 PM

Okay lets go with my a random ip for example: 213.5.177.60

Ascii: Õ±<

So I converted each character for a portion of the IP.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 06:37 PM

Don't see what you mean. There are no relation between an IP adress and it's ascii representation

As example: www.dreamincode.net is 67.228.133.106

What ASCII conversion do you want to do ?
Was This Post Helpful? 1
  • +
  • -

#5 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: ASCII to Decimal

Posted 05 April 2011 - 06:41 PM

I'm not talking about the domain...


If i tell you the IP is encoded like: Õ .  . ± . <
If you put each character into an ascii to decimal generator you will get 213.5.177.60

So:
int a = (int)'Õ' //equal to 213
int b = (int)'' //equal to 5
int c = (int)'±' //equal to 177
int d = (int)'<' //equal to 60


but while for the most part java is correct when I do this casting, in some areas it is way off, and I don't know why.

For example: java says that int i = (int)'Š' is 352 but it is really 138. Why is it outputting the wrong number?

I hope this clarifies things a little bit.

Metric
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 07:12 PM

Kind of tricky here :)
I really don't see what you wwant to achieve but anyhow...

First it is not all the ASCII characters from 0 to 255 that are defined
There are no representation for a dozen of them

the Character class isDefined() method can be used to test if this ASCII value have a character representation or not. What will you do whith <TAB>, <LF>, <FF>, <BSP> ?

Now converting from int to byte and byte to int is tricky because byte 128 is negative and put into int will yield to FFFFFFxx

So everytinme you put a byte into an int youl have to mask the 8 last bits

int a;
byte b;
a = b & 0xFF;

playing with character can also be tricky, characters are unicode in Java: 16 bits and translation between character to byte is not always obvious

Can you give me a character (value) that does not work back and forth, I'll write a program to actually test it

This post has been edited by pbl: 05 April 2011 - 07:23 PM

Was This Post Helpful? 1
  • +
  • -

#7 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: ASCII to Decimal

Posted 05 April 2011 - 07:21 PM

hey pbl
ÎÔúŠ = 206.212.250.352 

The first three characters are correct, but the fourth outputs as 352 when it should be 138.

so:
Î = 206 //this is correct
Ô = 212 //this is correct
ú = 250 //this is correct
Š = 352 //this is incorrect and should be 138


also note this is the converter i have been using but others have confirmed for me.
http://www.stringfun...ii-decimal.html


Cheers
Metric

This post has been edited by metric: 05 April 2011 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 07:26 PM

206.212.250.352

is not a valid IP address maximum for each fields which are byte is 255 (from 0x00 to 0xFF)

Now if you translate 352 (which is larger than a byte) into an int

Putting that 352 into a byte will truncate the data and put 96 in the byte
Was This Post Helpful? 1
  • +
  • -

#9 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: ASCII to Decimal

Posted 05 April 2011 - 07:33 PM

I know that 206.212.250.352 is not a valid IP, because Š is not being decoded properly.

Š should be 138...not 352!

Bah I'm confused :(

Here is the actual usage:
I request a list from a server, it sends me this: http://pastebin.com/3MSBVa79

In there are a series of IP's that is a server list.

This post has been edited by metric: 05 April 2011 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 07:44 PM

View Postmetric, on 05 April 2011 - 09:33 PM, said:

I know that 206.212.250.352 is not a valid IP, because Š is not being decoded properly.

Š should be 138...not 352!

Bah I'm confused :(

352 is (in binary)

101100000

the largest number you can put in a byte is

11111111

converting 101100000 into a byte will yield to 1100000

sure that you cannot hope that converting the ASCII representation of 1100000 will give you back 101100000

You simply cannot convert to a character something that is > 255
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 07:51 PM

Funny cut and pasted your weird symbol and put it in code

	public static void main(String[] args)
    {
		char x = 'Š';
		int i = x;
		System.out.println(x + " is " + i);


yield
Š is 352


:) So it is extended to a unicode of some type that translate back to 138

This post has been edited by pbl: 05 April 2011 - 07:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: ASCII to Decimal

Posted 05 April 2011 - 08:02 PM

Okay so how can I correct the output without the unicode?


Also, this may help, here is the source code of a C# version. Note the methods on line 372 and 450. http://code.google.c.../Forms/Form1.cs

Cheers Metric
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 08:12 PM

You will have to store it into a char[] array

char[] representation = Character.toChars(int unicode);

assuming your int > 255 is a valid UTF16 code

Anyhow, this seems to me to be a useless discussion, the basic question is:

Why would you want to convert to ASCII representation an invalid IP address ?

All the rest would be risky unsupported character/byte/char manipulation
Was This Post Helpful? 1
  • +
  • -

#14 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: ASCII to Decimal

Posted 05 April 2011 - 08:14 PM

The UDP packet data looks like this: http://pastebin.com/3MSBVa79

Notice between the backslashes there are 6 characters, the first four are the ip and the last two are port information its a unique algorithm.

So I have to do something with that to get the IP address. Maybe you can offer an alternative?

This post has been edited by metric: 05 April 2011 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ASCII to Decimal

Posted 05 April 2011 - 08:58 PM

This is just a dump of a UDP packet. And you neeed to read it ?

Two things:
1) if it is a UDP packet sure that the IP address will be valid so no need for something > 255
2) depending of your dump utility I will be concern how it treats:
- control characters that do not have an ASCII representation: 0 to 31 these are ACK, BEll, Backspace, Line Feed, XOn, Xoff, FormFeed, LineFeed, Carriage Return, dc1, dc2, dc3, dc4, ...
- characters that are undefined: 127, 128, 129, ...

For that type of thing, you need an Hex dump not an Ascii dump
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2