Method that converts uint8_t to string
String converter(uint8_t *str){
return String((char *)str);
}
Convert array of uint8_t to string in C++
Try this:
std::ostringstream convert;
for (int a = 0; a < key_size_; a++) {
convert << (int)key[a];
}
std::string key_string = convert.str();
std::cout << key_string << std::endl;
The ostringstream
class is like a string builder. You can append values to it, and when you're done you can call it's .str()
method to get a std::string
that contains everything you put into it.
You need to cast the uint8_t
values to int
before you add them to the ostringstream
because if you don't it will treat them as chars. On the other hand, if they do represent chars, you need to remove the (int)
cast to see the actual characters.
EDIT: If your array contains 0x1F 0x1F 0x1F and you want your string to be 1F1F1F, you can use std::uppercase
and std::hex
manipulators, like this:
std::ostringstream convert;
for (int a = 0; a < key_size_; a++) {
convert << std::uppercase << std::hex << (int)key[a];
}
If you want to go back to decimal and lowercase, you need to use std::nouppercase
and std::dec
.
conversion of uint8_t to a string [C]
1.) it's a little bit faster to eliminate the int array.
2.) adding '0'
changes the integer values 0
and 1
to their ascii values '0'
and '1'
.
3.) it's undefined behaviour to return the address of a local variable. You have to malloc memory in the heap.
4.) yes, just cut it out and do the whole operation all in one
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
char *convert(uint8_t *a)
{
char* buffer2;
int i;
buffer2 = malloc(9);
if (!buffer2)
return NULL;
buffer2[8] = 0;
for (i = 0; i <= 7; i++)
buffer2[7 - i] = (((*a) >> i) & (0x01)) + '0';
puts(buffer2);
return buffer2;
}
int main()
{
uint8_t example = 0x14;
char *final_string;
final_string = convert(&example);
if (final_string)
{
puts(final_string);
free(final_string);
}
return 0;
}
How to convert uint8 to string
Simply convert it :
fmt.Println(strconv.Itoa(int(str[1])))
How to Convert []uint8 to string
The conversion s := string(stuff)
is fine, but usually you'd expect stuff
to contain actual printable character sequences. In your case, it doesn't (mostly).
Make sure to read Strings, bytes, runes and characters in Go in detail.
Modifying your example to print byte values that have ASCII letter mappings:
package main
import (
"fmt"
)
func main() {
stuff := []byte{97, 98, 99, 100}
s := string(stuff)
fmt.Println(s)
}
Prints "abcd".
uint8_t to String, leads to no output C++ (Beginner programer)
You wrote:
stringstream list;
So I suspect you have an using namespace std;
somewhere above. The thing is, std::list
exists and is a type. In the remaining part of your program, when you write list
, it might be std::list
which is found instead. I don't know how it plays out, but I'm confident this is not what you think.
This is why using namespace std
is considered bad practice. Dont.
Convert uint8 to string
You are actually converting an array of uint8_t to a string. The canonical method is to use stringstream:
std::stringstream ss;
for (size_t i = 0; i < 6; ++i) {
ss << MACAddress[i];
if (i != 5) ss << ":";
}
std::string MACstring = ss.str();
You could avoid this by using to_string and concatenation:
std::string MACstring;
for (size_t i = 0; i < 6; ++i) {
MACstring += std::to_string(MACAddress[i]);
if (i != 5) MACstring += ":";
}
C/C++ DLL: Converting a const uint8_t to a String
You'll need UTF8Encoding to convert the bytes to characters. It has methods that take pointers, you'll want to take advantage of that. You first need to count the number of characters in the converted string, then allocate an array to store the converted characters, then you can turn it into System::String. Like this:
auto converter = gcnew System::Text::UTF8Encoding;
auto chars = converter->GetCharCount((Byte*)nativeCharArray, nativeCharArrayLength-1);
auto buffer = gcnew array<Char>(chars);
pin_ptr<Char> pbuffer = &buffer[0];
converter->GetChars((Byte*)nativeCharArray, nativeCharArrayLength-1, pbuffer, chars);
String^ result = gcnew String(buffer);
Note that the -1 on nativeCharArrayLength compensates for the zero terminator being included in the value.
From uint8_t to hex string array?
Using strtol()
to convert 2 characters at a time, explicitly using base 16 makes it easy:
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
// Assume s has an even number of hexdigit chars (at most 16) followed by a nul
void convert(const char *s, uint8_t out[static 8]) {
int i = 0;
// Handle 2 chars at a time
while (*s) {
char byte[3] = { *s, *(s + 1), 0 };
out[i++] = strtol(byte, NULL, 16);
s += 2;
}
// Fill the rest of the array with nuls
for (; i < 8; i += 1) {
out[i] = 0;
}
}
int main(void)
{
const char *input = "220209";
uint8_t myarr[8];
convert(input, myarr);
// Pretty-print the array using hex values
fputs("uint8_t myarr[8] = { ", stdout);
for (int i = 0; i < 8; i += 1) {
printf("0x%02" PRIx8, myarr[i]);
if (i < 7) {
fputs(", ", stdout);
}
}
puts(" };");
return 0;
}
Related Topics
How to Fill an Array With Random Values from a Range
How to Print Bytes as Hexadecimal
What Does the Single Ampersand After the Parameter List of a Member Function Declaration Mean
What Is the Most Effective Way For Float and Double Comparison
When Should I Write the Keyword 'Inline' For a Function/Method
Output Unicode Strings in Windows Console App
What Is the Proper Declaration of Main in C++
How to Print a List of Elements Separated by Commas
Implementation C++14 Make_Integer_Sequence
Unresolved External Symbol on Static Class Members
How to Determine Opencv Version
Correct Way of Passing Pointer to Another Thread
The Definitive C++ Book Guide and List
When and Why Will a Compiler Initialise Memory to 0Xcd, 0Xdd, etc. on Malloc/Free/New/Delete
Calling Virtual Functions Inside Constructors
"Unpacking" a Tuple to Call a Matching Function Pointer
Order of Evaluation in C++ Function Parameters
Why Do I Get the Same Sequence For Every Run With Std::Random_Device With Mingw Gcc4.8.1