最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSONata base64 to hex - Stack Overflow

programmeradmin4浏览0评论

I have UUIDs stored in DynamoDB. In the ddb table, they are stored as binary ('B' AttributeValues) 32 byte values. When queried with GetItem, this data is returned to the client as a base64 encoded string.

I need to take that and get the textual representation (e.g. e1ddb863-48a2-4f97-a0c0-55c13bb999dc) in an AWS step function using JSONata.

I have toyed with this quite a bit and I don't see a way to do it. I base64decode the value and split on "" to get an array of characters, but I don't see any way to coerce JSONata into recognizing what I'd consider a 'byte' datatype. JSONata seems to insist that a base64decoded bytes MUST be a string. (Interestingly, the documentation for base64decode DOES refer to "base 64 encoded bytes"; I just can't find a way to iterate over those bytes and convert each to its hex representation.)

Is there a secret I'm missing?

I have UUIDs stored in DynamoDB. In the ddb table, they are stored as binary ('B' AttributeValues) 32 byte values. When queried with GetItem, this data is returned to the client as a base64 encoded string.

I need to take that and get the textual representation (e.g. e1ddb863-48a2-4f97-a0c0-55c13bb999dc) in an AWS step function using JSONata.

I have toyed with this quite a bit and I don't see a way to do it. I base64decode the value and split on "" to get an array of characters, but I don't see any way to coerce JSONata into recognizing what I'd consider a 'byte' datatype. JSONata seems to insist that a base64decoded bytes MUST be a string. (Interestingly, the documentation for base64decode DOES refer to "base 64 encoded bytes"; I just can't find a way to iterate over those bytes and convert each to its hex representation.)

Is there a secret I'm missing?

Share Improve this question asked Mar 31 at 3:32 NevoNevo 9583 gold badges12 silver badges31 bronze badges 3
  • 1 you fail to show the code. uuid is just a 128 bit number (in your case 4x32 I guess). turning the numbers into an array of bytes which you encode into a hex string (base64 is something completely different). – mr mcwolf Commented Mar 31 at 4:01
  • You fail to understand what I wrote. I query an item from DynamoDB. One of the attributes in the DynamoDB item is stored as a 'binary' datatype in the database and is returned to the client as a base64 encoded string. In order for me to display this base64 encoded 32 byte string as hex, I would like to iterate over each byte and display the byte in hex format. – Nevo Commented Apr 1 at 12:54
  • and what is the problem? base64 decode will make the string into a byte array. you represent this array as a hex string or as a regular string (nobody knows what is actually encoded, and you are obviously keeping this a secret from us) – mr mcwolf Commented Apr 1 at 13:00
Add a comment  | 

1 Answer 1

Reset to default 0

It seems to me that you have a problem understanding how PHP handles the missing byte data type. Due to the lack of a byte type in PHP, all binary data is represented as strings! Think of them as C-style char* arrays.

This conventionally introduces two types of strings: simple and binary string. Simple strings, in turn, can also be divided into two types: simple and hex string. Where a hex string is a text representation of a hexadecimal value.

PHP has several functions for converting data into different "formats". Simple bin2hex() / hex2bin() and more flexible pack() / unpack().

If we analyze your problem from the back to the front, you have input data represented as a simple string e1ddb863-48a2-4f97-a0c0-55c13bb999dc. To convert this string to a hex string we need to remove the - characters which do not represent a valid hexadecimal value. Next, the hex string must be converted to a binary string with hex2bin() / pack(). And finally, we need to encode the resulting binary string in Base64 so that it can be safely transmitted over the network.

<?php
    $str = 'e1ddb863-48a2-4f97-a0c0-55c13bb999dc';
    $hex_string = str_replace('-', '', $str);
    $binary_string = pack('H*', $hex_string);

    echo base64_encode($binary_string); //4d24Y0iiT5egwFXBO7mZ3A==

Your question is about the opposite - you have Base64 encoded data 4d24Y0iiT5egwFXBO7mZ3A==. First they need to be decoded into a binary string. The resulting binary string needs to be represented as a hex string with bin2hex() / unpack(). Then the hex string needs to be represented as a simple string (adding the - symbol at the appropriate positions).

<?php
    $str = '4d24Y0iiT5egwFXBO7mZ3A==';
    $binary_string = base64_decode($str);
    $hex_string_array = unpack('H8c1/H4c2/H4c3/H4c4/H12c5', $binary_string);

    echo join('-', $hex_string_array); //e1ddb863-48a2-4f97-a0c0-55c13bb999dc

Here I use unpack() whose formatting parameter allows me to create an array of fixed-length hex strings (8-4-4-4-12) which I use directly to create the simple string representing the UUID.

发布评论

评论列表(0)

  1. 暂无评论