转换 ruby 等价于 java
To convert ruby unpack equivalent in java
我试图理解这行 ruby 代码:
token.unpack('m0').first.unpack('H*').first
正在转换
R1YKdH//cZKubZlA09ZIVZQ5/cxInvmokIACnl3MKJ0=
至
47560a747fff7192ae6d9940d3d648559439fdcc489ef9a89080029e5dcc289d
据我了解,这是 base64 到 hex 的转换,但是当我尝试做同样的事情时,它与转换后的不匹配。
我需要在 Java 中实现相同的功能。
所以我要分解这个。第一步是token.unpack('m0')
。根据 Idiosyncratic Ruby unpack('m0')
will decode base64, similarly to the built-in Base64
libararies Base64.decode64(string)
function. But unpack returns an arry here, with only 1 element, the converted base64. So we use token.unpack('m0').first
to get the first (and in this case the only) element of the array returned by token.unpack('m0')
. If this was all, then you'd be correct to say that it's just base64. But, the unpacked base64 is unpacked again, this time with 'H*'
,这会将字符转换为十六进制。最后,因为这将 return 一个数组,你再次使用 first 使它只是一个字符串。
所以总而言之,发生的事情是首先将您的字符串从 base64 解码为字符串,然后将其转换为十六进制。
我试图理解这行 ruby 代码:
token.unpack('m0').first.unpack('H*').first
正在转换
R1YKdH//cZKubZlA09ZIVZQ5/cxInvmokIACnl3MKJ0=
至
47560a747fff7192ae6d9940d3d648559439fdcc489ef9a89080029e5dcc289d
据我了解,这是 base64 到 hex 的转换,但是当我尝试做同样的事情时,它与转换后的不匹配。
我需要在 Java 中实现相同的功能。
所以我要分解这个。第一步是token.unpack('m0')
。根据 Idiosyncratic Ruby unpack('m0')
will decode base64, similarly to the built-in Base64
libararies Base64.decode64(string)
function. But unpack returns an arry here, with only 1 element, the converted base64. So we use token.unpack('m0').first
to get the first (and in this case the only) element of the array returned by token.unpack('m0')
. If this was all, then you'd be correct to say that it's just base64. But, the unpacked base64 is unpacked again, this time with 'H*'
,这会将字符转换为十六进制。最后,因为这将 return 一个数组,你再次使用 first 使它只是一个字符串。
所以总而言之,发生的事情是首先将您的字符串从 base64 解码为字符串,然后将其转换为十六进制。