码迷,mamicode.com
首页 > 编程语言 > 详细

C语言数据转换(hex - char - byte array - acsii)

时间:2016-10-16 01:30:26      阅读:6272      评论:0      收藏:0      [点我收藏+]

标签:

网上的整理过来的,来源记不清了,感谢作者。

DataConvert.c

#include <stdio.h>
#include <string.h>
#include "DataConvert.h"

int strToHex(char *ch, char *hex)
{
  int high,low;
  int tmp = 0;
  if(ch == NULL || hex == NULL){
    return -1;
  }

  if(strlen(ch) == 0){
    return -2;
  }

  while(*ch){
    tmp = (int)*ch;
    high = tmp >> 4;
    low = tmp & 15;
    *hex++ = valueToHexCh(high); //先写高字节
    *hex++ = valueToHexCh(low); //其次写低字节
    ch++;
  }
  *hex = \0;
  return 0;
}

int hexToStr(char *hex, char *ch)
{
  int high,low;
  int tmp = 0;
  if(hex == NULL || ch == NULL){
    return -1;
  }

  if(strlen(hex) %2 == 1){
    return -2;
  }

  while(*hex){
    high = hexCharToValue(*hex);
    if(high < 0){
      *ch = \0;
      return -3;
    }
    hex++; //指针移动到下一个字符上
    low = hexCharToValue(*hex);
    if(low < 0){
      *ch = \0;
      return -3;
    }
    tmp = (high << 4) + low;
    *ch++ = (char)tmp;
    hex++;
  }
  *ch = \0;
  return 0;
}

int hexCharToValue(const char ch){
  int result = 0;
  //获取16进制的高字节位数据
  if(ch >= 0 && ch <= 9){
    result = (int)(ch - 0);
  }
  else if(ch >= a && ch <= z){
    result = (int)(ch - a) + 10;
  }
  else if(ch >= A && ch <= Z){
    result = (int)(ch - A) + 10;
  }
  else{
    result = -1;
  }
  return result;
}

char valueToHexCh(const int value)
{
  char result = \0;
  if(value >= 0 && value <= 9){
    result = (char)(value + 48); //48为ascii编码的‘0’字符编码值
  }
  else if(value >= 10 && value <= 15){
    result = (char)(value - 10 + 65); //减去10则找出其在16进制的偏移量,65为ascii的‘A‘的字符编码值
  }
  else{
    ;
  }

  return result;
}

int hexChartoByte(char *s,char *byte)
{
    int i,n = 0;
    for(i = 0; s[i]; i += 2)
    {
        if(s[i] >= A && s[i] <= F)
            byte[n] = s[i] - A + 10;
        else byte[n] = s[i] - 0;
        if(s[i + 1] >= A && s[i + 1] <= F)
            byte[n] = (byte[n] << 4) | (s[i + 1] - A + 10);
        else byte[n] = (byte[n] << 4) | (s[i + 1] - 0);
        ++n;
    }
        return n;
}

unsigned char ChartoAscii(const unsigned char cha)
{
    unsigned char ascii;
    if ((cha >= 0x0A) && (cha <= 0x0F))
    {
        ascii = cha + A - 10;
    }
    else
    {
        ascii = cha + 0; 
    }
    return ascii;
}

 

DataConvert.h

#ifndef __DATA_H
#define __DATA_H

int strToHex(char *ch, char *hex);
int hexToStr(char *hex, char *ch);
int hexCharToValue(const char ch);
char valueToHexCh(const int value);
int hexChartoByte(char *s,char *byte);
unsigned char ChartoAscii(const unsigned char cha);
#endif

 

main.c

#include <stdio.h>
#include <string.h>
#include "DataConvert.h"

#define MCU_FIRWARE_VERSION "V1.0.0"
#define BLE_FIRWARE_VERSION "V1.0.0"
#define FONT_VERSION "V1.0.0"


int main(int argc, char *argv[])
{
    int i;
    char result[1024];
    char *p_result = result;

    //转换版本号数据
    char mcu_version_hex[12];
    char mcu_version_byte[6];
    char *p_ch = MCU_FIRWARE_VERSION;
    char *p_hex = mcu_version_hex;
    strToHex(p_ch,p_hex);
    int n = hexChartoByte(mcu_version_hex,mcu_version_byte);

    char ble_version_hex[12];
    char ble_version_byte[6];
    p_ch = BLE_FIRWARE_VERSION;
    p_hex = ble_version_hex;
    strToHex(p_ch,p_hex);
    int m = hexChartoByte(ble_version_hex,ble_version_byte);

    char font_version_hex[12];
    char font_version_byte[6];
    p_ch = FONT_VERSION;
    p_hex = font_version_hex;
    strToHex(p_ch,p_hex);
    int k = hexChartoByte(font_version_hex,font_version_byte);   


    //填充版本号数据
    for(int i = 0;i<n;i++)
        printf ("%X ",0XFF & mcu_version_byte[i]);

    for(int i = 0;i<m;i++)
        printf ("%X ",0XFF & ble_version_byte[i]);

    for(int i = 0;i<k;i++)
        printf ("%X ",0XFF & font_version_byte[i]);

    hexToStr(p_hex, p_result);
        printf("the string is:%s\n", p_result);
}

 

以上demo将字符串转换成utf8的字节流,可用utf8的转换工具还原成字符串验证。

 

------end

 

C语言数据转换(hex - char - byte array - acsii)

标签:

原文地址:http://www.cnblogs.com/dong1/p/5965645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!