ddwcyl
===========================================================
Java基本数据类型转换
===========================================================

1 字符串转换成数据
字符串转换成整数:

String MyNumber ="1234";
int MyInt = Integer.parseInt(MyNumber);
字符串转换成byte, short, int, float, double, long等数据类型,可以分别参考Byte, Short, Integer, Float, Double, Long类的parseXXX方法。

摘自伟网动力


 

1 字符串转换成数据
字符串转换成整数:

String MyNumber ="1234";
int MyInt = Integer.parseInt(MyNumber);
字符串转换成byte, short, int, float, double, long等数据类型,可以分别参考Byte, Short, Integer, Float, Double, Long类的parseXXX方法。

2 数据转换成字符串
整数转换成字符串:

int MyInt = 1234;
String MyString = "" + MyInt;
其它数据类型可以利用同样的方法转换成字符串。

3 十进制到其他进制的转换
十进制整数转换成二进制整数,返回结果是一个字符串:

Integer.toBinaryString(int i);
Integer
Long提供了toBinaryString, toHexStringtoOctalString方法,可以方便的将数据转换成二进制、十六进制和八进制字符串。功能更加强大的是其toString(int/long i, int radix)方法,可以将一个十进制数转换成任意进制的字符串形式。

byte, short, float
double等数据类型,可以利用Integer或者是LongtoBinaryString, toHexString, to OctalStringtoString方法转换成其他进制的字符串形式。

4 其它进制到十进制的转换
五进制字符串14414转换成十进制整数,结果是1234

System.out.println(Integer.valueOf("14414", 5);
Integer
Long提供的valueOf(String source, int radix)方法,可以将任意进制的字符串转换成十进制数据。

5 整数到字节数组的转换
public static byte[] toByteArray(int number)
{
int temp = number;
byte[] b=new byte[4];
for (int i = b.length - 1; i > -1; i--)
{
b[i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
}

6 字节数组到整数的转换
public static int toInteger(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[i] > 0)
s = s + b[i];
else
s = s + 256 + b[i];
s = s * 256;
}
if (b[3] > 0)
s = s + b[3];
else
s = s + 256 + b[3];
return s;
}

7 短整数与字节数组之间的相互转换
short
int之间的区别在于short是两个字节的,而int是四个字节的。因此,只需要将5 6 中的范例程序小做改动,即可实现短整数与字节数组之间的相互转换。

8 字节数组转换成双精度浮点数
public double toDouble(byte[] b)
{
long l = 0;
Double D = new Double(0.0);
l = b[0];
l |= ((long)b[1]<<8);
l |= ((long)b[2]<<16);
l |= ((long)b[3]<<24);
l |= ((long)b[4]<<32);
l |= ((long)b[5]<<40);
l |= ((long)b[6]<<48);
l |= ((long)b[7]<<56);
return D.longBitsToDouble(l);
}

9 布尔类型转换成字符串
第一种方法是:

boolean bool = true;
String s = new Boolean(bool).toString();//
bool利用对象封装器转化为对象
s.equals("true");
/*
其中,toString方法是一个继承方法。java中所有的类都是object的继承,object的一个重要方法就是toString,用于将对象转化为字符串。*/

第二种方法是:

boolean bool = true;
String s = String.valueOf( bool );

首先,从代码长度上讲第二种方法明显要比第一种方法简洁;其次,第一种方法在转化过程中多引入了一个完全没有必要的对象,因此,相对第二种方法来说这就造成了内存空间的浪费,大大减慢了运行速度。所以,推荐使用第二种方法。

10 数字类型与数字类对象之间的转换
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();

short t = 169;
Short to = new Short( t );
t = to.shortValue();

int i = 169;
Integer io = new Integer( i );
i = io.intValue();

long l = 169;
Long lo = new Long( l );
l = lo.longValue();

float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();

double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue
();
ddwcyl 发表于:2005.02.03 14:52 ::分类: ( 软件开发 ) ::阅读:(57809次) :: 评论 (65)
re: Java基本数据类型转换 [回复]

I like this!

Wyle 评论于: 2007.04.18 22:10
re: Java基本数据类型转换 [回复]

整数到字节数组的转换算法有误,if (b[i] > 0) 应为 >=
同理if (b[3] > 0)也应为 >=

pkliang 评论于: 2007.11.14 04:27
re: Java基本数据类型转换 [回复]

Joe 评论于: 2007.11.23 05:20
re: Java基本数据类型转换 [回复]

Kir 评论于: 2007.11.23 07:16
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Arnie 评论于: 2007.11.23 08:54
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.23 12:08
re: Java基本数据类型转换 [回复]

Kir 评论于: 2007.11.23 13:48
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Hero 评论于: 2007.11.23 17:05
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Bill 评论于: 2007.11.24 00:17
re: Java基本数据类型转换 [回复]

Dominic 评论于: 2007.11.24 02:14
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.24 04:13
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Dominic 评论于: 2007.11.24 06:09
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Still 评论于: 2007.11.24 07:59
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Still 评论于: 2007.11.24 08:00
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Still 评论于: 2007.11.24 08:00
re: Java基本数据类型转换 [回复]

Halo 评论于: 2007.11.24 11:26
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Still 评论于: 2007.11.24 13:18
re: Java基本数据类型转换 [回复]

Diesel 评论于: 2007.11.24 16:59
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Neo 评论于: 2007.11.24 20:44
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Joe 评论于: 2007.11.24 22:41
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.25 00:31
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Halo 评论于: 2007.11.25 04:46
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Kir 评论于: 2007.11.25 07:28
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Kir 评论于: 2007.11.25 07:28
re: Java基本数据类型转换 [回复]

Neo 评论于: 2007.11.25 18:59
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.25 21:03
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.25 21:03
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.25 21:04
re: Java基本数据类型转换 [回复]

Arnie 评论于: 2007.11.26 04:19
re: Java基本数据类型转换 [回复]

Heel 评论于: 2007.11.26 06:51
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Joe 评论于: 2007.11.26 09:05
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Dominic 评论于: 2007.11.26 11:33
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Dominic 评论于: 2007.11.26 11:33
re: Java基本数据类型转换 [回复]

Still 评论于: 2007.11.26 14:03
re: Java基本数据类型转换 [回复]

Arnie 评论于: 2007.11.26 16:29
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Neo 评论于: 2007.11.26 18:49
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Heel 评论于: 2007.11.26 21:11
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Heel 评论于: 2007.11.26 21:11
re: Java基本数据类型转换 [回复]

Joe 评论于: 2007.11.26 23:19
re: Java基本数据类型转换 [回复]

Aron 评论于: 2007.11.27 01:51
re: Java基本数据类型转换 [回复]

Aron 评论于: 2007.11.27 01:51
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Dominic 评论于: 2007.11.27 04:31
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Kir 评论于: 2007.11.27 07:06
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Kir 评论于: 2007.11.27 07:07
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Kir 评论于: 2007.11.27 07:07
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Dominic 评论于: 2007.11.28 23:43
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Halo 评论于: 2007.11.29 01:51
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.29 06:17
re: Java基本数据类型转换 [回复]

Joe 评论于: 2007.11.29 12:47
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Heel 评论于: 2007.11.29 16:52
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Aron 评论于: 2007.11.29 19:04
re: Java基本数据类型转换 [回复]

Hero 评论于: 2007.11.29 21:04
re: Java基本数据类型转换 [回复]

Jane 评论于: 2007.11.29 22:58
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Diesel 评论于: 2007.11.30 01:00
re: Java基本数据类型转换 [回复]


[URL=][/URL]

Kir 评论于: 2007.11.30 03:14
秧桁 [回复]

Vodsfeellew 评论于: 2008.08.02 11:17
Cool funny anecdote:) [回复]

There was this guy see.
He wasn't very bright and he reached his adult life without ever having learned "the facts".
Somehow, it gets to be his wedding day.
While he is walking down the isle, his father tugs his sleeve and says,

"Son, when you get to the hotel room...Call me"

Hours later he gets to the hotel room with his beautiful blushing bride and he calls his father,

"Dad, we are the hotel, what do I do?"

"O.K. Son, listen up, take off your clothes and get in the bed, then she should take off her clothes and get in the bed, if not help her. Then either way, ah, call me"

A few moments later...

"Dad we took off our clothes and we are in the bed, what do I do?"

O.K. Son, listen up. Move real close to her and she should move real close to you, and then... Ah, call me."

A few moments later...

"DAD! WE TOOK OFF OUR CLOTHES, GOT IN THE BED AND MOVED REAL CLOSE, WHAT DO I DO???"

"O.K. Son, Listen up, this is the most important part. Stick the long part of your body into the place where she goes to the bathroom."

A few moments later...

"Dad, I've got my foot in the toilet, what do I do?"

PypeVenty 评论于: 2008.10.24 01:05
Nice informative community. [回复]

Just wanted to say hello to all to a wonderfull community. Very informative great work.

looking forward to contribute to the community.

-------------------------------------------------------------------------
[url=http://www.pregnancychildcare.info]Pregnancy Week By Week[/url]

Haltytara 评论于: 2008.11.14 14:01
DDDepressionnn emo emo emo [回复]

Depression Depression Depression aaaaaaaa
HEEEEELP angry.gif angry.gif angry.gif
I hate winter! I want summer!

DDDDepressionnnn 评论于: 2008.11.21 13:29
car insurance cheap uk [回复]

car insurance chicago
car insurance quote online nova scotia
car insurance for 1 day
car insurance category uk
Look at great [url=http://car-insurance-hirx.netfirms.com/fully-comprehensive-car-insurance.html]fully comprehensive car insurance[/url]

hagarty car insurance
car insurance with pass plus discount
money saving tips about car insurance
car insurance only woman
alabama car insurance
direct car insurance ireland
car insurance mobile phone
traveler car insurance
florida car insurance law
car insurance grand fork north dakota
cheap car insurance for provisional drivers
gap car insurance
car insurance perth australia
car insurance insure
hagertys classic car insurance
car insurance scam
car insurance quotes in nj
different car insurance company

foonrypup 评论于: 2008.11.22 15:01
Achtung [回复]

Hello
http://dfsg.us/
G'night

dumbuk 评论于: 2008.12.04 03:18
CrabMumeHaume [回复]

trand Arcade dispensary Online - [url=http://tagged.com/talkingcommunities]phentermine online diet pill[/url]
and Womens Heath & attraction products, and a imprecise align of Non-formula Medicines.,. Online stores online phentermine Rather,is indicated for the mezzo-rilievo 'medium ' of phentermine Side Effects. Buy phentermine is employed for treating seasonal allergy symptoms online. Low price. solicit crisis medical notice if you ruminate over you bear in use accustomed to too much of phentermine. OR-spend features dwell and on-insist on Webcasts of breakthrough medical procedures from phentermine - Online. phentermine - Online. Online Services. 2nd Annual Kids riskless Online

meesyJemWar 评论于: 2008.12.09 14:43
GARRETT DEFINE ARAMA DEDEKT諶 GTI-2500 [回复]

Bilgisayar devreleri ve microproses鰎 teknikleri sayesinde, d黱yada 黵etilen diger dedekt鰎lere g鰎e % 50 daha fazla derinlik ve ayirim g點黱e sahiptirler. Bu oran defalarca 鏾gu profesyonel defineciler tarafindan denenmis ve onaylanmistir. 7,2 kHzlik yeni 9,5

Peapledipneed 评论于: 2008.12.30 09:45
沐礤疣蝾 [回复]

祟赅朦睇

Soft-office 评论于: 2009.01.08 06:58
Make $3000/ Month with GPT Sites [回复]

This Thread will focus on how you can make money online.
Cash Crate is a rather large and popular get paid to complete offers and survey site that pays its members to participate in contests, complete surveys, and refer members.
When you join Cash Crate, you are able to complete surveys and earn real cash, which is good for people looking for work online....or better yet, to make a living!!!

I make $1600-$2200 A Month with a few GPT Sites, but there's lots of people making $2000+ per month with JUST CASHCRATE!!
Cash Create was started in early 2006 as a way to help people earn cash. There is no doubt that this is can be ranked within the top five paying GPT/SURVEY sites on the Internet.

Quick Look:
How to Get Paid - Complete offers, surveys, and participate in contests to win cash.
Payment Method - Check
When Paid - Once per month (the 15th to 20th) when you have reached $20.
There are nearly 160 offers available ( more added every week or so ) for competion, ranging in value between $0.15 to $125, with the average offer valued at a few dollars. As stated elsewhere, you can rearrange them according to the type of offer you want to complete. Many of them are free, however the higher paying offers often require a small shipping fee or credit card to properly submit the article.

How to Earn
The site pays based on the number of offers and surveys you complete, at varying levels of payouts.
You may also use the "Cash Shopping" page to receive cash back for shopping at various online stores. It's quite simple when you realize the true potential that sites like Cash Crate have.

Referral Program
The referral program is a fairly basic, two-tier program, with 20% earnings for direct referrals and 10% for the people your direct referrals refer to the site.

Once you reach a certain level of referrals (normally active, United States/ UK/ Canada members who have reached a minimum of $10 worth of offers), you are promoted to a higher level of referral payout, a huge incentive to help you promote the site.

Bronze - 0 Active Referrals - 20% First Level, 10% Second Level
Silver - 50 Active Referrals - 25% First Level, 10% Second Level
Gold - 150 Active Referrals - 25% First Level, 15% Second Level
Platinum - 300 Active Referrals - 30% First Level, 15% Second Level
Elite - 500 Active Referals - 30% First Level, 20% Second Level

THIS IS THE BEST PART:
When you hit 50 Active referrals, you get the option to be paid $1.00 for each referral who complete's their first offer!!! It's how I make $20-$25 a day!!!

Here's an example:
10 Referrals making $10 per month = $100 for Cashcrate = $20 for you
100 Referrals making $10 per month = $1000 for Cashcrate = $250 for you
1000 Referrals making $10 per month = $10,000 for Cashcrate, $3,333 for you

Now let's say they all refer 1 member:
10 Referrals refer 1 member each = 20 Referrals
- Making $10 per month = $200 for CashCrate = $40 for you
100 Referrals refer 1 member each = 200 Referrals
- Making $10 per month = $2000 for CashCrate = $500 for you
1000 Referrals refer 1 member each = 2000 Referrals
- Making $10 per month = $20,000 for CashCrate = $6,666 for you!!!

=============================================

Now that you kinda have an idea on how it works, you sign up, refer some members, do some surveys, and make money!! it's that easy!!!
I find that making a blog/website with a review, and referral links is the easiest way to go!!... Just promote your blog, and it'll kind of take off itself!!
If you decide to sign up under me, PM me with any questions, and i'll be more than happy to help you out!!... MY REFERRALS GET SPECIAL TREATMENT!!!

SIGN UP UNDER ME --> http://get.1add.net

DookEffisee 评论于: 2009.01.29 07:33

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)




切换风格
新闻聚合
博客日历
文章归档...
最新发表...
博客统计...
网站链接...