联想Ideapad310S-14ISK可以玩冒险岛哪个职业好玩2么,对电脑有影响么

求助,字符串按位(双字节16进制)异或(XOR)怎么解决?_vb吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:96,987贴子:
求助,字符串按位(双字节16进制)异或(XOR)怎么解决?收藏
现在想实现这样一个功能,在TEXT1,text2中各输入一任意长度字符串(16进制双字节)现在想求出2个字符串,每一位依次异或的结果,以一个双字节16进制数存在TEXT3中。比如上图中要求出
82 Xor fb Xor 20 Xor 50 Xor 01 Xor 01 Xor 20 Xor dc Xor 4b结果返回给TEXT3。*******************************************************************************有高手能给个代码吗?*******************************************************************************我初步设想是这样的:&&
s1=text1.text&&
s2=text2.text&&
s=s1+s2&&& sa=replace(s," ","")&&
sb(len(sb))=split(s," ")&&
for i=0 to len(sb)-1&&&&& Val(sb(i))= 0 Xor Val(sb(i))&&
text3=hex(val(sb(i)) 具体代码我就不知道怎么写,比如上面的Len(s)就不对的。。好像len计算长度时按单字节计算的。。。。要的长度不是我要的实际字符串长度(双字节)...有人能给个完整的代码吗?不胜感激
票牛教你如何买到热门、便宜、真实的演出门票!
sb(len(sa))=split(s," ")..呵呵,乱七八糟的弄错
没人愿意扶持新人啊。。。得。。自己来解决~~~~~~
********************************************************************************Private Sub Command1_Click() Dim s1 As String Dim s2 As String, i As Integer Dim sok As String Dim sa As String Dim sb As String Dim sc() As String Dim sd As Byte s1 = Text1.Text&& '文本框1内容 s2 = Text2.Text&& '文本框2内容 sa = Trim(Trim(s1) + " " + Trim(s2)) '文本框1内容+文本框2内容(去头尾空格)(之间加空格--作数组分隔符) 'sb = Replace(sa, " ", "") sc = Split(sa, " ") '将文本框1,2里的字符串拆成数组 For i = 1 To UBound(sc) If Len(sc(i)) && 2 Then MsgBox "输入的数据不对!超过2位": Exit Sub '判断拆出的数组值是否为2位(免溢出) Next i sd = Val("&h" & sc(0)) '参与异或的第一个值
For i = 1 To UBound(sc) '循环异或&& sd = sd Xor Val("&h" & sc(i))&& sok = Hex(sd)&& If Len(sok) = 1 Then&&& sok = "0" + Hex(sd)&&& Else&&& sok = Hex(sd)&&& End If && Next i&& 'sok = Hex(sd)&& Text3 = sok End Sub*******************************************************************************有需要的可以拿去玩下。。呵呵。。程序虽然很简单。。对于新人总是那么难。。哎
初涉VB,给我感觉最深的就是数据类型之间的转换关系。。。。。。
回复:3楼有个小BUG~ **************************************************************For i = 1 To UBound(sc)If Len(sc(i)) && 2 Then MsgBox "输入的数据不对!超过2位": Exit Sub '判断拆出的数组值是否为2位(免溢出)Next i**************************************************************应该是For i= 0 To UBound(sc)**************************************************************
我也是新人,刚刚学VB,刚好要用到这段代码,找了好久终于找到了,太谢谢你了~~~~~~
不错哦,正是想要的功能!
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或查看: 11612|回复: 8
[求助]两个二进制数异或计算
阅读权限70
在线时间 小时
各位:求教:能简单易行算出两个二进制数异或值的方法。谢谢!&&
(1.48 KB, 下载次数: 406)
18:10 上传
点击文件名下载附件
[求助]两个二进制数异或计算
阅读权限100
在线时间 小时
是这样:B3=TEXT(SUM(ABS(MID(B1,{1,2,3,4,5,6,7,8},1)-MID(B2,{1,2,3,4,5,6,7,8},1))*10^{7,6,5,4,3,2,1,0}),"")
阅读权限100
在线时间 小时
& & & & & & & &
二楼编写得过烦了:C2=TEXT(--SUBSTITUTE(B1+B2,2,0),"")
阅读权限50
在线时间 小时
你的第二个方法真妙
阅读权限70
在线时间 小时
感谢各位!请问wddn:“--SUBSTITUTE”前面为何要多出“--”?去掉它会影响结果吗?
阅读权限100
在线时间 小时
以下是引用gfs57在 21:32:24的发言:感谢各位!请问wddn:“--SUBSTITUTE”前面为何要多出“--”?去掉它会影响结果吗?去掉“--”应该不会影响结果。
阅读权限70
在线时间 小时
那“--”到底表示什么呢?请明示。
阅读权限50
在线时间 小时
& & & & & & & &
以下是引用gfs57在 22:35:58的发言:那“--”到底表示什么呢?请明示。表示将文字型数字转换为数值型数字。
阅读权限70
在线时间 小时
谢谢 huanbei 的指点!
玩命加载中,请稍候
玩命加载中,请稍候
Powered by
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任! & & 本站特聘法律顾问:徐怀玉律师 李志群律师XORing two hex strings in Python - which method is correct? [异或:两个十六进制字符串的方法是正确的?] - 问题-字节技术
XORing two hex strings in Python - which method is correct?
异或:两个十六进制字符串的方法是正确的?
问题 (Question)
I've been struggling to find a way to XOR two hex numbers stored in strings correctly for a couple of days now, and I have come across two methods, both of which make sense to me but produce different results. I am not that versed in Python (as in, I have like 3 days of experience :D), so I cannot figure out which method is correct.
s1 = #hex number stored in a string 1
s2 = #hex number stored in a string 2
#Decoding the hex strings into ASCII symbols
s3 = s1.decode('hex')
s4 = s2.decode('hex')
#strxor - see the next code segment for the code of this function
xor1 = strxor(s3, s4)
#Encode the result back into ASCII
xor2 = xor1.encode('hex')
strxor function:
#This was given in my assignment and I am not entirely sure what is going on in
#here. I've been told that it takes two ASCII strings as input, converts them to
#numbers, XORs the numbers and converts the result back to ASCII again.
def strxor(a, b):
if len(a) & len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
s1 = #ciphertext 1 - hex number in a string
s2 = #ciphertext 2 - hex number in a string
#convert the string to integers, xor them
#and convert back to hex
xor = hex(int(s1, 16) ^ int(s2, 16))
As I said before, to my limited brain these two solutions seem identical, yet they produce completely different results. What is the problem? I have both Python 2.7.3 and 3.3.2 on my system, and I've tried both (although not for method 1, as python 3 no longer has the decode function for strings)
我一直在挣扎着异或两个十六进制数字的字符串存储在正确的好几天了,我遇到了两个方法,它们都使我的意识而产生不同的结果。我不精通Python(如,我有3天的经验:D),所以我不知道哪种方法是正确的。方法1:s1 = #hex number stored in a string 1
s2 = #hex number stored in a string 2
#Decoding the hex strings into ASCII symbols
s3 = s1.decode('hex')
s4 = s2.decode('hex')
#strxor - see the next code segment for the code of this function
xor1 = strxor(s3, s4)
#Encode the result back into ASCII
xor2 = xor1.encode('hex')
strxor功能:#This was given in my assignment and I am not entirely sure what is going on in
#here. I've been told that it takes two ASCII strings as input, converts them to
#numbers, XORs the numbers and converts the result back to ASCII again.
def strxor(a, b):
if len(a) & len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
方法2:s1 = #ciphertext 1 - hex number in a string
s2 = #ciphertext 2 - hex number in a string
#convert the string to integers, xor them
#and convert back to hex
xor = hex(int(s1, 16) ^ int(s2, 16))
就像我之前说的,我有限的大脑的这两种解决方案都是相同的,但他们产生完全不同的结果。问题是什么?我有两个Python 2.7.3和3.3.2在我的系统,我都试了(虽然没有方法1,Python 3不再具有解码函数的字符串)
最佳答案 (Best Answer)
your_string.encode('hex') will replace each character of your_string with its ASCII value using hexadecimal.
For instance, knowing that the 'A' letter is 0x41 in ASCII:
&&& 'AAAA'.encode('hex')
and you can do the other way using decode:
&&& ''.decode('hex')
but that's not what you really want. What you want is 0x12 to be converted into 18 (16 + 2). And to do that, the correct way is to use int(your_string, 16) to interpret your_string as a number encoded in base 16.
So, the correct solution is the last one.
xor = hex(int(s1, 16) ^ int(s2, 16))
s1 and s2 are strings containing the hexadecimal representation of your numbers, you decode them into int telling Python it's base 16. Then you do the xor and finally you convert it back into a string using the hexadecimal representation (with hex).
your_string.encode('hex')将每个字符your_string使用十六进制的ASCII码值。例如,知道“信码0x41:&&& 'AAAA'.encode('hex')
你可以做其他的方式使用decode:&&& ''.decode('hex')
但那不是你真正想要的是什么。你想要的是0x12被转换成18(16 + 2)。为了做到这一点,正确的方法是使用int(your_string, 16)解释your_string作为一个基地16编码数。所以,正确的解决方案是最后一个。xor = hex(int(s1, 16) ^ int(s2, 16))
s1和s2含有你的数字的十六进制表示的字符串,你解码成intPython是基地16讲。然后你做异或最后你把它转换回使用十六进制表示一个字符串(带hex)。
答案 (Answer) 2
The immediate issue with the first method is that you're applying strxor to s1 and s2:
xor1 = strxor(s1, s2)
whereas you probably meant s3 and s4:
xor1 = strxor(s3, s4)
With this change, I get identical results from both methods (on a simple test case).
与第一种方法的迫切的问题是,你的申请strxor对s1和s2:xor1 = strxor(s1, s2)
而你可能意味着s3和s4:xor1 = strxor(s3, s4)
这一变化,我从这两种方法相同的结果(在一个简单的测试用例)。
答案 (Answer) 3
...these two solutions seem identical, yet they produce completely different results. What is the problem?
To me the results are the same:
def strxor(a, b):
len_ = min(len(a), len(b))
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len_], b[:len_])])
def work(s1, s2):
#strxor - see the next code segment for the code of this function
xor1 = strxor(s1.decode('hex'), s2.decode('hex')).encode('hex')
#convert the string to integers, xor them
#and convert back to hex
xor2 = hex(int(s1, 16) ^ int(s2, 16))[2:]
print xor1
print xor2
work('A0', '0A')
work('A0', 'A0')
work('00', 'AA')
work('00A0', 'A000')
……这两种解决方案都是相同的,但他们产生完全不同的结果。问题是什么?我的结果都是一样的:def strxor(a, b):
len_ = min(len(a), len(b))
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len_], b[:len_])])
def work(s1, s2):
#strxor - see the next code segment for the code of this function
xor1 = strxor(s1.decode('hex'), s2.decode('hex')).encode('hex')
#convert the string to integers, xor them
#and convert back to hex
xor2 = hex(int(s1, 16) ^ int(s2, 16))[2:]
print xor1
print xor2
work('A0', '0A')
work('A0', 'A0')
work('00', 'AA')
work('00A0', 'A000')
给出了:aa
答案 (Answer) 4
It would help if you define test cases that you know the answers to. For example:
0x0f0f0f ^ 0xf0f0f0
-& 0xffffff
0x101010 ^ 0x000000
-& 0x101010
and so on. Your "Method 2" is valid and correct and is legal in Python 2 and 3 (but you should make sure your test cases confirm this).
The function strxor is defective as your tests will show. It takes two input strings converts the corresponding character in each string to its ordinal representation, XORs them together, converts that back to ASCII with chr and joins the whole mess together again. One test case is needed to show that it might work fine for decimal digits but bombs on mixed case hexadecimal:
strxor('b', 'B')
should not yield #.
Method 2 is the cleanest by far, using str.decode could be considered codec abuse when hex and int are already there. The instructor might have been more interested in , but there are far better examples that could have been chosen.
如果你定义的测试用例,你知道答案的帮助。例如:0x0f0f0f ^ 0xf0f0f0
-& 0xffffff
0x101010 ^ 0x000000
-& 0x101010
等。你的“法2”是有效的和正确的,法律是Python 2和3(但是你要确保你的测试案例确认)。功能strxor你的测试结果是有缺陷的。需要两个输入字符串转换为相应的字符在每个字符串其ord脊柱表示,异或在一起,将返回ASCII与chr加入全乱了起来。一个测试案例来说明它可能对小数位数但混合的情况下,十六进制弹好:strxor('b', 'B')
不要屈服#。方法2是最干净的到目前为止,使用str.decode可以考虑编解码时滥用hex和int已经存在。老师可能会一直更感兴趣但是,有更好的例子,可能被选择。
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:2014年10月 C/C++大版内专家分月排行榜第三2014年4月 C/C++大版内专家分月排行榜第三
2014年10月 C/C++大版内专家分月排行榜第三2014年4月 C/C++大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。2008年9月 .NET技术大版内专家分月排行榜第三
2013年 总版技术专家分年内排行榜第一
2014年 总版技术专家分年内排行榜第三
2008年8月 .NET技术大版内专家分月排行榜第一
2008年6月 .NET技术大版内专家分月排行榜第二
2004年4月 硬件使用大版内专家分月排行榜第二
2009年8月 .NET技术大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 冒险岛什么职业好玩 的文章

 

随机推荐