Usage

./bin2hex 166_TurnItUpPhrVox_01_627a.mp3 1

/* begin binary data: */
char bin_data[] = /* 35065 */
{0xFF,0xFB,0x90,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0x69,0x6E,0x67,0x00,0x00,0x00,0x0F,0x00
//...
,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA
,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};
/* end binary data. size = 35065 bytes */

Perl source

#!/usr/bin/perl

#
# bin2hex.pl by Chami.com
# http://www.chami.com/tips/
# # number of characters per line
$chars_per_line = 15; # ------------------------------------- # language id
#
# 0 = Perl (default)
# 1 = C / C++
# 2 = Pascal / Delphi
#
$lang = $ARGV[1]; $rem_begin = "begin binary data:";
$rem_end = "end binary data."; # initialize for Perl strings
# by default
$_var = "# $rem_begin\n".
"\$bin_data = # %d\n";
$_begin = "\"";
$_end = "\";\n";
$_break = "\".\n\"";
$_format = "\\x%02X";
$_separator = "";
$_comment = "# $rem_end ".
"size = %d bytes"; # C / C++
if(1 == $lang)
{
$_var = "/* $rem_begin */\n".
"char bin_data[] = ".
"/* %d */\n";
$_begin = "{";
$_end = "};\n";
$_break = "\n";
$_format = "0x%02X";
$_separator = ",";
$_comment = "/* $rem_end ".
"size = %d bytes */";
}
elsif(2 == $lang)
{
$_var = "{ $rem_begin }\n".
"const bin_data : ".
"array [1..%d] of ".
"byte = \n";
$_begin = "(";
$_end = ");\n";
$_break = "\n";
$_format = "\$%02X";
$_separator = ",";
$_comment = "{ $rem_end ".
"size = %d bytes }";
} if(open(F, "<".$ARGV[0]))
{
binmode(F); $s = '';
$i = 0;
$count = 0;
$first = 1;
$s .= $_begin;
while(!eof(F))
{
if($i >= $chars_per_line)
{
$s .= $_break;
$i = 0;
}
if(!$first)
{
$s .= $_separator;
}
$s .= sprintf(
$_format, ord(getc(F)));
++$i;
++$count;
$first = 0;
}
$s .= $_end;
$s .= sprintf $_comment, $count;
$s .= "\n\n"; $s = "\n".sprintf($_var, $count).$s; print $s; close( F );
}
else
{
print
"bin2hex.pl by Chami.com\n".
"\n".
"usage:\n".
" perl bin2hex.pl <binary file>".
" <language id>\n".
"\n".
" <binary file> : path to the ".
"binary file\n".
" <language id> : 0 = Perl, ".
"1 = C/C++/Java, ".
"2 = Pascal/Delphi\n".
"\n";
}

Perl Script to convert binary to hex的更多相关文章

  1. [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  2. Convert Binary Search Tree to Doubly Linked List

    Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...

  3. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

    原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...

  4. 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

    题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...

  5. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  6. Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

    (http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...

  7. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  8. [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  9. LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List

    题目 Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right point ...

  10. [Algorithm] 1290. Convert Binary Number in a Linked List to Integer

    Given head which is a reference node to a singly-linked list. The value of each node in the linked l ...

随机推荐

  1. Verilog仿真实践

    Verilog必须掌握 逻辑仿真工具(VCS)和逻辑综合工具(DC) AndOR module AndOr( output X,Y, input A,B,C ); // A B进行按位与运算 assi ...

  2. [转帖]清除掉shared pool中某条sql语句方法

    https://www.xifenfei.com/2012/02/%E6%B8%85%E9%99%A4%E6%8E%89shared-pool%E4%B8%AD%E6%9F%90%E6%9D%A1sq ...

  3. [转帖]ORACLE新参数MAX_IDLE_TIME和MAX_IDLE_BLOCKING_TIME简介

    https://www.cnblogs.com/kerrycode/p/16856171.html Oracle 12.2 引入了新参数MAX_IDLE_TIME.它可以指定会话空闲的最大分钟数.如果 ...

  4. [转帖]Java 8 要过时了?从JDK8飞升到JDK17,一次性给你讲明白

    https://blog.csdn.net/agonie201218/article/details/127916729?spm=1001.2101.3001.6650.2&utm_mediu ...

  5. [转帖]TiFlash 源码阅读(一) TiFlash 存储层概览

    https://cloud.tencent.com/developer/article/1988629 背景 本系列会聚焦在 TiFlash 自身,读者需要有一些对 TiDB 基本的知识.可以通过这三 ...

  6. [转帖]如何升级vCenter(VCSA)补丁?

    https://www.dinghui.org/upgrade-vcsa-patch.html VCSA的升级分为repo源和iso镜像两种方式,由于使用repo源进行升级只支持HTTPS和FTPS协 ...

  7. nginx日志定期备份清理的方法

    nginx日志定期备份清理的方法 前言 实在不想动不动就 yum install 也不太想因为一个很小的需求就搞一下ansible. 想着能够尽量简单, 尽量方便的进行一些工作. 具体思路就是 压缩, ...

  8. [转帖]Cat导致内存不足原因分析

    背景 线上几亿的数据在回刷的时候容器服务会出现OOM而重启,导致任务中断 内存泄露分析 jmap -histo pid 找出了有几十亿的java.lang.StackTraceElement对象,找不 ...

  9. 一个简单的监控java进程获取日志的办法

    公司里面一个长时间运行的环境会出现问题, 这边简单写了一个脚本自动获取日志信息 脚本如下 注意 我的path 其实就是复用的 我们应用里面的jdk  剩下的就非常简单了. 每个日志都自动打包 并且移除 ...

  10. 让你轻松看懂defer和async

    defer和async产生的原因 HTML 网页中,浏览器通过<script>标签加载 JavaScript 脚本. <!-- 页面内嵌的脚本 --> <script t ...